/[sudobot]/trunk/src/commands/moderation/NotesCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/moderation/NotesCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 336 - (show annotations)
Mon Jul 29 17:29:36 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6224 byte(s)
refactor(moderation): use mongodb
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
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) {
129 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
130 await msg.reply({
131 embeds: [
132 new MessageEmbed()
133 .setColor('#f14a60')
134 .setDescription(`This command requires at least one argument.`)
135 ]
136 });
137
138 return;
139 }
140
141 let user: User | null | undefined;
142
143 if (options.isInteraction) {
144 user = await <User> options.options.getUser('user');
145 }
146 else {
147 try {
148 user = await getUser(client, msg as Message, options);
149
150 if (!user)
151 throw new Error();
152 }
153 catch (e) {
154 console.log(e);
155
156 await msg.reply({
157 embeds: [
158 new MessageEmbed()
159 .setColor('#f14a60')
160 .setDescription(`Invalid user given.`)
161 ]
162 });
163
164 return;
165 }
166 }
167
168 (global as any).user = user;
169
170 let message = <Message> await msg.reply(await this.genEmbed(client, msg, user));
171
172 if (msg instanceof CommandInteraction)
173 message = <Message> await msg.fetchReply();
174
175 const collector = new InteractionCollector(client, {
176 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 collector.on('collect', async i => {
189 await this.update(client, i, message);
190 });
191
192 collector.on('end', async () => {
193 try {
194 await message.edit({
195 components: []
196 });
197 }
198 catch (e) {
199 console.log(e);
200 }
201 });
202 }
203 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26