/[sudobot]/branches/5.x/src/commands/settings/BlockedWordCommand.ts
ViewVC logotype

Annotation of /branches/5.x/src/commands/settings/BlockedWordCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 9178 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
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 { EmbedBuilder, PermissionFlagsBits, SlashCommandBuilder, Snowflake, escapeMarkdown } from "discord.js";
21     import Command, { ArgumentType, BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command";
22     import Pagination from "../../utils/Pagination";
23    
24     export default class BlockedWordCommand extends Command {
25     public readonly subcommandsCustom = ["add", "remove", "has", "list"];
26     public readonly name = "blockedword";
27     public readonly validationRules: ValidationRule[] = [
28     {
29     types: [ArgumentType.String],
30     requiredErrorMessage: `Please provide a subcommand! The valid subcommands are: \`${this.subcommandsCustom.join("`, `")}\`.`,
31     typeErrorMessage: `Please provide a __valid__ subcommand! The valid subcommands are: \`${this.subcommandsCustom.join("`, `")}\`.`,
32     name: "subcommand"
33     }
34     ];
35     public readonly permissions = [PermissionFlagsBits.ManageGuild, PermissionFlagsBits.BanMembers];
36     public readonly permissionMode = "or";
37    
38     public readonly description = "Manage blocked words.";
39    
40     public readonly detailedDescription = [
41     "Add/remove/check/view the blocked words. All arguments, separated by spaces will be treated as different words.\n",
42     "**Subcommands**",
43     "* `add <...words>` - Add blocked word(s)",
44     "* `remove <...words>` - Remove blocked word(s)",
45     "* `has <word>` - Check if the given word is blocked",
46     "* `list` - List all the blocked words"
47     ].join("\n");
48    
49     public readonly argumentSyntaxes = ["<subcommand> [...args]"];
50    
51     public readonly slashCommandBuilder = new SlashCommandBuilder()
52     .addSubcommand(subcommand =>
53     subcommand
54     .setName("add")
55     .setDescription("Add blocked words")
56     .addStringOption(option => option.setName("words").setDescription("The words to block").setRequired(true))
57     )
58     .addSubcommand(subcommand =>
59     subcommand
60     .setName("remove")
61     .setDescription("Remove blocked words")
62     .addStringOption(option => option.setName("words").setDescription("The words to remove from blocklist").setRequired(true))
63     )
64     .addSubcommand(subcommand =>
65     subcommand
66     .setName("has")
67     .setDescription("Check if a blocked word exists in the blocklist")
68     .addStringOption(option => option.setName("word").setDescription("The word to check").setRequired(true))
69     )
70     .addSubcommand(subcommand => subcommand.setName("list").setDescription("Show the blocked word list"));
71     public readonly aliases = ["blockedwords"];
72    
73     createConfigIfNotExists(guildId: Snowflake) {
74     this.client.configManager.config[guildId!]!.message_filter ??= {
75     enabled: true,
76     delete_message: true,
77     send_logs: true
78     };
79    
80     this.client.configManager.config[guildId!]!.message_filter!.data ??= {
81     blocked_tokens: [],
82     blocked_words: []
83     };
84    
85     this.client.configManager.config[guildId!]!.message_filter!.data!.blocked_words ??= [];
86     }
87    
88     async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
89     const subcommand = (context.isLegacy ? context.parsedNamedArgs.subcommand : context.options.getSubcommand(true))?.toString();
90    
91     if (!this.subcommandsCustom.includes(subcommand)) {
92     await this.error(message, `Invalid subcommand provided. The valid subcommands are: \`${this.subcommandsCustom.join("`, `")}\`.`);
93     return;
94     }
95    
96     if (context.isLegacy && context.args[1] === undefined && subcommand !== "list") {
97     await this.error(
98     message,
99     `You must specify a word ${subcommand === "add" ? "to block" : subcommand === "remove" ? "to remove" : "to check"}!`
100     );
101     return;
102     }
103    
104     if (!this.client.configManager.config[message.guildId!]) {
105     return;
106     }
107    
108     await this.deferIfInteraction(message);
109    
110     if (context.isLegacy) {
111     context.args.shift();
112     }
113    
114     this.createConfigIfNotExists(message.guildId!);
115    
116     switch (subcommand) {
117     case "add":
118     const words = context.isLegacy ? context.args : context.options.getString("words", true).split(/ +/);
119    
120     for await (const word of words) {
121     if (this.client.configManager.config[message.guildId!]?.message_filter?.data?.blocked_words.includes(word)) {
122     continue;
123     }
124    
125     this.client.configManager.config[message.guildId!]?.message_filter?.data?.blocked_words.push(word);
126     }
127    
128     await this.client.configManager.write();
129     await this.success(message, `The given word(s) have been blocked.`);
130     break;
131    
132     case "has":
133     const word = context.isLegacy ? context.args[0] : context.options.getString("word", true);
134    
135     if (this.client.configManager.config[message.guildId!]?.message_filter?.data?.blocked_words.includes(word)) {
136     await this.success(message, `This word is in the blocklist.`);
137     } else {
138     await this.error(message, `This word is not in the blocklist.`);
139     }
140    
141     return;
142    
143     case "remove":
144     const wordsToRemove = context.isLegacy ? context.args : context.options.getString("words", true).split(/ +/);
145    
146     for await (const word of wordsToRemove) {
147     const index = this.client.configManager.config[message.guildId!]?.message_filter?.data?.blocked_words.indexOf(word);
148    
149     if (!index || index === -1) {
150     continue;
151     }
152    
153     this.client.configManager.config[message.guildId!]?.message_filter?.data?.blocked_words.splice(index, 1);
154     }
155    
156     await this.client.configManager.write();
157     await this.success(message, `The given word(s) have been unblocked.`);
158     break;
159    
160     case "list":
161     {
162     const words: string[] = this.client.configManager.config[message.guildId!]?.message_filter?.data?.blocked_words ?? [];
163     const safeWords: string[][] = [];
164     let length = 0;
165    
166     for (const unsafeWord of words) {
167     if (safeWords.length === 0) safeWords.push([]);
168    
169     const word = escapeMarkdown(unsafeWord);
170    
171     if (length + word.length >= 3000) {
172     safeWords.push([word]);
173     length = word.length;
174     continue;
175     }
176    
177     const index = safeWords.length - 1;
178    
179     safeWords[index].push(word);
180     length += word.length;
181     }
182    
183     const pagination = new Pagination(safeWords, {
184     channelId: message.channelId!,
185     guildId: message.guildId!,
186     limit: 1,
187     timeout: 120_000,
188     userId: message.member!.user.id,
189     client: this.client,
190     embedBuilder({ currentPage, data, maxPages }) {
191     return new EmbedBuilder({
192     author: {
193     name: `Blocked words in ${message.guild!.name}`,
194     iconURL: message.guild!.iconURL() ?? undefined
195     },
196     color: 0x007bff,
197     description: "`" + data[0].join("`, `") + "`",
198     footer: {
199     text: `Page ${currentPage} of ${maxPages}`
200     }
201     });
202     }
203     });
204    
205     let reply = await this.deferredReply(message, await pagination.getMessageOptions());
206     await pagination.start(reply);
207     }
208    
209     break;
210     }
211     }
212     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26