/[sudobot]/branches/3.x/src/commands/moderation/TempBanCommand.ts
ViewVC logotype

Contents of /branches/3.x/src/commands/moderation/TempBanCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26