/[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 341 - (hide annotations)
Mon Jul 29 17:29:38 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 4861 byte(s)
refactor: removing using the old history manager
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 getMember from '../../utils/getMember';
8 rakin 86 import Punishment from '../../models/Punishment';
9     import PunishmentType from '../../types/PunishmentType';
10 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
11 rakin 51
12     export default class KickCommand extends BaseCommand {
13     supportsInteractions: boolean = true;
14 rakin 125 supportsContextMenu: boolean = true;
15 rakin 206
16     permissions = [Permissions.FLAGS.KICK_MEMBERS];
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 195 if (!(await hasPermission(client, user, msg, null, "You don't have permission to kick this user."))) {
90 rakin 194 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 rakin 336 reason,
105     createdAt: new Date()
106 rakin 86 });
107    
108 rakin 153 // await History.create(user.id, msg.guild!, 'kick', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
109 rakin 51 }
110     catch (e) {
111     await msg.reply({
112     embeds: [
113     new MessageEmbed()
114     .setColor('#f14a60')
115     .setDescription("Failed to kick this user. Maybe missing permisions or I'm not allowed to kick this user?")
116     ]
117     });
118    
119     return;
120     }
121    
122     await msg.reply({
123     embeds: [
124     new MessageEmbed()
125     .setAuthor({
126     name: user.user.tag,
127     iconURL: user.user.displayAvatarURL(),
128     })
129     .setDescription(user.user.tag + " has been kicked from this server.")
130     .addFields([
131     {
132     name: "Kicked by",
133     value: (msg.member!.user as User).tag
134     },
135     {
136     name: "Reason",
137     value: reason === undefined ? "*No reason provided*" : reason
138     }
139     ])
140     ]
141     });
142     }
143 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26