1 |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js'; |
import { CommandInteraction, Interaction, InteractionCollector, Message, MessageActionRow, MessageButton, User } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import MessageEmbed from '../../client/MessageEmbed'; |
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
import getUser from '../../utils/getUser'; |
import getUser from '../../utils/getUser'; |
8 |
import getMember from '../../utils/getMember'; |
import Note from '../../models/Note'; |
|
import History from '../../automod/History'; |
|
9 |
|
|
10 |
export default class NotesCommand extends BaseCommand { |
export default class NotesCommand extends BaseCommand { |
11 |
supportsInteractions: boolean = true; |
supportsInteractions: boolean = true; |
14 |
super('notes', 'moderation', []); |
super('notes', 'moderation', []); |
15 |
} |
} |
16 |
|
|
17 |
|
async genEmbed(client: DiscordClient, msg: Message | Interaction, user: User, page: number = 1) { |
18 |
|
const limit = 5; |
19 |
|
const offset = ((page < 1 ? 1 : page) - 1) * limit; |
20 |
|
|
21 |
|
const notes = await Note.find({ |
22 |
|
guild_id: msg.guild!.id, |
23 |
|
user_id: user.id, |
24 |
|
}).skip(offset).limit(limit).sort("createdAt"); |
25 |
|
|
26 |
|
let str = ''; |
27 |
|
|
28 |
|
const maxPage = Math.ceil((await Note.count({ |
29 |
|
guild_id: msg.guild!.id, |
30 |
|
user_id: user.id, |
31 |
|
})) / limit); |
32 |
|
|
33 |
|
for await (const note of notes) { |
34 |
|
str += `**Note ID**: ${note.id}\n`; |
35 |
|
str += `Note taken by: ${note.mod_tag}\n`; |
36 |
|
str += `Date: ${note.createdAt.toLocaleString()}\n`; |
37 |
|
str += `Content:\n\`\`\`\n${note.content}\n\`\`\`\n`; |
38 |
|
str += '\n'; |
39 |
|
} |
40 |
|
|
41 |
|
return { |
42 |
|
embeds: [ |
43 |
|
new MessageEmbed({ |
44 |
|
author: { |
45 |
|
name: user.tag, |
46 |
|
iconURL: user.displayAvatarURL() |
47 |
|
}, |
48 |
|
title: 'Notes', |
49 |
|
description: str === '' ? 'No notes.' : str, |
50 |
|
timestamp: new Date(), |
51 |
|
}) |
52 |
|
], |
53 |
|
components: [ |
54 |
|
this.createActionRow(page, maxPage) |
55 |
|
] |
56 |
|
}; |
57 |
|
} |
58 |
|
|
59 |
|
createActionRow(page: number, max: number) { |
60 |
|
console.log(max); |
61 |
|
|
62 |
|
const back = new MessageButton({ |
63 |
|
customId: 'notes-back-', |
64 |
|
label: '<<', |
65 |
|
style: 'PRIMARY' |
66 |
|
}); |
67 |
|
|
68 |
|
const next = new MessageButton({ |
69 |
|
customId: 'notes-next-', |
70 |
|
label: '>>', |
71 |
|
style: 'PRIMARY' |
72 |
|
}); |
73 |
|
|
74 |
|
let nextPage = page + 1; |
75 |
|
console.log(nextPage); |
76 |
|
|
77 |
|
if (nextPage > max) { |
78 |
|
nextPage = max; |
79 |
|
next.setDisabled(true); |
80 |
|
} |
81 |
|
else { |
82 |
|
next.setDisabled(false); |
83 |
|
} |
84 |
|
|
85 |
|
let prevPage = page - 1; |
86 |
|
console.log(prevPage); |
87 |
|
|
88 |
|
if (prevPage <= 0) { |
89 |
|
prevPage = 1; |
90 |
|
back.setDisabled(true); |
91 |
|
} |
92 |
|
else { |
93 |
|
back.setDisabled(false); |
94 |
|
} |
95 |
|
|
96 |
|
next.setCustomId('notes-next-' + nextPage); |
97 |
|
back.setCustomId('notes-back-' + prevPage); |
98 |
|
|
99 |
|
return new MessageActionRow() |
100 |
|
.addComponents( |
101 |
|
back, |
102 |
|
next |
103 |
|
); |
104 |
|
} |
105 |
|
|
106 |
|
async update(client: DiscordClient, interaction: Interaction, message: Message) { |
107 |
|
console.log('here'); |
108 |
|
|
109 |
|
if (interaction.isButton() && interaction.customId.startsWith('notes-')) { |
110 |
|
const splitted = interaction.customId.split('-'); |
111 |
|
|
112 |
|
if (splitted[2] === undefined || parseInt(splitted[2]) === NaN) |
113 |
|
return; |
114 |
|
|
115 |
|
if (splitted[1] === 'next' || splitted[1] === 'back') { |
116 |
|
const options = await this.genEmbed(client, interaction, (global as any).user, parseInt(splitted[2])); |
117 |
|
|
118 |
|
try { |
119 |
|
await interaction.update(options); |
120 |
|
} |
121 |
|
catch (e) { |
122 |
|
console.log(e); |
123 |
|
} |
124 |
|
} |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
129 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
130 |
await msg.reply({ |
await msg.reply({ |
138 |
return; |
return; |
139 |
} |
} |
140 |
|
|
141 |
let user: GuildMember; |
let user: User | null | undefined; |
142 |
|
|
143 |
if (options.isInteraction) { |
if (options.isInteraction) { |
144 |
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; |
|
|
} |
|
145 |
} |
} |
146 |
else { |
else { |
147 |
try { |
try { |
148 |
const user2 = await getMember((msg as Message), options); |
user = await getUser(client, msg as Message, options); |
|
|
|
|
if (!user2) { |
|
|
throw new Error('Invalid user'); |
|
|
} |
|
149 |
|
|
150 |
user = user2; |
if (!user) |
151 |
|
throw new Error(); |
152 |
} |
} |
153 |
catch (e) { |
catch (e) { |
154 |
|
console.log(e); |
155 |
|
|
156 |
await msg.reply({ |
await msg.reply({ |
157 |
embeds: [ |
embeds: [ |
158 |
new MessageEmbed() |
new MessageEmbed() |
160 |
.setDescription(`Invalid user given.`) |
.setDescription(`Invalid user given.`) |
161 |
] |
] |
162 |
}); |
}); |
163 |
|
|
164 |
return; |
return; |
165 |
} |
} |
|
|
|
|
console.log(user); |
|
166 |
} |
} |
167 |
|
|
168 |
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); |
|
|
} |
|
|
|
|
|
if (data === undefined || data.length < 1) { |
|
|
await msg.reply({ |
|
|
embeds: [ |
|
|
new MessageEmbed() |
|
|
.setColor('#f14a60') |
|
|
.setDescription('No notes found for user ' + user.user.tag) |
|
|
] |
|
|
}); |
|
169 |
|
|
170 |
return; |
let message = <Message> await msg.reply(await this.genEmbed(client, msg, user)); |
|
} |
|
171 |
|
|
172 |
let desc = ''; |
if (msg instanceof CommandInteraction) |
173 |
|
message = <Message> await msg.fetchReply(); |
174 |
|
|
175 |
for (let row of data) { |
const collector = new InteractionCollector(client, { |
176 |
desc += `\n\n**Note #${row.id}**\n${row.content}\nDate: ${new Date(row.date).toUTCString()}`; |
guild: msg.guild!, |
177 |
|
channel: msg.channel!, |
178 |
|
max: 20, |
179 |
|
componentType: 'BUTTON', |
180 |
|
interactionType: 'MESSAGE_COMPONENT', |
181 |
|
message, |
182 |
|
time: 30000, |
183 |
|
filter(i) { |
184 |
|
return i.isButton() && i.member?.user.id === msg.member!.user.id; |
185 |
} |
} |
186 |
|
}); |
187 |
|
|
188 |
desc = desc.substring(1); |
collector.on('collect', async i => { |
189 |
|
await this.update(client, i, message); |
190 |
|
}); |
191 |
|
|
192 |
await msg.reply({ |
collector.on('end', async () => { |
193 |
embeds: [ |
try { |
194 |
new MessageEmbed() |
await message.edit({ |
195 |
.setAuthor({ |
components: [] |
196 |
iconURL: user.displayAvatarURL(), |
}); |
197 |
name: user.user.tag |
} |
198 |
}) |
catch (e) { |
199 |
.setDescription(desc) |
console.log(e); |
200 |
] |
} |
|
}); |
|
201 |
}); |
}); |
202 |
} |
} |
203 |
} |
} |