/[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 336 - (hide annotations)
Mon Jul 29 17:29:36 2024 UTC (8 months, 3 weeks ago) by rakin
File MIME type: application/typescript
File size: 4949 byte(s)
refactor(moderation): use mongodb
1 rakin 206 import { BanOptions, CommandInteraction, ContextMenuInteraction, GuildMember, Interaction, Message, Permissions, 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 206
18     permissions = [Permissions.FLAGS.KICK_MEMBERS];
19 rakin 51
20     constructor() {
21 rakin 125 super('kick', 'moderation', ['Kick']);
22 rakin 51 }
23    
24 rakin 125 async run(client: DiscordClient, msg: Message | CommandInteraction | ContextMenuInteraction, options: CommandOptions | InteractionOptions) {
25 rakin 51 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
26     await msg.reply({
27     embeds: [
28     new MessageEmbed()
29     .setColor('#f14a60')
30     .setDescription(`This command requires at least one argument.`)
31     ]
32     });
33    
34     return;
35     }
36    
37     let user: GuildMember;
38     let reason: string | undefined;
39    
40     if (options.isInteraction) {
41 rakin 125 user = await <GuildMember> (msg instanceof ContextMenuInteraction ? options.options.getMember('user') : options.options.getMember('member'));
42 rakin 51
43     if (!user) {
44     await msg.reply({
45     embeds: [
46     new MessageEmbed()
47     .setColor('#f14a60')
48     .setDescription("Invalid user given.")
49     ]
50     });
51    
52     return;
53     }
54    
55     if (options.options.getString('reason')) {
56     reason = await <string> options.options.getString('reason');
57     }
58     }
59     else {
60     try {
61     const user2 = await getMember((msg as Message), options);
62    
63     if (!user2) {
64     throw new Error('Invalid user');
65     }
66    
67     user = user2;
68     }
69     catch (e) {
70     await msg.reply({
71     embeds: [
72     new MessageEmbed()
73     .setColor('#f14a60')
74     .setDescription(`Invalid user given.`)
75     ]
76     });
77    
78     return;
79     }
80    
81     console.log(user);
82    
83     if (options.args[1]) {
84     const args = [...options.args];
85     args.shift();
86     reason = await args.join(' ');
87     }
88     }
89    
90     try {
91 rakin 195 if (!(await hasPermission(client, user, msg, null, "You don't have permission to kick this user."))) {
92 rakin 194 return;
93     }
94    
95 rakin 153 if (!user.kickable || shouldNotModerate(client, user))
96 rakin 51 throw new Error('User not kickable');
97    
98     await user.kick(reason);
99 rakin 86
100     await Punishment.create({
101     type: PunishmentType.KICK,
102     user_id: user.id,
103     guild_id: msg.guild!.id,
104     mod_id: msg.member!.user.id,
105     mod_tag: (msg.member!.user as User).tag,
106 rakin 336 reason,
107     createdAt: new Date()
108 rakin 86 });
109    
110 rakin 153 // await History.create(user.id, msg.guild!, 'kick', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
111 rakin 51 }
112     catch (e) {
113     await msg.reply({
114     embeds: [
115     new MessageEmbed()
116     .setColor('#f14a60')
117     .setDescription("Failed to kick this user. Maybe missing permisions or I'm not allowed to kick this user?")
118     ]
119     });
120    
121     return;
122     }
123    
124     await msg.reply({
125     embeds: [
126     new MessageEmbed()
127     .setAuthor({
128     name: user.user.tag,
129     iconURL: user.user.displayAvatarURL(),
130     })
131     .setDescription(user.user.tag + " has been kicked from this server.")
132     .addFields([
133     {
134     name: "Kicked by",
135     value: (msg.member!.user as User).tag
136     },
137     {
138     name: "Reason",
139     value: reason === undefined ? "*No reason provided*" : reason
140     }
141     ])
142     ]
143     });
144     }
145 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26