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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (show annotations)
Mon Jul 29 17:28:48 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5971 byte(s)
Add Non-moderable role (#34)

* feat(utils): add shouldNotModerate() function

* fix(mod-cmds): no non-moderable role checking
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 { shouldNotModerate } from '../../utils/util';
12
13 export default class BanCommand extends BaseCommand {
14 supportsInteractions: boolean = true;
15 supportsContextMenu: boolean = true;
16
17 constructor() {
18 super('ban', 'moderation', ['Ban']);
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
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 try {
110 const member = await msg.guild?.members.fetch(user.id);
111
112 if (member && shouldNotModerate(client, member)) {
113 await msg.reply({
114 embeds: [
115 new MessageEmbed()
116 .setColor('#f14a60')
117 .setDescription('Cannot ban this user: Operation not permitted')
118 ]
119 });
120
121 return;
122 }
123 }
124 catch (e) {
125 console.log(e);
126 return;
127 }
128
129 try {
130 await msg.guild?.bans.create(user, banOptions);
131
132 await Punishment.create({
133 type: PunishmentType.BAN,
134 user_id: user.id,
135 guild_id: msg.guild!.id,
136 mod_id: msg.member!.user.id,
137 mod_tag: (msg.member!.user as User).tag,
138 reason: banOptions.reason ?? undefined
139 });
140
141 await History.create(user.id, msg.guild!, 'ban', msg.member!.user.id, typeof banOptions.reason === 'undefined' ? null : banOptions.reason, async (data: any) => undefined);
142 }
143 catch (e) {
144 await msg.reply({
145 embeds: [
146 new MessageEmbed()
147 .setColor('#f14a60')
148 .setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?")
149 ]
150 });
151
152 return;
153 }
154
155 await msg.reply({
156 embeds: [
157 new MessageEmbed()
158 .setAuthor({
159 name: user.tag,
160 iconURL: user.displayAvatarURL(),
161 })
162 .setDescription(user.tag + " has been banned from this server.")
163 .addFields([
164 {
165 name: "Banned by",
166 value: (msg.member!.user as User).tag
167 },
168 {
169 name: "Reason",
170 value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason
171 },
172 {
173 name: "Days of message deletion",
174 value: banOptions.days === undefined ? "*No message will be deleted*" : (banOptions.days + '')
175 }
176 ])
177 ]
178 });
179 }
180 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26