/[sudobot]/branches/2.x/src/commands/moderation/KickCommand.ts
ViewVC logotype

Contents of /branches/2.x/src/commands/moderation/KickCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 4910 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 import { BanOptions, CommandInteraction, ContextMenuInteraction, GuildMember, Interaction, Message, Permissions, 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 getMember from '../../utils/getMember';
9 import History from '../../automod/History';
10 import Punishment from '../../models/Punishment';
11 import PunishmentType from '../../types/PunishmentType';
12 import { hasPermission, shouldNotModerate } from '../../utils/util';
13
14 export default class KickCommand extends BaseCommand {
15 supportsInteractions: boolean = true;
16 supportsContextMenu: boolean = true;
17
18 permissions = [Permissions.FLAGS.KICK_MEMBERS];
19
20 constructor() {
21 super('kick', 'moderation', ['Kick']);
22 }
23
24 async run(client: DiscordClient, msg: Message | CommandInteraction | ContextMenuInteraction, options: CommandOptions | InteractionOptions) {
25 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 user = await <GuildMember> (msg instanceof ContextMenuInteraction ? options.options.getMember('user') : options.options.getMember('member'));
42
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 if (!(await hasPermission(client, user, msg, null, "You don't have permission to kick this user."))) {
92 return;
93 }
94
95 if (!user.kickable || shouldNotModerate(client, user))
96 throw new Error('User not kickable');
97
98 await user.kick(reason);
99
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 reason
107 });
108
109 // await History.create(user.id, msg.guild!, 'kick', msg.member!.user.id, typeof reason === 'undefined' ? null : reason);
110 }
111 catch (e) {
112 await msg.reply({
113 embeds: [
114 new MessageEmbed()
115 .setColor('#f14a60')
116 .setDescription("Failed to kick this user. Maybe missing permisions or I'm not allowed to kick this user?")
117 ]
118 });
119
120 return;
121 }
122
123 await msg.reply({
124 embeds: [
125 new MessageEmbed()
126 .setAuthor({
127 name: user.user.tag,
128 iconURL: user.user.displayAvatarURL(),
129 })
130 .setDescription(user.user.tag + " has been kicked from this server.")
131 .addFields([
132 {
133 name: "Kicked by",
134 value: (msg.member!.user as User).tag
135 },
136 {
137 name: "Reason",
138 value: reason === undefined ? "*No reason provided*" : reason
139 }
140 ])
141 ]
142 });
143 }
144 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26