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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 341 - (hide annotations)
Mon Jul 29 17:29:38 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 7701 byte(s)
refactor: removing using the old history manager
1 rakin 205 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User, Permissions } from 'discord.js';
2 rakin 106 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 Punishment from '../../models/Punishment';
9     import PunishmentType from '../../types/PunishmentType';
10     import { fetchEmojiStr } from '../../utils/Emoji';
11     import ms from 'ms';
12     import { clearTimeoutv2, getTimeouts, setTimeoutv2 } from '../../utils/setTimeout';
13 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
14 rakin 106
15 rakin 194 export default class TempBanCommand extends BaseCommand {
16 rakin 106 supportsInteractions: boolean = true;
17 rakin 205 permissions = [Permissions.FLAGS.BAN_MEMBERS];
18 rakin 106
19     constructor() {
20     super('tempban', 'moderation', []);
21     }
22    
23     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
24     if (!options.isInteraction && typeof options.args[1] === 'undefined') {
25     await msg.reply({
26     embeds: [
27     new MessageEmbed()
28     .setColor('#f14a60')
29     .setDescription(`This command requires at least two arguments.`)
30     ]
31     });
32    
33     return;
34     }
35    
36     let user: User;
37     let banOptions: BanOptions = {
38     days: 7
39     };
40     let time;
41    
42     if (options.isInteraction) {
43     user = await <User> options.options.getUser('user');
44     time = await <string> options.options.getString('time');
45    
46     if (options.options.getString('reason')) {
47     banOptions.reason = await <string> options.options.getString('reason');
48     }
49    
50     if (options.options.getInteger('days')) {
51     banOptions.days = await <number> options.options.getInteger('days');
52     }
53     }
54     else {
55     const user2 = await getUser(client, (msg as Message), options);
56    
57     if (!user2) {
58     await msg.reply({
59     embeds: [
60     new MessageEmbed()
61     .setColor('#f14a60')
62     .setDescription(`Invalid user given.`)
63     ]
64     });
65    
66     return;
67     }
68    
69     user = user2;
70    
71     options.args.shift();
72    
73     time = options.args[0];
74    
75     const index = await options.args.indexOf('-d');
76    
77     if (options.args[1]) {
78     const args = [...options.args];
79     args.shift();
80    
81     if (index !== -1) {
82     args.splice(index - 1, 2)
83     }
84    
85     banOptions.reason = await args.join(' ');
86     }
87    
88     if (index !== -1) {
89     const days = await options.args[index + 1];
90    
91     if (days === undefined) {
92     await msg.reply({
93     embeds: [
94     new MessageEmbed()
95     .setColor('#f14a60')
96     .setDescription(`Option \`-d\` (days) requires an argument.`)
97     ]
98     });
99    
100     return;
101     }
102    
103     if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
104     await msg.reply({
105     embeds: [
106     new MessageEmbed()
107     .setColor('#f14a60')
108     .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
109     ]
110     });
111    
112     return;
113     }
114    
115     banOptions.days = await parseInt(days);
116     }
117     }
118    
119     console.log(time);
120    
121     if (!time || !ms(time)) {
122     await msg.reply({
123     embeds: [
124     new MessageEmbed()
125     .setColor('#f14a60')
126     .setDescription(`The time must be a valid time identifier.`)
127     ]
128     });
129    
130     return;
131     }
132    
133     time = ms(time);
134    
135     try {
136 rakin 153 try {
137     const member = await msg.guild?.members.fetch(user.id);
138    
139 rakin 194 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to tempban this user."))) {
140     return;
141     }
142    
143 rakin 153 if (member && shouldNotModerate(client, member)) {
144     await msg.reply({
145     embeds: [
146     new MessageEmbed()
147     .setColor('#f14a60')
148     .setDescription(`This user cannot be tempbanned.`)
149     ]
150     });
151    
152     return;
153     }
154     }
155     catch (e) {
156     console.log(e);
157     }
158    
159 rakin 106 await msg.guild?.bans.create(user, banOptions);
160    
161     const punishment = await Punishment.create({
162     type: PunishmentType.TEMPBAN,
163     user_id: user.id,
164     guild_id: msg.guild!.id,
165     mod_id: msg.member!.user.id,
166     mod_tag: (msg.member!.user as User).tag,
167     reason: banOptions.reason ?? undefined,
168     meta: {
169     days: banOptions.days,
170     time
171 rakin 336 },
172     createdAt: new Date()
173 rakin 106 });
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 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26