/[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 334 - (hide annotations)
Mon Jul 29 17:29:36 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 6129 byte(s)
refactor(models): use mongodb in Note
1 rakin 86 import { BanOptions, CommandInteraction, EmojiIdentifierResolvable, GuildMember, Interaction, InteractionCollector, Message, MessageActionRow, MessageButton, MessageOptions, ReplyOptions, TextChannel, 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 334 const maxPage = Math.ceil(notes.length / limit);
28 rakin 86
29 rakin 334 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 rakin 86 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 rakin 51 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 rakin 86 let user: User | null | undefined;
138 rakin 51
139     if (options.isInteraction) {
140 rakin 86 user = await <User> options.options.getUser('user');
141 rakin 51 }
142     else {
143     try {
144 rakin 86 user = await getUser(client, msg as Message, options);
145 rakin 51
146 rakin 86 if (!user)
147     throw new Error();
148 rakin 51 }
149     catch (e) {
150 rakin 86 console.log(e);
151    
152 rakin 51 await msg.reply({
153     embeds: [
154     new MessageEmbed()
155     .setColor('#f14a60')
156     .setDescription(`Invalid user given.`)
157     ]
158     });
159 rakin 86
160 rakin 51 return;
161     }
162     }
163    
164 rakin 86 (global as any).user = user;
165 rakin 51
166 rakin 86 let message = <Message> await msg.reply(await this.genEmbed(client, msg, user));
167 rakin 51
168 rakin 86 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 rakin 51 }
182 rakin 86 });
183 rakin 51
184 rakin 86 collector.on('collect', async i => {
185     await this.update(client, i, message);
186     });
187 rakin 51
188 rakin 86 collector.on('end', async () => {
189     try {
190     await message.edit({
191     components: []
192     });
193 rakin 51 }
194 rakin 86 catch (e) {
195     console.log(e);
196     }
197 rakin 51 });
198     }
199     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26