/[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 344 - (hide annotations)
Mon Jul 29 17:29:40 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 6145 byte(s)
chore: eslint autofix
1 rakin 344 import { Permissions, BanOptions, CommandInteraction, Message, User } from 'discord.js';
2 rakin 51 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 rakin 86 import Punishment from '../../models/Punishment';
9     import PunishmentType from '../../types/PunishmentType';
10 rakin 194 import { shouldNotModerate, hasPermission } from '../../utils/util';
11 rakin 51
12     export default class BanCommand extends BaseCommand {
13     supportsInteractions: boolean = true;
14 rakin 125 supportsContextMenu: boolean = true;
15 rakin 197 permissions = [Permissions.FLAGS.BAN_MEMBERS];
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 rakin 336 reason: banOptions.reason ?? undefined,
142     createdAt: new Date()
143 rakin 86 });
144    
145 rakin 341 // await History.create(user.id, msg.guild!, 'ban', msg.member!.user.id, typeof banOptions.reason === 'undefined' ? null : banOptions.reason, async (data: any) => undefined);
146 rakin 51 }
147     catch (e) {
148     await msg.reply({
149     embeds: [
150     new MessageEmbed()
151     .setColor('#f14a60')
152     .setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?")
153     ]
154     });
155    
156     return;
157     }
158    
159     await msg.reply({
160     embeds: [
161     new MessageEmbed()
162     .setAuthor({
163     name: user.tag,
164     iconURL: user.displayAvatarURL(),
165     })
166     .setDescription(user.tag + " has been banned from this server.")
167     .addFields([
168     {
169     name: "Banned by",
170     value: (msg.member!.user as User).tag
171     },
172     {
173     name: "Reason",
174     value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason
175     },
176     {
177     name: "Days of message deletion",
178     value: banOptions.days === undefined ? "*No message will be deleted*" : (banOptions.days + '')
179     }
180     ])
181     ]
182     });
183     }
184 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26