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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26