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

Contents of /trunk/src/commands/moderation/SoftBanCommand.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: 6153 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
13 export default class SoftBanCommand extends BaseCommand {
14 supportsInteractions: boolean = true;
15
16 constructor() {
17 super('softban', 'moderation', []);
18 }
19
20 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
21 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
22 await msg.reply({
23 embeds: [
24 new MessageEmbed()
25 .setColor('#f14a60')
26 .setDescription(`This command requires at least one argument.`)
27 ]
28 });
29
30 return;
31 }
32
33 let user: User;
34 let banOptions: BanOptions = {
35 days: 7
36 };
37
38 if (options.isInteraction) {
39 user = await <User> options.options.getUser('user');
40
41 if (options.options.getString('reason')) {
42 banOptions.reason = await <string> options.options.getString('reason');
43 }
44
45 if (options.options.getInteger('days')) {
46 banOptions.days = await <number> options.options.getInteger('days');
47 }
48 }
49 else {
50 const user2 = await getUser(client, (msg as Message), options);
51
52 if (!user2) {
53 await msg.reply({
54 embeds: [
55 new MessageEmbed()
56 .setColor('#f14a60')
57 .setDescription(`Invalid user given.`)
58 ]
59 });
60
61 return;
62 }
63
64 user = user2;
65
66 const index = await options.args.indexOf('-d');
67
68 if (options.args[1]) {
69 const args = [...options.args];
70 args.shift();
71
72 if (index !== -1) {
73 args.splice(index - 1, 2)
74 }
75
76 banOptions.reason = await args.join(' ');
77 }
78
79 if (index !== -1) {
80 const days = await options.args[index + 1];
81
82 if (days === undefined) {
83 await msg.reply({
84 embeds: [
85 new MessageEmbed()
86 .setColor('#f14a60')
87 .setDescription(`Option \`-d\` (days) requires an argument.`)
88 ]
89 });
90
91 return;
92 }
93
94 if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
95 await msg.reply({
96 embeds: [
97 new MessageEmbed()
98 .setColor('#f14a60')
99 .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
100 ]
101 });
102
103 return;
104 }
105
106 banOptions.days = await parseInt(days);
107 }
108 }
109
110 let reply = await msg.reply({
111 embeds: [
112 new MessageEmbed({
113 author: {
114 name: user.tag,
115 iconURL: user.displayAvatarURL()
116 },
117 description: `${await fetchEmojiStr('loading')} Softban is in progress...`,
118 color: 'GOLD'
119 })
120 ]
121 });
122
123 if (msg instanceof CommandInteraction)
124 reply = <Message> await msg.fetchReply();
125
126 try {
127 await msg.guild?.bans.create(user, banOptions);
128 await new Promise(r => setTimeout(r, 1600));
129 await msg.guild?.bans.remove(user);
130
131 const punishment = await Punishment.create({
132 type: PunishmentType.SOFTBAN,
133 user_id: user.id,
134 guild_id: msg.guild!.id,
135 mod_id: msg.member!.user.id,
136 mod_tag: (msg.member!.user as User).tag,
137 reason: banOptions.reason ?? undefined,
138 meta: {
139 days: banOptions.days
140 }
141 });
142
143 await client.logger.logSoftBan(banOptions, msg.guild!, user, punishment);
144
145 await reply!.edit({
146 embeds: [
147 new MessageEmbed({
148 author: {
149 name: user.tag,
150 iconURL: user.displayAvatarURL()
151 },
152 description: `${await fetchEmojiStr('check')} Softbanned user ${user.tag}`,
153 fields: [
154 {
155 name: 'Softbanned by',
156 value: (<User> msg.member?.user).tag
157 },
158 {
159 name: 'Reason',
160 value: banOptions.reason ?? '*No reason provided*'
161 }
162 ]
163 })
164 ]
165 });
166 }
167 catch (e) {
168 await reply!.edit({
169 embeds: [
170 new MessageEmbed()
171 .setColor('#f14a60')
172 .setDescription("Failed to softban this user. Maybe missing permisions or I'm not allowed to ban this user?")
173 ]
174 });
175
176 return;
177 }
178 }
179 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26