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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 545 - (show annotations)
Mon Jul 29 17:30:45 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6935 byte(s)
perf(shots): fix performance issues of shot command (#115)
1 /**
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 import { CommandInteraction, ContextMenuInteraction, GuildMember, Message, User } from 'discord.js';
21 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 import Punishment from '../../models/Punishment';
28 import PunishmentType from '../../types/PunishmentType';
29
30 export default class ShotCommand extends BaseCommand {
31 supportsInteractions: boolean = true;
32 supportsContextMenu: boolean = true;
33
34 constructor() {
35 super('shot', 'moderation', ['Shot']);
36 }
37
38 async run(client: DiscordClient, msg: Message | CommandInteraction | ContextMenuInteraction, options: CommandOptions | InteractionOptions) {
39 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 let dm = true;
53 let reason: string | undefined;
54
55 if (msg instanceof CommandInteraction || msg instanceof ContextMenuInteraction) {
56 await msg.deferReply();
57 }
58
59 if (options.isInteraction) {
60 user = await <GuildMember> (msg instanceof ContextMenuInteraction ? options.options.getMember('user') : options.options.getMember('member'));
61
62 if (!user) {
63 await this.deferReply(msg, {
64 embeds: [
65 new MessageEmbed()
66 .setColor('#f14a60')
67 .setDescription("Invalid member given.")
68 ]
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 await this.deferReply(msg, {
90 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 if (user.id === client.user?.id) {
110 const random = Math.random() >= 0.5;
111
112 await this.deferReply(msg, {
113 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 });
116
117 return;
118 }
119
120 const anonymous = options.isInteraction ? options.options.getBoolean('anonymous') ?? false : false;
121
122 try {
123 await Punishment.create({
124 type: PunishmentType.SHOT,
125 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 reason,
130 createdAt: new Date()
131 });
132
133 // await History.create(user.id, msg.guild!, 'bean', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
134
135 try {
136 await user.send({
137 embeds: [
138 new MessageEmbed()
139 .setAuthor({
140 iconURL: <string> msg.guild!.iconURL(),
141 name: `\tYou got a shot in ${msg.guild!.name}`
142 })
143 .addFields([
144 {
145 name: "Reason",
146 value: typeof reason === 'undefined' ? '*No reason provided*' : reason
147 },
148 ...(!anonymous ? [{
149 name: "💉 Doctor",
150 value: `${(msg.member?.user as User).tag}`
151 }] : [])
152 ])
153 ]
154 });
155 }
156 catch (e) {
157 console.log(e);
158 dm = false;
159 }
160
161 // await client.logger.logBeaned(user, typeof reason === 'undefined' ? '*No reason provided*' : reason, msg.member!.user as User);
162 }
163 catch (e) {
164 console.log(e);
165 }
166
167 await this.deferReply(msg, {
168 embeds: [
169 new MessageEmbed()
170 .setAuthor({
171 name: user.user.tag,
172 iconURL: user.user.displayAvatarURL(),
173 })
174 .setDescription(user.user.tag + " got a shot." + (!dm ? "\nThey have DMs disabled. They will not know that they got a shot." : ''))
175 .addFields([
176 {
177 name: "💉 Doctor",
178 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 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26