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

Annotation of /trunk/src/commands/moderation/SoftBanCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 341 - (hide annotations)
Mon Jul 29 17:29:38 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 6887 byte(s)
refactor: removing using the old history manager
1 rakin 205 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User, Permissions } from 'discord.js';
2 rakin 106 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 Punishment from '../../models/Punishment';
9     import PunishmentType from '../../types/PunishmentType';
10     import { fetchEmojiStr } from '../../utils/Emoji';
11 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
12 rakin 106
13     export default class SoftBanCommand extends BaseCommand {
14     supportsInteractions: boolean = true;
15 rakin 205 permissions = [Permissions.FLAGS.BAN_MEMBERS];
16 rakin 106
17     constructor() {
18     super('softban', 'moderation', []);
19     }
20    
21     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
22     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
23     await msg.reply({
24     embeds: [
25     new MessageEmbed()
26     .setColor('#f14a60')
27     .setDescription(`This command requires at least one argument.`)
28     ]
29     });
30    
31     return;
32     }
33    
34     let user: User;
35     let banOptions: BanOptions = {
36     days: 7
37     };
38    
39     if (options.isInteraction) {
40     user = await <User> options.options.getUser('user');
41    
42     if (options.options.getString('reason')) {
43     banOptions.reason = await <string> options.options.getString('reason');
44     }
45    
46     if (options.options.getInteger('days')) {
47     banOptions.days = await <number> options.options.getInteger('days');
48     }
49     }
50     else {
51     const user2 = await getUser(client, (msg as Message), options);
52    
53     if (!user2) {
54     await msg.reply({
55     embeds: [
56     new MessageEmbed()
57     .setColor('#f14a60')
58     .setDescription(`Invalid user given.`)
59     ]
60     });
61    
62     return;
63     }
64    
65     user = user2;
66    
67     const index = await options.args.indexOf('-d');
68    
69     if (options.args[1]) {
70     const args = [...options.args];
71     args.shift();
72    
73     if (index !== -1) {
74     args.splice(index - 1, 2)
75     }
76    
77     banOptions.reason = await args.join(' ');
78     }
79    
80     if (index !== -1) {
81     const days = await options.args[index + 1];
82    
83     if (days === undefined) {
84     await msg.reply({
85     embeds: [
86     new MessageEmbed()
87     .setColor('#f14a60')
88     .setDescription(`Option \`-d\` (days) requires an argument.`)
89     ]
90     });
91    
92     return;
93     }
94    
95     if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
96     await msg.reply({
97     embeds: [
98     new MessageEmbed()
99     .setColor('#f14a60')
100     .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
101     ]
102     });
103    
104     return;
105     }
106    
107     banOptions.days = await parseInt(days);
108     }
109     }
110    
111     let reply = await msg.reply({
112     embeds: [
113     new MessageEmbed({
114     author: {
115     name: user.tag,
116     iconURL: user.displayAvatarURL()
117     },
118     description: `${await fetchEmojiStr('loading')} Softban is in progress...`,
119     color: 'GOLD'
120     })
121     ]
122     });
123    
124     if (msg instanceof CommandInteraction)
125     reply = <Message> await msg.fetchReply();
126    
127     try {
128 rakin 153 try {
129     const member = await msg.guild!.members.fetch(user.id);
130    
131 rakin 194 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to softban this user."))) {
132     return;
133     }
134    
135 rakin 153 if (member && shouldNotModerate(client, member)) {
136     await msg.reply({
137     embeds: [
138     new MessageEmbed()
139     .setColor('#f14a60')
140     .setDescription('This user cannot be softbanned.')
141     ]
142     });
143    
144     return;
145     }
146     }
147     catch (e) {
148     console.log(e);
149     }
150    
151 rakin 106 await msg.guild?.bans.create(user, banOptions);
152     await new Promise(r => setTimeout(r, 1600));
153     await msg.guild?.bans.remove(user);
154    
155     const punishment = await Punishment.create({
156     type: PunishmentType.SOFTBAN,
157     user_id: user.id,
158     guild_id: msg.guild!.id,
159     mod_id: msg.member!.user.id,
160     mod_tag: (msg.member!.user as User).tag,
161     reason: banOptions.reason ?? undefined,
162     meta: {
163     days: banOptions.days
164 rakin 336 },
165     createdAt: new Date()
166 rakin 106 });
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 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26