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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 428 - (show annotations)
Mon Jul 29 17:30:11 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 9107 byte(s)
refactor: use new queue handler
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 { BanOptions, CommandInteraction, Message, User, Permissions } 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 getUser from '../../utils/getUser';
27 import Punishment from '../../models/Punishment';
28 import PunishmentType from '../../types/PunishmentType';
29 import { fetchEmojiStr } from '../../utils/Emoji';
30 import ms from 'ms';
31 import { clearTimeoutv2, getTimeouts, setTimeoutv2 } from '../../utils/setTimeout';
32 import { hasPermission, shouldNotModerate } from '../../utils/util';
33 import UnbanQueue from '../../queues/UnbanQueue';
34
35 export default class TempBanCommand extends BaseCommand {
36 supportsInteractions: boolean = true;
37 permissions = [Permissions.FLAGS.BAN_MEMBERS];
38
39 constructor() {
40 super('tempban', 'moderation', []);
41 }
42
43 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
44 if (!options.isInteraction && typeof options.args[1] === 'undefined') {
45 await msg.reply({
46 embeds: [
47 new MessageEmbed()
48 .setColor('#f14a60')
49 .setDescription(`This command requires at least two arguments.`)
50 ]
51 });
52
53 return;
54 }
55
56 let user: User;
57 let banOptions: BanOptions = {
58 days: 7
59 };
60 let time;
61
62 if (options.isInteraction) {
63 user = await <User> options.options.getUser('user');
64 time = await <string> options.options.getString('time');
65
66 if (options.options.getString('reason')) {
67 banOptions.reason = await <string> options.options.getString('reason');
68 }
69
70 if (options.options.getInteger('days')) {
71 banOptions.days = await <number> options.options.getInteger('days');
72 }
73 }
74 else {
75 const user2 = await getUser(client, (msg as Message), options);
76
77 if (!user2) {
78 await msg.reply({
79 embeds: [
80 new MessageEmbed()
81 .setColor('#f14a60')
82 .setDescription(`Invalid user given.`)
83 ]
84 });
85
86 return;
87 }
88
89 user = user2;
90
91 options.args.shift();
92
93 time = options.args[0];
94
95 const index = await options.args.indexOf('-d');
96
97 if (options.args[1]) {
98 const args = [...options.args];
99 args.shift();
100
101 if (index !== -1) {
102 args.splice(index - 1, 2)
103 }
104
105 banOptions.reason = await args.join(' ');
106 }
107
108 if (index !== -1) {
109 const days = await options.args[index + 1];
110
111 if (days === undefined) {
112 await msg.reply({
113 embeds: [
114 new MessageEmbed()
115 .setColor('#f14a60')
116 .setDescription(`Option \`-d\` (days) requires an argument.`)
117 ]
118 });
119
120 return;
121 }
122
123 if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
124 await msg.reply({
125 embeds: [
126 new MessageEmbed()
127 .setColor('#f14a60')
128 .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
129 ]
130 });
131
132 return;
133 }
134
135 banOptions.days = await parseInt(days);
136 }
137 }
138
139 console.log(time);
140
141 if (!time || !ms(time)) {
142 await msg.reply({
143 embeds: [
144 new MessageEmbed()
145 .setColor('#f14a60')
146 .setDescription(`The time must be a valid time identifier.`)
147 ]
148 });
149
150 return;
151 }
152
153 time = ms(time);
154
155 try {
156 try {
157 const member = await msg.guild?.members.fetch(user.id);
158
159 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to tempban this user."))) {
160 return;
161 }
162
163 if (member && shouldNotModerate(client, member)) {
164 await msg.reply({
165 embeds: [
166 new MessageEmbed()
167 .setColor('#f14a60')
168 .setDescription(`This user cannot be tempbanned.`)
169 ]
170 });
171
172 return;
173 }
174 }
175 catch (e) {
176 console.log(e);
177 }
178
179 await msg.guild?.bans.create(user, { ...banOptions, reason: `[TEMPBAN] ${banOptions.reason ?? '**No reason provided**'}` });
180
181 const punishment = await Punishment.create({
182 type: PunishmentType.TEMPBAN,
183 user_id: user.id,
184 guild_id: msg.guild!.id,
185 mod_id: msg.member!.user.id,
186 mod_tag: (msg.member!.user as User).tag,
187 reason: banOptions.reason ?? undefined,
188 meta: {
189 days: banOptions.days,
190 time
191 },
192 createdAt: new Date()
193 });
194
195 // const timeouts = getTimeouts();
196
197 // for (const timeout of timeouts.values()) {
198 // if (timeout.row.params) {
199 // try {
200 // const json = JSON.parse(timeout.row.params);
201
202 // if (json) {
203 // if (json[1] === user.id && timeout.row.filePath.endsWith('tempban-remove')) {
204 // await clearTimeoutv2(timeout);
205 // }
206 // }
207 // }
208 // catch (e) {
209 // console.log(e);
210 // }
211 // }
212 // }
213
214 // await setTimeoutv2('tempban-remove', time, msg.guild!.id, 'unban ' + user.id, user.id, msg.guild!.id);
215
216 for await (const queue of client.queueManager.queues.values()) {
217 if (queue instanceof UnbanQueue && queue.data!.userID === user.id && queue.data!.guildID === msg.guild!.id) {
218 await queue.cancel();
219 }
220 }
221
222 await client.queueManager.addQueue(UnbanQueue, {
223 data: {
224 userID: user.id,
225 guildID: msg.guild!.id
226 },
227 runAt: new Date(Date.now() + time)
228 });
229
230 await client.logger.logTempBan(banOptions, msg.guild!, user, punishment);
231
232 await msg.reply({
233 embeds: [
234 new MessageEmbed({
235 author: {
236 name: user.tag,
237 iconURL: user.displayAvatarURL()
238 },
239 description: `${await fetchEmojiStr('check')} Temporarily banned user ${user.tag}`,
240 fields: [
241 {
242 name: 'Banned by',
243 value: (<User> msg.member?.user).tag
244 },
245 {
246 name: 'Reason',
247 value: banOptions.reason ?? '*No reason provided*'
248 }
249 ]
250 })
251 ]
252 });
253 }
254 catch (e) {
255 await msg.reply({
256 embeds: [
257 new MessageEmbed()
258 .setColor('#f14a60')
259 .setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?")
260 ]
261 });
262
263 return;
264 }
265 }
266 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26