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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (hide annotations)
Mon Jul 29 17:28:56 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 6672 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 rakin 106 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 { fetchEmojiStr } from '../../utils/Emoji';
12 rakin 153 import { shouldNotModerate } from '../../utils/util';
13 rakin 106
14     export default class SoftBanCommand extends BaseCommand {
15     supportsInteractions: boolean = true;
16    
17     constructor() {
18     super('softban', 'moderation', []);
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     days: 7
37     };
38    
39     if (options.isInteraction) {
40     user = await <User> options.options.getUser('user');
41    
42     if (options.options.getString('reason')) {
43     banOptions.reason = await <string> options.options.getString('reason');
44     }
45    
46     if (options.options.getInteger('days')) {
47     banOptions.days = await <number> options.options.getInteger('days');
48     }
49     }
50     else {
51     const user2 = await getUser(client, (msg as Message), options);
52    
53     if (!user2) {
54     await msg.reply({
55     embeds: [
56     new MessageEmbed()
57     .setColor('#f14a60')
58     .setDescription(`Invalid user given.`)
59     ]
60     });
61    
62     return;
63     }
64    
65     user = user2;
66    
67     const index = await options.args.indexOf('-d');
68    
69     if (options.args[1]) {
70     const args = [...options.args];
71     args.shift();
72    
73     if (index !== -1) {
74     args.splice(index - 1, 2)
75     }
76    
77     banOptions.reason = await args.join(' ');
78     }
79    
80     if (index !== -1) {
81     const days = await options.args[index + 1];
82    
83     if (days === undefined) {
84     await msg.reply({
85     embeds: [
86     new MessageEmbed()
87     .setColor('#f14a60')
88     .setDescription(`Option \`-d\` (days) requires an argument.`)
89     ]
90     });
91    
92     return;
93     }
94    
95     if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
96     await msg.reply({
97     embeds: [
98     new MessageEmbed()
99     .setColor('#f14a60')
100     .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`)
101     ]
102     });
103    
104     return;
105     }
106    
107     banOptions.days = await parseInt(days);
108     }
109     }
110    
111     let reply = await msg.reply({
112     embeds: [
113     new MessageEmbed({
114     author: {
115     name: user.tag,
116     iconURL: user.displayAvatarURL()
117     },
118     description: `${await fetchEmojiStr('loading')} Softban is in progress...`,
119     color: 'GOLD'
120     })
121     ]
122     });
123    
124     if (msg instanceof CommandInteraction)
125     reply = <Message> await msg.fetchReply();
126    
127     try {
128 rakin 153 try {
129     const member = await msg.guild!.members.fetch(user.id);
130    
131     if (member && shouldNotModerate(client, member)) {
132     await msg.reply({
133     embeds: [
134     new MessageEmbed()
135     .setColor('#f14a60')
136     .setDescription('This user cannot be softbanned.')
137     ]
138     });
139    
140     return;
141     }
142     }
143     catch (e) {
144     console.log(e);
145     }
146    
147 rakin 106 await msg.guild?.bans.create(user, banOptions);
148     await new Promise(r => setTimeout(r, 1600));
149     await msg.guild?.bans.remove(user);
150    
151     const punishment = await Punishment.create({
152     type: PunishmentType.SOFTBAN,
153     user_id: user.id,
154     guild_id: msg.guild!.id,
155     mod_id: msg.member!.user.id,
156     mod_tag: (msg.member!.user as User).tag,
157     reason: banOptions.reason ?? undefined,
158     meta: {
159     days: banOptions.days
160     }
161     });
162    
163     await client.logger.logSoftBan(banOptions, msg.guild!, user, punishment);
164    
165     await reply!.edit({
166     embeds: [
167     new MessageEmbed({
168     author: {
169     name: user.tag,
170     iconURL: user.displayAvatarURL()
171     },
172     description: `${await fetchEmojiStr('check')} Softbanned user ${user.tag}`,
173     fields: [
174     {
175     name: 'Softbanned by',
176     value: (<User> msg.member?.user).tag
177     },
178     {
179     name: 'Reason',
180     value: banOptions.reason ?? '*No reason provided*'
181     }
182     ]
183     })
184     ]
185     });
186     }
187     catch (e) {
188     await reply!.edit({
189     embeds: [
190     new MessageEmbed()
191     .setColor('#f14a60')
192     .setDescription("Failed to softban this user. Maybe missing permisions or I'm not allowed to ban this user?")
193     ]
194     });
195    
196     return;
197     }
198     }
199 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26