/[sudobot]/branches/8.x/src/commands/tools/SnippetCommand.ts
ViewVC logotype

Contents of /branches/8.x/src/commands/tools/SnippetCommand.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: 5206 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 { PermissionsBitField, SlashCommandBuilder, escapeCodeBlock, escapeInlineCode } from "discord.js";
21 import Command, { ArgumentType, BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command";
22
23 export default class SnippetCommand extends Command {
24 public readonly subcommands = ["list", "create", "delete", "rename", "randomize", "pushfile", "pushfiles", "edit"];
25 public readonly name = "snippet";
26 public readonly validationRules: ValidationRule[] = [
27 {
28 types: [ArgumentType.String],
29 name: "subcommand",
30 errors: {
31 required: `Please provide a valid subcommand! The available subcommands are: \`${this.subcommands.join(
32 "`, `"
33 )}\`.`
34 }
35 },
36 {
37 types: [ArgumentType.String],
38 name: "name",
39 optional: true
40 },
41 {
42 types: [ArgumentType.StringRest],
43 name: "content",
44 optional: true
45 }
46 ];
47 public readonly permissions = [
48 PermissionsBitField.Flags.BanMembers,
49 PermissionsBitField.Flags.KickMembers,
50 PermissionsBitField.Flags.ManageGuild,
51 PermissionsBitField.Flags.ModerateMembers,
52 PermissionsBitField.Flags.ManageMessages
53 ];
54 public readonly permissionMode = "or";
55 public readonly aliases = ["tag", "tags", "snippets"];
56 public readonly description = "Manage snippets/tags.";
57
58 public readonly slashCommandBuilder = new SlashCommandBuilder()
59 .addSubcommand(subcommand =>
60 subcommand
61 .setName("create")
62 .setDescription("Create a snippet")
63 .addStringOption(option => option.setName("name").setDescription("The snippet name").setRequired(true))
64 .addStringOption(option => option.setName("content").setDescription("The snippet content").setRequired(true))
65 )
66 .addSubcommand(subcommand =>
67 subcommand
68 .setName("delete")
69 .setDescription("Delete a snippet")
70 .addStringOption(option => option.setName("name").setDescription("The snippet name").setRequired(true))
71 )
72 .addSubcommand(subcommand =>
73 subcommand
74 .setName("rename")
75 .setDescription("Rename a snippet")
76 .addStringOption(option => option.setName("old_name").setDescription("The old snippet name").setRequired(true))
77 .addStringOption(option => option.setName("new_name").setDescription("New snippet name to set").setRequired(true))
78 )
79 .addSubcommand(subcommand => subcommand.setName("list").setDescription("List all snippets in the server"));
80
81 async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
82 const subcommand: string = context.isLegacy ? context.parsedNamedArgs.subcommand : context.options.getSubcommand(true);
83
84 if (!this.subcommands.includes(subcommand.toLowerCase())) {
85 return {
86 __reply: true,
87 content: `\`${escapeInlineCode(
88 escapeCodeBlock(subcommand)
89 )}\` is not a valid subcommand! The available subcommands are: \`${this.subcommands.join("`, `")}\``
90 };
91 }
92
93 if (context.isLegacy && subcommand.toLowerCase() !== "list") {
94 /* Ensure that `parsedNamedArgs.name` is populated */
95 if (context.args[1] === undefined) {
96 await this.error(message, "Please provide a snippet/tag name to perform this action!");
97 return;
98 }
99 }
100
101 await this.deferIfInteraction(message);
102
103 const command = this.client.commands.get(`snippet__${subcommand}`);
104
105 if (command) {
106 await command.execute(message, {
107 ...context,
108 ...(context.isLegacy
109 ? {
110 parsedNamedArgs: {
111 ...context.parsedNamedArgs,
112 name: context.parsedNamedArgs.name,
113 content: context.parsedNamedArgs.content,
114 new_name: subcommand === "rename" ? context.parsedNamedArgs.content.split(/ +/)[0] : undefined
115 }
116 }
117 : {})
118 });
119 }
120 }
121 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26