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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 344 - (hide annotations)
Mon Jul 29 17:29:40 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 6129 byte(s)
chore: eslint autofix
1 rakin 344 import { CommandInteraction, Interaction, InteractionCollector, Message, MessageActionRow, MessageButton, User } from 'discord.js';
2 rakin 51 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 rakin 86 import Note from '../../models/Note';
9 rakin 51
10     export default class NotesCommand extends BaseCommand {
11     supportsInteractions: boolean = true;
12    
13     constructor() {
14     super('notes', 'moderation', []);
15     }
16    
17 rakin 86 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 rakin 334 const notes = await Note.find({
22     guild_id: msg.guild!.id,
23     user_id: user.id,
24     }).skip(offset).limit(limit).sort("createdAt");
25 rakin 86
26     let str = '';
27 rakin 336
28     const maxPage = Math.ceil((await Note.count({
29     guild_id: msg.guild!.id,
30     user_id: user.id,
31     })) / limit);
32 rakin 86
33 rakin 334 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 rakin 86 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 rakin 51 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 rakin 86 let user: User | null | undefined;
142 rakin 51
143     if (options.isInteraction) {
144 rakin 86 user = await <User> options.options.getUser('user');
145 rakin 51 }
146     else {
147     try {
148 rakin 86 user = await getUser(client, msg as Message, options);
149 rakin 51
150 rakin 86 if (!user)
151     throw new Error();
152 rakin 51 }
153     catch (e) {
154 rakin 86 console.log(e);
155    
156 rakin 51 await msg.reply({
157     embeds: [
158     new MessageEmbed()
159     .setColor('#f14a60')
160     .setDescription(`Invalid user given.`)
161     ]
162     });
163 rakin 86
164 rakin 51 return;
165     }
166     }
167    
168 rakin 86 (global as any).user = user;
169 rakin 51
170 rakin 86 let message = <Message> await msg.reply(await this.genEmbed(client, msg, user));
171 rakin 51
172 rakin 86 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 rakin 51 }
186 rakin 86 });
187 rakin 51
188 rakin 86 collector.on('collect', async i => {
189     await this.update(client, i, message);
190     });
191 rakin 51
192 rakin 86 collector.on('end', async () => {
193     try {
194     await message.edit({
195     components: []
196     });
197 rakin 51 }
198 rakin 86 catch (e) {
199     console.log(e);
200     }
201 rakin 51 });
202     }
203     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26