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