/[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 186 - (show annotations)
Mon Jul 29 17:28:56 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5960 byte(s)
fix: mention reading strategy (#41)

* fix(commands): ban related commands not working when banning users outside of the server

* fix(utils): reading users/members in the wrong way
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 }
127
128 try {
129 await msg.guild?.bans.create(user, banOptions);
130
131 await Punishment.create({
132 type: PunishmentType.BAN,
133 user_id: user.id,
134 guild_id: msg.guild!.id,
135 mod_id: msg.member!.user.id,
136 mod_tag: (msg.member!.user as User).tag,
137 reason: banOptions.reason ?? undefined
138 });
139
140 await History.create(user.id, msg.guild!, 'ban', msg.member!.user.id, typeof banOptions.reason === 'undefined' ? null : banOptions.reason, async (data: any) => undefined);
141 }
142 catch (e) {
143 await msg.reply({
144 embeds: [
145 new MessageEmbed()
146 .setColor('#f14a60')
147 .setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?")
148 ]
149 });
150
151 return;
152 }
153
154 await msg.reply({
155 embeds: [
156 new MessageEmbed()
157 .setAuthor({
158 name: user.tag,
159 iconURL: user.displayAvatarURL(),
160 })
161 .setDescription(user.tag + " has been banned from this server.")
162 .addFields([
163 {
164 name: "Banned by",
165 value: (msg.member!.user as User).tag
166 },
167 {
168 name: "Reason",
169 value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason
170 },
171 {
172 name: "Days of message deletion",
173 value: banOptions.days === undefined ? "*No message will be deleted*" : (banOptions.days + '')
174 }
175 ])
176 ]
177 });
178 }
179 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26