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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 194 - (hide annotations)
Mon Jul 29 17:28:58 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 4843 byte(s)
feat: add proper permission checking and error messages
1 rakin 125 import { BanOptions, CommandInteraction, ContextMenuInteraction, GuildMember, Interaction, 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     import getMember from '../../utils/getMember';
9     import History from '../../automod/History';
10 rakin 86 import Punishment from '../../models/Punishment';
11     import PunishmentType from '../../types/PunishmentType';
12 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
13 rakin 51
14     export default class KickCommand extends BaseCommand {
15     supportsInteractions: boolean = true;
16 rakin 125 supportsContextMenu: boolean = true;
17 rakin 51
18     constructor() {
19 rakin 125 super('kick', 'moderation', ['Kick']);
20 rakin 51 }
21    
22 rakin 125 async run(client: DiscordClient, msg: Message | CommandInteraction | ContextMenuInteraction, options: CommandOptions | InteractionOptions) {
23 rakin 51 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: GuildMember;
36     let reason: string | undefined;
37    
38     if (options.isInteraction) {
39 rakin 125 user = await <GuildMember> (msg instanceof ContextMenuInteraction ? options.options.getMember('user') : options.options.getMember('member'));
40 rakin 51
41     if (!user) {
42     await msg.reply({
43     embeds: [
44     new MessageEmbed()
45     .setColor('#f14a60')
46     .setDescription("Invalid user given.")
47     ]
48     });
49    
50     return;
51     }
52    
53     if (options.options.getString('reason')) {
54     reason = await <string> options.options.getString('reason');
55     }
56     }
57     else {
58     try {
59     const user2 = await getMember((msg as Message), options);
60    
61     if (!user2) {
62     throw new Error('Invalid user');
63     }
64    
65     user = user2;
66     }
67     catch (e) {
68     await msg.reply({
69     embeds: [
70     new MessageEmbed()
71     .setColor('#f14a60')
72     .setDescription(`Invalid user given.`)
73     ]
74     });
75    
76     return;
77     }
78    
79     console.log(user);
80    
81     if (options.args[1]) {
82     const args = [...options.args];
83     args.shift();
84     reason = await args.join(' ');
85     }
86     }
87    
88     try {
89 rakin 194 if (!(await hasPermissions(client, member, msg, null, "You don't have permission to kick this user."))) {
90     return;
91     }
92    
93 rakin 153 if (!user.kickable || shouldNotModerate(client, user))
94 rakin 51 throw new Error('User not kickable');
95    
96     await user.kick(reason);
97 rakin 86
98     await Punishment.create({
99     type: PunishmentType.KICK,
100     user_id: user.id,
101     guild_id: msg.guild!.id,
102     mod_id: msg.member!.user.id,
103     mod_tag: (msg.member!.user as User).tag,
104     reason
105     });
106    
107 rakin 153 // await History.create(user.id, msg.guild!, 'kick', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
108 rakin 51 }
109     catch (e) {
110     await msg.reply({
111     embeds: [
112     new MessageEmbed()
113     .setColor('#f14a60')
114     .setDescription("Failed to kick this user. Maybe missing permisions or I'm not allowed to kick this user?")
115     ]
116     });
117    
118     return;
119     }
120    
121     await msg.reply({
122     embeds: [
123     new MessageEmbed()
124     .setAuthor({
125     name: user.user.tag,
126     iconURL: user.user.displayAvatarURL(),
127     })
128     .setDescription(user.user.tag + " has been kicked from this server.")
129     .addFields([
130     {
131     name: "Kicked by",
132     value: (msg.member!.user as User).tag
133     },
134     {
135     name: "Reason",
136     value: reason === undefined ? "*No reason provided*" : reason
137     }
138     ])
139     ]
140     });
141     }
142 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26