/[sudobot]/branches/2.x/src/commands/moderation/BanCommand.ts
ViewVC logotype

Contents of /branches/2.x/src/commands/moderation/BanCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6174 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 import { Permissions, 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 { shouldNotModerate, hasPermission } from '../../utils/util';
12
13 export default class BanCommand extends BaseCommand {
14 supportsInteractions: boolean = true;
15 supportsContextMenu: boolean = true;
16 permissions = [Permissions.FLAGS.BAN_MEMBERS];
17
18 constructor() {
19 super('ban', 'moderation', ['Ban']);
20 }
21
22 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
23 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
24 await msg.reply({
25 embeds: [
26 new MessageEmbed()
27 .setColor('#f14a60')
28 .setDescription(`This command requires at least one argument.`)
29 ]
30 });
31
32 return;
33 }
34
35 let user: User;
36 let banOptions: BanOptions = {};
37
38 if (options.isInteraction) {
39 user = await <User> options.options.getUser('user');
40
41 if (options.options.getString('reason')) {
42 banOptions.reason = await <string> options.options.getString('reason');
43 }
44
45 if (options.options.getInteger('days')) {
46 banOptions.days = await <number> options.options.getInteger('days');
47 }
48 }
49 else {
50 const user2 = await getUser(client, (msg as Message), options);
51
52 if (!user2) {
53 await msg.reply({
54 embeds: [
55 new MessageEmbed()
56 .setColor('#f14a60')
57 .setDescription(`Invalid user given.`)
58 ]
59 });
60
61 return;
62 }
63
64 user = user2;
65
66 const index = await options.args.indexOf('-d');
67
68 if (options.args[1]) {
69 const args = [...options.args];
70 args.shift();
71
72 if (index !== -1) {
73 args.splice(index - 1, 2)
74 }
75
76 banOptions.reason = await args.join(' ');
77 }
78
79 if (index !== -1) {
80 const days = await options.args[index + 1];
81
82 if (days === undefined) {
83 await msg.reply({
84 embeds: [
85 new MessageEmbed()
86 .setColor('#f14a60')
87 .setDescription(`Option \`-d\` (days) requires an argument.`)
88 ]
89 });
90
91 return;
92 }
93
94 if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
95 await msg.reply({
96 embeds: [
97 new MessageEmbed()
98 .setColor('#f14a60')
99 .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
100 ]
101 });
102
103 return;
104 }
105
106 banOptions.days = await parseInt(days);
107 }
108 }
109
110 try {
111 const member = await msg.guild?.members.fetch(user.id);
112
113 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to ban this user."))) {
114 return;
115 }
116
117 if (member && shouldNotModerate(client, member)) {
118 await msg.reply({
119 embeds: [
120 new MessageEmbed()
121 .setColor('#f14a60')
122 .setDescription('Cannot ban this user: Operation not permitted')
123 ]
124 });
125
126 return;
127 }
128 }
129 catch (e) {
130 console.log(e);
131 }
132
133 try {
134 await msg.guild?.bans.create(user, banOptions);
135
136 await Punishment.create({
137 type: PunishmentType.BAN,
138 user_id: user.id,
139 guild_id: msg.guild!.id,
140 mod_id: msg.member!.user.id,
141 mod_tag: (msg.member!.user as User).tag,
142 reason: banOptions.reason ?? undefined
143 });
144
145 await History.create(user.id, msg.guild!, 'ban', msg.member!.user.id, typeof banOptions.reason === 'undefined' ? null : banOptions.reason, async (data: any) => undefined);
146 }
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 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26