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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26