/[sudobot]/branches/8.x/src/commands/moderation/UnbanCommand.ts
ViewVC logotype

Contents of /branches/8.x/src/commands/moderation/UnbanCommand.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: 4801 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
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 {
21 GuildMember,
22 PermissionsBitField,
23 SlashCommandBuilder,
24 User,
25 escapeMarkdown
26 } from "discord.js";
27 import Command, {
28 ArgumentType,
29 BasicCommandContext,
30 CommandMessage,
31 CommandReturn,
32 ValidationRule
33 } from "../../core/Command";
34 import { logError } from "../../utils/Logger";
35 import { createModerationEmbed } from "../../utils/utils";
36
37 export default class UnbanCommand extends Command {
38 public readonly name = "unban";
39 public readonly validationRules: ValidationRule[] = [
40 {
41 types: [ArgumentType.User],
42 entity: true,
43 errors: {
44 required: "You must specify a user to unban!",
45 "type:invalid": "You have specified an invalid user mention or ID.",
46 "entity:null": "The given user does not exist!"
47 },
48 name: "user"
49 },
50 {
51 types: [ArgumentType.StringRest],
52 optional: true,
53 errors: {
54 "type:invalid": "You have specified an invalid unban reason."
55 },
56 string: {
57 maxLength: 3999
58 },
59 name: "reason"
60 }
61 ];
62 public readonly permissions = [PermissionsBitField.Flags.BanMembers];
63
64 public readonly description = "Unbans a user.";
65 public readonly detailedDescription = "This command unbans the given user.";
66 public readonly argumentSyntaxes = ["<UserID|UserMention> [Reason]"];
67
68 public readonly botRequiredPermissions = [PermissionsBitField.Flags.BanMembers];
69
70 public readonly slashCommandBuilder = new SlashCommandBuilder()
71 .addUserOption(option =>
72 option.setName("user").setDescription("The user").setRequired(true)
73 )
74 .addStringOption(option =>
75 option.setName("reason").setDescription("The reason for unbanning this user")
76 );
77
78 async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
79 await this.deferIfInteraction(message);
80
81 const user: User = context.isLegacy
82 ? context.parsedNamedArgs.user
83 : context.options.getUser("user", true);
84 const reason: string | undefined =
85 (!context.isLegacy
86 ? context.options.getString("reason")
87 : context.parsedNamedArgs.reason) ?? undefined;
88
89 try {
90 const member =
91 message.guild!.members.cache.get(user.id) ??
92 (await message.guild!.members.fetch(user.id));
93
94 if (
95 !(await this.client.permissionManager.shouldModerate(
96 member,
97 message.member! as GuildMember
98 ))
99 ) {
100 await this.error(message, "You don't have permission to unban this user!");
101 return;
102 }
103 } catch (e) {
104 logError(e);
105 }
106
107 const { infraction, noSuchBan } = await this.client.infractionManager.removeUserBan(user, {
108 guild: message.guild!,
109 moderator: message.member!.user! as User,
110 reason,
111 sendLog: true,
112 abortOnTemplateNotFound: true
113 });
114
115 if (noSuchBan) {
116 await this.error(message, "This user wasn't banned.");
117 return;
118 }
119
120 if (!infraction) {
121 await this.error(message);
122 return;
123 }
124
125 await this.deferredReply(
126 message,
127 {
128 embeds: [
129 await createModerationEmbed({
130 moderator: message.member!.user as User,
131 user,
132 actionDoneName: "unbanned",
133 description: `**${escapeMarkdown(user.tag)}** has been unbanned.`,
134 id: `${infraction.id}`,
135 reason: infraction.reason,
136 color: "Green"
137 })
138 ]
139 },
140 "auto"
141 );
142 }
143 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26