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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 545 - (hide annotations)
Mon Jul 29 17:30:45 2024 UTC (8 months, 3 weeks ago) by rakin
File MIME type: application/typescript
File size: 6935 byte(s)
perf(shots): fix performance issues of shot command (#115)
1 rakin 393 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20 rakin 344 import { CommandInteraction, ContextMenuInteraction, GuildMember, Message, User } from 'discord.js';
21 rakin 51 import BaseCommand from '../../utils/structures/BaseCommand';
22     import DiscordClient from '../../client/Client';
23     import CommandOptions from '../../types/CommandOptions';
24     import InteractionOptions from '../../types/InteractionOptions';
25     import MessageEmbed from '../../client/MessageEmbed';
26     import getMember from '../../utils/getMember';
27 rakin 86 import Punishment from '../../models/Punishment';
28     import PunishmentType from '../../types/PunishmentType';
29 rakin 51
30 rakin 197 export default class ShotCommand extends BaseCommand {
31 rakin 51 supportsInteractions: boolean = true;
32 rakin 125 supportsContextMenu: boolean = true;
33 rakin 51
34     constructor() {
35 rakin 197 super('shot', 'moderation', ['Shot']);
36 rakin 51 }
37    
38 rakin 125 async run(client: DiscordClient, msg: Message | CommandInteraction | ContextMenuInteraction, options: CommandOptions | InteractionOptions) {
39 rakin 51 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
40     await msg.reply({
41     embeds: [
42     new MessageEmbed()
43     .setColor('#f14a60')
44     .setDescription(`This command requires at least one argument.`)
45     ]
46     });
47    
48     return;
49     }
50    
51     let user: GuildMember;
52 rakin 160 let dm = true;
53 rakin 51 let reason: string | undefined;
54 rakin 363
55 rakin 545 if (msg instanceof CommandInteraction || msg instanceof ContextMenuInteraction) {
56     await msg.deferReply();
57     }
58    
59 rakin 51 if (options.isInteraction) {
60 rakin 125 user = await <GuildMember> (msg instanceof ContextMenuInteraction ? options.options.getMember('user') : options.options.getMember('member'));
61 rakin 51
62     if (!user) {
63 rakin 545 await this.deferReply(msg, {
64 rakin 51 embeds: [
65     new MessageEmbed()
66     .setColor('#f14a60')
67 rakin 125 .setDescription("Invalid member given.")
68 rakin 51 ]
69     });
70    
71     return;
72     }
73    
74     if (options.options.getString('reason')) {
75     reason = await <string> options.options.getString('reason');
76     }
77     }
78     else {
79     try {
80     const user2 = await getMember((msg as Message), options);
81    
82     if (!user2) {
83     throw new Error('Invalid user');
84     }
85    
86     user = user2;
87     }
88     catch (e) {
89 rakin 545 await this.deferReply(msg, {
90 rakin 51 embeds: [
91     new MessageEmbed()
92     .setColor('#f14a60')
93     .setDescription(`Invalid user given.`)
94     ]
95     });
96    
97     return;
98     }
99    
100     console.log(user);
101    
102     if (options.args[1]) {
103     const args = [...options.args];
104     args.shift();
105     reason = await args.join(' ');
106     }
107     }
108    
109 rakin 384 if (user.id === client.user?.id) {
110 rakin 392 const random = Math.random() >= 0.5;
111    
112 rakin 545 await this.deferReply(msg, {
113 rakin 392 content: `Oh no no no... wait wait, you can't just do that with me${random ? "... I'm such an innocent bot :innocent:, PWEASE don't do that :pleading_face:" : "!?!? Can you?"}`,
114     files: [random ? "https://tenor.com/view/folded-hands-emoji-funny-animals-gray-cat-cute-pwease-gif-14039745.gif" : 'https://tenor.com/view/are-you-even-capable-vera-bennett-wentworth-can-you-handle-this-are-you-qualified-gif-22892513.gif']
115 rakin 391 });
116    
117 rakin 384 return;
118     }
119    
120 rakin 363 const anonymous = options.isInteraction ? options.options.getBoolean('anonymous') ?? false : false;
121    
122 rakin 51 try {
123 rakin 86 await Punishment.create({
124 rakin 197 type: PunishmentType.SHOT,
125 rakin 86 user_id: user.id,
126     guild_id: msg.guild!.id,
127     mod_id: msg.member!.user.id,
128     mod_tag: (msg.member!.user as User).tag,
129 rakin 336 reason,
130     createdAt: new Date()
131 rakin 86 });
132    
133 rakin 197 // await History.create(user.id, msg.guild!, 'bean', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
134 rakin 51
135 rakin 189 try {
136     await user.send({
137     embeds: [
138     new MessageEmbed()
139     .setAuthor({
140     iconURL: <string> msg.guild!.iconURL(),
141 rakin 197 name: `\tYou got a shot in ${msg.guild!.name}`
142 rakin 189 })
143     .addFields([
144     {
145     name: "Reason",
146     value: typeof reason === 'undefined' ? '*No reason provided*' : reason
147 rakin 363 },
148     ...(!anonymous ? [{
149     name: "💉 Doctor",
150     value: `${(msg.member?.user as User).tag}`
151     }] : [])
152 rakin 189 ])
153     ]
154     });
155 rakin 160 }
156     catch (e) {
157     console.log(e);
158     dm = false;
159     }
160 rakin 51
161 rakin 197 // await client.logger.logBeaned(user, typeof reason === 'undefined' ? '*No reason provided*' : reason, msg.member!.user as User);
162 rakin 51 }
163     catch (e) {
164     console.log(e);
165     }
166    
167 rakin 545 await this.deferReply(msg, {
168 rakin 51 embeds: [
169     new MessageEmbed()
170     .setAuthor({
171     name: user.user.tag,
172     iconURL: user.user.displayAvatarURL(),
173     })
174 rakin 197 .setDescription(user.user.tag + " got a shot." + (!dm ? "\nThey have DMs disabled. They will not know that they got a shot." : ''))
175 rakin 51 .addFields([
176     {
177 rakin 363 name: "💉 Doctor",
178 rakin 51 value: (msg.member!.user as User).tag
179     },
180     {
181     name: "Reason",
182     value: reason === undefined ? "*No reason provided*" : reason
183     }
184     ])
185     ]
186     });
187     }
188 rakin 160 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26