1 |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js'; |
/** |
2 |
|
* This file is part of SudoBot. |
3 |
|
* |
4 |
|
* Copyright (C) 2021-2022 OSN Inc. |
5 |
|
* |
6 |
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU Affero General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
*/ |
19 |
|
|
20 |
|
import { CommandInteraction, Interaction, InteractionCollector, Message, MessageActionRow, MessageButton, User } from 'discord.js'; |
21 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
import BaseCommand from '../../utils/structures/BaseCommand'; |
22 |
import DiscordClient from '../../client/Client'; |
import DiscordClient from '../../client/Client'; |
23 |
import CommandOptions from '../../types/CommandOptions'; |
import CommandOptions from '../../types/CommandOptions'; |
24 |
import InteractionOptions from '../../types/InteractionOptions'; |
import InteractionOptions from '../../types/InteractionOptions'; |
25 |
import MessageEmbed from '../../client/MessageEmbed'; |
import MessageEmbed from '../../client/MessageEmbed'; |
26 |
import getUser from '../../utils/getUser'; |
import getUser from '../../utils/getUser'; |
27 |
import getMember from '../../utils/getMember'; |
import Note from '../../models/Note'; |
|
import History from '../../automod/History'; |
|
28 |
|
|
29 |
export default class NotesCommand extends BaseCommand { |
export default class NotesCommand extends BaseCommand { |
30 |
supportsInteractions: boolean = true; |
supportsInteractions: boolean = true; |
33 |
super('notes', 'moderation', []); |
super('notes', 'moderation', []); |
34 |
} |
} |
35 |
|
|
36 |
|
async genEmbed(client: DiscordClient, msg: Message | Interaction, user: User, page: number = 1) { |
37 |
|
const limit = 5; |
38 |
|
const offset = ((page < 1 ? 1 : page) - 1) * limit; |
39 |
|
|
40 |
|
const notes = await Note.find({ |
41 |
|
guild_id: msg.guild!.id, |
42 |
|
user_id: user.id, |
43 |
|
}).skip(offset).limit(limit).sort("createdAt"); |
44 |
|
|
45 |
|
let str = ''; |
46 |
|
|
47 |
|
const maxPage = Math.ceil((await Note.count({ |
48 |
|
guild_id: msg.guild!.id, |
49 |
|
user_id: user.id, |
50 |
|
})) / limit); |
51 |
|
|
52 |
|
for await (const note of notes) { |
53 |
|
str += `**Note ID**: ${note.id}\n`; |
54 |
|
str += `Note taken by: ${note.mod_tag}\n`; |
55 |
|
str += `Date: ${note.createdAt.toLocaleString()}\n`; |
56 |
|
str += `Content:\n\`\`\`\n${note.content}\n\`\`\`\n`; |
57 |
|
str += '\n'; |
58 |
|
} |
59 |
|
|
60 |
|
return { |
61 |
|
embeds: [ |
62 |
|
new MessageEmbed({ |
63 |
|
author: { |
64 |
|
name: user.tag, |
65 |
|
iconURL: user.displayAvatarURL() |
66 |
|
}, |
67 |
|
title: 'Notes', |
68 |
|
description: str === '' ? 'No notes.' : str, |
69 |
|
timestamp: new Date(), |
70 |
|
}) |
71 |
|
], |
72 |
|
components: [ |
73 |
|
this.createActionRow(page, maxPage) |
74 |
|
] |
75 |
|
}; |
76 |
|
} |
77 |
|
|
78 |
|
createActionRow(page: number, max: number) { |
79 |
|
console.log(max); |
80 |
|
|
81 |
|
const back = new MessageButton({ |
82 |
|
customId: 'notes-back-', |
83 |
|
label: '<<', |
84 |
|
style: 'PRIMARY' |
85 |
|
}); |
86 |
|
|
87 |
|
const next = new MessageButton({ |
88 |
|
customId: 'notes-next-', |
89 |
|
label: '>>', |
90 |
|
style: 'PRIMARY' |
91 |
|
}); |
92 |
|
|
93 |
|
let nextPage = page + 1; |
94 |
|
console.log(nextPage); |
95 |
|
|
96 |
|
if (nextPage > max) { |
97 |
|
nextPage = max; |
98 |
|
next.setDisabled(true); |
99 |
|
} |
100 |
|
else { |
101 |
|
next.setDisabled(false); |
102 |
|
} |
103 |
|
|
104 |
|
let prevPage = page - 1; |
105 |
|
console.log(prevPage); |
106 |
|
|
107 |
|
if (prevPage <= 0) { |
108 |
|
prevPage = 1; |
109 |
|
back.setDisabled(true); |
110 |
|
} |
111 |
|
else { |
112 |
|
back.setDisabled(false); |
113 |
|
} |
114 |
|
|
115 |
|
next.setCustomId('notes-next-' + nextPage); |
116 |
|
back.setCustomId('notes-back-' + prevPage); |
117 |
|
|
118 |
|
return new MessageActionRow() |
119 |
|
.addComponents( |
120 |
|
back, |
121 |
|
next |
122 |
|
); |
123 |
|
} |
124 |
|
|
125 |
|
async update(client: DiscordClient, interaction: Interaction, message: Message) { |
126 |
|
console.log('here'); |
127 |
|
|
128 |
|
if (interaction.isButton() && interaction.customId.startsWith('notes-')) { |
129 |
|
const splitted = interaction.customId.split('-'); |
130 |
|
|
131 |
|
if (splitted[2] === undefined || parseInt(splitted[2]) === NaN) |
132 |
|
return; |
133 |
|
|
134 |
|
if (splitted[1] === 'next' || splitted[1] === 'back') { |
135 |
|
const options = await this.genEmbed(client, interaction, (global as any).user, parseInt(splitted[2])); |
136 |
|
|
137 |
|
try { |
138 |
|
await interaction.update(options); |
139 |
|
} |
140 |
|
catch (e) { |
141 |
|
console.log(e); |
142 |
|
} |
143 |
|
} |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
148 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
149 |
await msg.reply({ |
await msg.reply({ |
157 |
return; |
return; |
158 |
} |
} |
159 |
|
|
160 |
let user: GuildMember; |
let user: User | null | undefined; |
161 |
|
|
162 |
if (options.isInteraction) { |
if (options.isInteraction) { |
163 |
user = await <GuildMember> options.options.getMember('member'); |
user = await <User> options.options.getUser('user'); |
|
|
|
|
if (!user) { |
|
|
await msg.reply({ |
|
|
embeds: [ |
|
|
new MessageEmbed() |
|
|
.setColor('#f14a60') |
|
|
.setDescription("Invalid user given.") |
|
|
] |
|
|
}); |
|
|
|
|
|
return; |
|
|
} |
|
164 |
} |
} |
165 |
else { |
else { |
166 |
try { |
try { |
167 |
const user2 = await getMember((msg as Message), options); |
user = await getUser(client, msg as Message, options); |
|
|
|
|
if (!user2) { |
|
|
throw new Error('Invalid user'); |
|
|
} |
|
168 |
|
|
169 |
user = user2; |
if (!user) |
170 |
|
throw new Error(); |
171 |
} |
} |
172 |
catch (e) { |
catch (e) { |
173 |
|
console.log(e); |
174 |
|
|
175 |
await msg.reply({ |
await msg.reply({ |
176 |
embeds: [ |
embeds: [ |
177 |
new MessageEmbed() |
new MessageEmbed() |
179 |
.setDescription(`Invalid user given.`) |
.setDescription(`Invalid user given.`) |
180 |
] |
] |
181 |
}); |
}); |
182 |
|
|
183 |
return; |
return; |
184 |
} |
} |
|
|
|
|
console.log(user); |
|
185 |
} |
} |
186 |
|
|
187 |
await client.db.all("SELECT * FROM notes WHERE user_id = ? AND guild_id = ?", [user.id, msg.guild!.id], async (err: any, data: any) => { |
(global as any).user = user; |
|
if (err) { |
|
|
console.log(err); |
|
|
} |
|
188 |
|
|
189 |
if (data === undefined || data.length < 1) { |
let message = <Message> await msg.reply(await this.genEmbed(client, msg, user)); |
|
await msg.reply({ |
|
|
embeds: [ |
|
|
new MessageEmbed() |
|
|
.setColor('#f14a60') |
|
|
.setDescription('No notes found for user ' + user.user.tag) |
|
|
] |
|
|
}); |
|
|
|
|
|
return; |
|
|
} |
|
190 |
|
|
191 |
let desc = ''; |
if (msg instanceof CommandInteraction) |
192 |
|
message = <Message> await msg.fetchReply(); |
193 |
|
|
194 |
for (let row of data) { |
const collector = new InteractionCollector(client, { |
195 |
desc += `\n\n**Note #${row.id}**\n${row.content}\nDate: ${new Date(row.date).toUTCString()}`; |
guild: msg.guild!, |
196 |
|
channel: msg.channel!, |
197 |
|
max: 20, |
198 |
|
componentType: 'BUTTON', |
199 |
|
interactionType: 'MESSAGE_COMPONENT', |
200 |
|
message, |
201 |
|
time: 30000, |
202 |
|
filter(i) { |
203 |
|
return i.isButton() && i.member?.user.id === msg.member!.user.id; |
204 |
} |
} |
205 |
|
}); |
206 |
|
|
207 |
desc = desc.substring(1); |
collector.on('collect', async i => { |
208 |
|
await this.update(client, i, message); |
209 |
|
}); |
210 |
|
|
211 |
await msg.reply({ |
collector.on('end', async () => { |
212 |
embeds: [ |
try { |
213 |
new MessageEmbed() |
await message.edit({ |
214 |
.setAuthor({ |
components: [] |
215 |
iconURL: user.displayAvatarURL(), |
}); |
216 |
name: user.user.tag |
} |
217 |
}) |
catch (e) { |
218 |
.setDescription(desc) |
console.log(e); |
219 |
] |
} |
|
}); |
|
220 |
}); |
}); |
221 |
} |
} |
222 |
} |
} |