/[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 153 - (hide annotations)
Mon Jul 29 17:28:48 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7498 byte(s)
Add Non-moderable role (#34)

* feat(utils): add shouldNotModerate() function

* fix(mod-cmds): no non-moderable role checking
1 rakin 106 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } 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 rakin 153 import { shouldNotModerate } from '../../utils/util';
15 rakin 106
16     export default class tempBanCommand extends BaseCommand {
17     supportsInteractions: boolean = true;
18    
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     if (member && shouldNotModerate(client, member)) {
140     await msg.reply({
141     embeds: [
142     new MessageEmbed()
143     .setColor('#f14a60')
144     .setDescription(`This user cannot be tempbanned.`)
145     ]
146     });
147    
148     return;
149     }
150     }
151     catch (e) {
152     console.log(e);
153     return;
154     }
155    
156 rakin 106 await msg.guild?.bans.create(user, banOptions);
157    
158     const punishment = await Punishment.create({
159     type: PunishmentType.TEMPBAN,
160     user_id: user.id,
161     guild_id: msg.guild!.id,
162     mod_id: msg.member!.user.id,
163     mod_tag: (msg.member!.user as User).tag,
164     reason: banOptions.reason ?? undefined,
165     meta: {
166     days: banOptions.days,
167     time
168     }
169     });
170    
171     const timeouts = getTimeouts();
172    
173     for (const timeout of timeouts.values()) {
174     if (timeout.row.params) {
175     try {
176     const json = JSON.parse(timeout.row.params);
177    
178     if (json) {
179     if (json[1] === user.id && timeout.row.filePath.endsWith('tempban-remove')) {
180     await clearTimeoutv2(timeout);
181     }
182     }
183     }
184     catch (e) {
185     console.log(e);
186     }
187     }
188     }
189    
190     await setTimeoutv2('tempban-remove', time, msg.guild!.id, 'unban ' + user.id, user.id, msg.guild!.id);
191    
192     await client.logger.logTempBan(banOptions, msg.guild!, user, punishment);
193    
194     await msg.reply({
195     embeds: [
196     new MessageEmbed({
197     author: {
198     name: user.tag,
199     iconURL: user.displayAvatarURL()
200     },
201     description: `${await fetchEmojiStr('check')} Temporarily banned user ${user.tag}`,
202     fields: [
203     {
204     name: 'Banned by',
205     value: (<User> msg.member?.user).tag
206     },
207     {
208     name: 'Reason',
209     value: banOptions.reason ?? '*No reason provided*'
210     }
211     ]
212     })
213     ]
214     });
215     }
216     catch (e) {
217     await msg.reply({
218     embeds: [
219     new MessageEmbed()
220     .setColor('#f14a60')
221     .setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?")
222     ]
223     });
224    
225     return;
226     }
227     }
228 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26