1 |
/** |
2 |
* This file is part of SudoBot. |
3 |
* |
4 |
* Copyright (C) 2021-2023 OSN Developers. |
5 |
* |
6 |
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
* under the terms of the GNU Affero General Public License as published by |
8 |
* the Free Software Foundation, either version 3 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* SudoBot is distributed in the hope that it will be useful, but |
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU Affero General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Affero General Public License |
17 |
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
*/ |
19 |
|
20 |
import { ChatInputCommandInteraction, GuildMember, PermissionsBitField, SlashCommandBuilder, User, escapeMarkdown } from "discord.js"; |
21 |
import Command, { ArgumentType, BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command"; |
22 |
import { createModerationEmbed } from "../../utils/utils"; |
23 |
|
24 |
export default class KickCommand extends Command { |
25 |
public readonly name = "kick"; |
26 |
public readonly validationRules: ValidationRule[] = [ |
27 |
{ |
28 |
types: [ArgumentType.GuildMember], |
29 |
entityNotNull: true, |
30 |
requiredErrorMessage: "You must specify a member to kick!", |
31 |
typeErrorMessage: "You have specified an invalid user mention or ID.", |
32 |
entityNotNullErrorMessage: "The given member does not exist in the server!", |
33 |
name: "member" |
34 |
}, |
35 |
{ |
36 |
types: [ArgumentType.StringRest], |
37 |
optional: true, |
38 |
typeErrorMessage: "You have specified an invalid kick reason.", |
39 |
lengthMax: 3999, |
40 |
name: "reason" |
41 |
} |
42 |
]; |
43 |
public readonly permissions = [PermissionsBitField.Flags.KickMembers]; |
44 |
|
45 |
public readonly description = "Kicks a server member."; |
46 |
public readonly detailedDescription = "This command kicks a server member."; |
47 |
public readonly argumentSyntaxes = ["<UserID|UserMention> [reason]"]; |
48 |
|
49 |
public readonly botRequiredPermissions = [PermissionsBitField.Flags.KickMembers]; |
50 |
|
51 |
public readonly slashCommandBuilder = new SlashCommandBuilder() |
52 |
.addUserOption(option => option.setName("member").setDescription("The member").setRequired(true)) |
53 |
.addStringOption(option => option.setName("reason").setDescription("The reason for kicking this user")) |
54 |
.addBooleanOption(option => |
55 |
option.setName("silent").setDescription("Specify if the system should not notify the user about this action. Defaults to false") |
56 |
); |
57 |
|
58 |
async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> { |
59 |
const member: GuildMember | null = context.isLegacy ? context.parsedNamedArgs.member : context.options.getMember("member"); |
60 |
|
61 |
if (!member) { |
62 |
return { |
63 |
__reply: true, |
64 |
ephemeral: true, |
65 |
content: `Invalid member given. Probably that user isn't a member of this server?` |
66 |
}; |
67 |
} |
68 |
|
69 |
const reason: string | undefined = !context.isLegacy |
70 |
? context.options.getString("reason") ?? undefined |
71 |
: context.parsedNamedArgs.reason ?? undefined; |
72 |
|
73 |
if (message instanceof ChatInputCommandInteraction) await message.deferReply(); |
74 |
|
75 |
if (!this.client.permissionManager.shouldModerate(member, message.member! as GuildMember)) { |
76 |
await this.error(message, "You don't have permission to kick this user!"); |
77 |
return; |
78 |
} |
79 |
|
80 |
const id = await this.client.infractionManager.createMemberKick(member, { |
81 |
guild: message.guild!, |
82 |
moderator: message.member!.user as User, |
83 |
reason, |
84 |
notifyUser: context.isLegacy ? true : !context.options.getBoolean("silent"), |
85 |
sendLog: true |
86 |
}); |
87 |
|
88 |
if (!id) { |
89 |
await this.error(message); |
90 |
return; |
91 |
} |
92 |
|
93 |
await this.deferredReply(message, { |
94 |
embeds: [ |
95 |
await createModerationEmbed({ |
96 |
user: member.user, |
97 |
actionDoneName: "kicked", |
98 |
id, |
99 |
description: `**${escapeMarkdown(member.user.tag)}** has been kicked from this server.`, |
100 |
reason |
101 |
}) |
102 |
] |
103 |
}); |
104 |
} |
105 |
} |