/[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 194 - (show annotations)
Mon Jul 29 17:28:58 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6829 byte(s)
feat: add proper permission checking and error messages
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 { hasPermission, shouldNotModerate } from '../../utils/util';
13
14 export default class SoftBanCommand extends BaseCommand {
15 supportsInteractions: boolean = true;
16
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 try {
129 const member = await msg.guild!.members.fetch(user.id);
130
131 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to softban this user."))) {
132 return;
133 }
134
135 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 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 }
165 });
166
167 await client.logger.logSoftBan(banOptions, msg.guild!, user, punishment);
168
169 await reply!.edit({
170 embeds: [
171 new MessageEmbed({
172 author: {
173 name: user.tag,
174 iconURL: user.displayAvatarURL()
175 },
176 description: `${await fetchEmojiStr('check')} Softbanned user ${user.tag}`,
177 fields: [
178 {
179 name: 'Softbanned by',
180 value: (<User> msg.member?.user).tag
181 },
182 {
183 name: 'Reason',
184 value: banOptions.reason ?? '*No reason provided*'
185 }
186 ]
187 })
188 ]
189 });
190 }
191 catch (e) {
192 await reply!.edit({
193 embeds: [
194 new MessageEmbed()
195 .setColor('#f14a60')
196 .setDescription("Failed to softban this user. Maybe missing permisions or I'm not allowed to ban this user?")
197 ]
198 });
199
200 return;
201 }
202 }
203 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26