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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 194 - (hide annotations)
Mon Jul 29 17:28:58 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6110 byte(s)
feat: add proper permission checking and error messages
1 rakin 51 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 rakin 86 import Punishment from '../../models/Punishment';
10     import PunishmentType from '../../types/PunishmentType';
11 rakin 194 import { shouldNotModerate, hasPermission } from '../../utils/util';
12 rakin 51
13     export default class BanCommand extends BaseCommand {
14     supportsInteractions: boolean = true;
15 rakin 125 supportsContextMenu: boolean = true;
16 rakin 51
17     constructor() {
18 rakin 125 super('ban', 'moderation', ['Ban']);
19 rakin 51 }
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    
37     if (options.isInteraction) {
38     user = await <User> options.options.getUser('user');
39    
40     if (options.options.getString('reason')) {
41     banOptions.reason = await <string> options.options.getString('reason');
42     }
43    
44     if (options.options.getInteger('days')) {
45     banOptions.days = await <number> options.options.getInteger('days');
46     }
47     }
48     else {
49     const user2 = await getUser(client, (msg as Message), options);
50    
51     if (!user2) {
52     await msg.reply({
53     embeds: [
54     new MessageEmbed()
55     .setColor('#f14a60')
56     .setDescription(`Invalid user given.`)
57     ]
58     });
59    
60     return;
61     }
62    
63     user = user2;
64    
65     const index = await options.args.indexOf('-d');
66    
67     if (options.args[1]) {
68     const args = [...options.args];
69     args.shift();
70    
71     if (index !== -1) {
72     args.splice(index - 1, 2)
73     }
74    
75     banOptions.reason = await args.join(' ');
76     }
77    
78     if (index !== -1) {
79     const days = await options.args[index + 1];
80    
81     if (days === undefined) {
82     await msg.reply({
83     embeds: [
84     new MessageEmbed()
85     .setColor('#f14a60')
86     .setDescription(`Option \`-d\` (days) requires an argument.`)
87     ]
88     });
89    
90     return;
91     }
92    
93     if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
94     await msg.reply({
95     embeds: [
96     new MessageEmbed()
97     .setColor('#f14a60')
98     .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
99     ]
100     });
101    
102     return;
103     }
104    
105     banOptions.days = await parseInt(days);
106     }
107     }
108    
109 rakin 153 try {
110     const member = await msg.guild?.members.fetch(user.id);
111    
112 rakin 194 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to ban this user."))) {
113     return;
114     }
115    
116 rakin 153 if (member && shouldNotModerate(client, member)) {
117     await msg.reply({
118     embeds: [
119     new MessageEmbed()
120     .setColor('#f14a60')
121     .setDescription('Cannot ban this user: Operation not permitted')
122     ]
123     });
124    
125     return;
126     }
127     }
128     catch (e) {
129     console.log(e);
130     }
131    
132 rakin 51 try {
133     await msg.guild?.bans.create(user, banOptions);
134 rakin 86
135     await Punishment.create({
136     type: PunishmentType.BAN,
137     user_id: user.id,
138     guild_id: msg.guild!.id,
139     mod_id: msg.member!.user.id,
140     mod_tag: (msg.member!.user as User).tag,
141     reason: banOptions.reason ?? undefined
142     });
143    
144 rakin 51 await History.create(user.id, msg.guild!, 'ban', msg.member!.user.id, typeof banOptions.reason === 'undefined' ? null : banOptions.reason, async (data: any) => undefined);
145     }
146     catch (e) {
147     await msg.reply({
148     embeds: [
149     new MessageEmbed()
150     .setColor('#f14a60')
151     .setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?")
152     ]
153     });
154    
155     return;
156     }
157    
158     await msg.reply({
159     embeds: [
160     new MessageEmbed()
161     .setAuthor({
162     name: user.tag,
163     iconURL: user.displayAvatarURL(),
164     })
165     .setDescription(user.tag + " has been banned from this server.")
166     .addFields([
167     {
168     name: "Banned by",
169     value: (msg.member!.user as User).tag
170     },
171     {
172     name: "Reason",
173     value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason
174     },
175     {
176     name: "Days of message deletion",
177     value: banOptions.days === undefined ? "*No message will be deleted*" : (banOptions.days + '')
178     }
179     ])
180     ]
181     });
182     }
183 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26