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

Annotation of /branches/3.x/src/commands/moderation/SoftBanCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 7585 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { BanOptions, CommandInteraction, Message, User, Permissions } from 'discord.js';
21     import BaseCommand from '../../utils/structures/BaseCommand';
22     import DiscordClient from '../../client/Client';
23     import CommandOptions from '../../types/CommandOptions';
24     import InteractionOptions from '../../types/InteractionOptions';
25     import MessageEmbed from '../../client/MessageEmbed';
26     import getUser from '../../utils/getUser';
27     import Punishment from '../../models/Punishment';
28     import PunishmentType from '../../types/PunishmentType';
29     import { fetchEmojiStr } from '../../utils/Emoji';
30     import { hasPermission, shouldNotModerate } from '../../utils/util';
31    
32     export default class SoftBanCommand extends BaseCommand {
33     supportsInteractions: boolean = true;
34     permissions = [Permissions.FLAGS.BAN_MEMBERS];
35    
36     constructor() {
37     super('softban', 'moderation', []);
38     }
39    
40     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
41     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
42     await msg.reply({
43     embeds: [
44     new MessageEmbed()
45     .setColor('#f14a60')
46     .setDescription(`This command requires at least one argument.`)
47     ]
48     });
49    
50     return;
51     }
52    
53     let user: User;
54     let banOptions: BanOptions = {
55     days: 7
56     };
57    
58     if (options.isInteraction) {
59     user = await <User> options.options.getUser('user');
60    
61     if (options.options.getString('reason')) {
62     banOptions.reason = await <string> options.options.getString('reason');
63     }
64    
65     if (options.options.getInteger('days')) {
66     banOptions.days = await <number> options.options.getInteger('days');
67     }
68     }
69     else {
70     const user2 = await getUser(client, (msg as Message), options);
71    
72     if (!user2) {
73     await msg.reply({
74     embeds: [
75     new MessageEmbed()
76     .setColor('#f14a60')
77     .setDescription(`Invalid user given.`)
78     ]
79     });
80    
81     return;
82     }
83    
84     user = user2;
85    
86     const index = await options.args.indexOf('-d');
87    
88     if (options.args[1]) {
89     const args = [...options.args];
90     args.shift();
91    
92     if (index !== -1) {
93     args.splice(index - 1, 2)
94     }
95    
96     banOptions.reason = await args.join(' ');
97     }
98    
99     if (index !== -1) {
100     const days = await options.args[index + 1];
101    
102     if (days === undefined) {
103     await msg.reply({
104     embeds: [
105     new MessageEmbed()
106     .setColor('#f14a60')
107     .setDescription(`Option \`-d\` (days) requires an argument.`)
108     ]
109     });
110    
111     return;
112     }
113    
114     if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
115     await msg.reply({
116     embeds: [
117     new MessageEmbed()
118     .setColor('#f14a60')
119     .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
120     ]
121     });
122    
123     return;
124     }
125    
126     banOptions.days = await parseInt(days);
127     }
128     }
129    
130     let reply = await msg.reply({
131     embeds: [
132     new MessageEmbed({
133     author: {
134     name: user.tag,
135     iconURL: user.displayAvatarURL()
136     },
137     description: `${await fetchEmojiStr('loading')} Softban is in progress...`,
138     color: 'GOLD'
139     })
140     ]
141     });
142    
143     if (msg instanceof CommandInteraction)
144     reply = <Message> await msg.fetchReply();
145    
146     try {
147     try {
148     const member = await msg.guild!.members.fetch(user.id);
149    
150     if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to softban this user."))) {
151     return;
152     }
153    
154     if (member && shouldNotModerate(client, member)) {
155     await msg.reply({
156     embeds: [
157     new MessageEmbed()
158     .setColor('#f14a60')
159     .setDescription('This user cannot be softbanned.')
160     ]
161     });
162    
163     return;
164     }
165     }
166     catch (e) {
167     console.log(e);
168     }
169    
170     await msg.guild?.bans.create(user, banOptions);
171     await new Promise(r => setTimeout(r, 1600));
172     await msg.guild?.bans.remove(user);
173    
174     const punishment = await Punishment.create({
175     type: PunishmentType.SOFTBAN,
176     user_id: user.id,
177     guild_id: msg.guild!.id,
178     mod_id: msg.member!.user.id,
179     mod_tag: (msg.member!.user as User).tag,
180     reason: banOptions.reason ?? undefined,
181     meta: {
182     days: banOptions.days
183     },
184     createdAt: new Date()
185     });
186    
187     await client.logger.logSoftBan(banOptions, msg.guild!, user, punishment);
188    
189     await reply!.edit({
190     embeds: [
191     new MessageEmbed({
192     author: {
193     name: user.tag,
194     iconURL: user.displayAvatarURL()
195     },
196     description: `${await fetchEmojiStr('check')} Softbanned user ${user.tag}`,
197     fields: [
198     {
199     name: 'Softbanned by',
200     value: (<User> msg.member?.user).tag
201     },
202     {
203     name: 'Reason',
204     value: banOptions.reason ?? '*No reason provided*'
205     }
206     ]
207     })
208     ]
209     });
210     }
211     catch (e) {
212     await reply!.edit({
213     embeds: [
214     new MessageEmbed()
215     .setColor('#f14a60')
216     .setDescription("Failed to softban this user. Maybe missing permisions or I'm not allowed to ban this user?")
217     ]
218     });
219    
220     return;
221     }
222     }
223     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26