/[sudobot]/branches/4.x/src/commands/moderation/PenalizeCommand.ts
ViewVC logotype

Contents of /branches/4.x/src/commands/moderation/PenalizeCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months, 1 week ago) by rakinar2
File MIME type: application/typescript
File size: 7383 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-2022 OSN Inc.
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 { BanOptions, CommandInteraction, Message, Permissions, User } from "discord.js";
21 import BaseCommand from "../../utils/structures/BaseCommand";
22 import DiscordClient from "../../client/Client";
23 import CommandOptions from "../../types/CommandOptions";
24 import InteractionOptions from "../../types/InteractionOptions";
25 import MessageEmbed from "../../client/MessageEmbed";
26 import getUser from "../../utils/getUser";
27 import { shouldNotModerate, hasPermission } from "../../utils/util";
28
29 export default class PenalizeCommand extends BaseCommand {
30 supportsInteractions: boolean = true;
31 permissions = [Permissions.FLAGS.MANAGE_MESSAGES];
32
33 constructor() {
34 super("penalize", "moderation", ["pban", "penalty"]);
35 }
36
37 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
38 if (!options.isInteraction && typeof options.args[0] === "undefined") {
39 await msg.reply({
40 embeds: [new MessageEmbed().setColor("#f14a60").setDescription(`This command requires at least one argument.`)],
41 });
42
43 return;
44 }
45
46 let user: User;
47 let banOptions: BanOptions = {};
48
49 if (options.isInteraction) {
50 user = await (<User>options.options.getUser("user"));
51
52 if (options.options.getString("reason")) {
53 banOptions.reason = await (<string>options.options.getString("reason"));
54 }
55
56 if (options.options.getInteger("days")) {
57 banOptions.days = await (<number>options.options.getInteger("days"));
58 }
59 } else {
60 const user2 = await getUser(client, msg as Message, options);
61
62 if (!user2) {
63 await msg.reply({
64 embeds: [new MessageEmbed().setColor("#f14a60").setDescription(`Invalid user given.`)],
65 });
66
67 return;
68 }
69
70 user = user2;
71
72 const index = await options.args.indexOf("-d");
73
74 if (options.args[1]) {
75 const args = [...options.args];
76 args.shift();
77
78 if (index !== -1) {
79 args.splice(index - 1, 2);
80 }
81
82 banOptions.reason = await args.join(" ");
83 }
84
85 if (index !== -1) {
86 const days = await options.args[index + 1];
87
88 if (days === undefined) {
89 await msg.reply({
90 embeds: [new MessageEmbed().setColor("#f14a60").setDescription(`Option \`-d\` (days) requires an argument.`)],
91 });
92
93 return;
94 }
95
96 if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) {
97 await msg.reply({
98 embeds: [
99 new MessageEmbed()
100 .setColor("#f14a60")
101 .setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`),
102 ],
103 });
104
105 return;
106 }
107
108 banOptions.days = await parseInt(days);
109 }
110 }
111
112 try {
113 const member = await msg.guild?.members.fetch(user.id);
114
115 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to penalize this user."))) {
116 return;
117 }
118
119 if (member && shouldNotModerate(client, member)) {
120 await msg.reply({
121 embeds: [new MessageEmbed().setColor("#f14a60").setDescription("Cannot penalize this user: Operation not permitted")],
122 });
123
124 return;
125 }
126 } catch (e) {
127 console.log(e);
128 }
129
130 try {
131 const id = Math.round(Math.random() * 10000);
132
133 user.send({
134 embeds: [
135 new MessageEmbed({
136 author: {
137 name: `You have been penalized in ${msg.guild!.name}`,
138 iconURL: msg.guild!.iconURL() ?? undefined,
139 },
140 color: 0xf14a60,
141 description: "This is a penalty for your behavior in the server. You may appeal it by contacting a moderator.",
142 fields: [
143 {
144 name: "Reason",
145 value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason,
146 },
147 {
148 name: "Infraction ID",
149 value: id + "",
150 },
151 ],
152 }),
153 ],
154 }).catch(console.error);
155
156 await msg.reply({
157 embeds: [
158 new MessageEmbed()
159 .setAuthor({
160 name: user.tag,
161 iconURL: user.displayAvatarURL(),
162 })
163 .setDescription(user.tag + " has been penalized. || This is fake :joy: ||")
164 .addFields([
165 {
166 name: "Penalized by",
167 value: (msg.member!.user as User).tag,
168 },
169 {
170 name: "Reason",
171 value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason,
172 },
173 {
174 name: "Days of message deletion",
175 value: banOptions.days === undefined ? "*No message will be deleted*" : banOptions.days + "",
176 },
177 {
178 name: "Infraction ID",
179 value: id + "",
180 },
181 ]),
182 ],
183 });
184 } catch (e) {
185 await msg.reply({
186 embeds: [
187 new MessageEmbed()
188 .setColor("#f14a60")
189 .setDescription("Failed to penalize this user. Maybe missing permisions or I'm not allowed to penalize this user?"),
190 ],
191 });
192
193 return;
194 }
195 }
196 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26