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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26