/[sudobot]/branches/6.x/src/commands/tools/SnippetEditCommand.ts
ViewVC logotype

Contents of /branches/6.x/src/commands/tools/SnippetEditCommand.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: 7397 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 { Message, PermissionsBitField } from "discord.js";
21 import Command, { ArgumentType, CommandReturn, ValidationRule } from "../../core/Command";
22 import { LegacyCommandContext } from "../../services/CommandManager";
23
24 export default class SnippetEditCommand extends Command {
25 public readonly name = "snippet__edit";
26 public readonly validationRules: ValidationRule[] = [
27 {
28 types: [ArgumentType.String],
29 errors: {
30 required: "Please specify the name of the snippet/tag to edit!",
31 "type:invalid": "Please specify a valid snippet/tag name!"
32 },
33 name: "name"
34 },
35 {
36 types: [ArgumentType.String],
37 errors: {
38 required:
39 "Please specify what to edit in the snippet! One of these can be specified: `level`, `permission` (`perm`), `content`",
40 "type:invalid":
41 "Please specify a valid attribute of the snippet to edit! One of these can be specified: `level`, `permission` (`perm`), `content`"
42 },
43 name: "attr"
44 }
45 ];
46 public readonly permissions = [
47 PermissionsBitField.Flags.BanMembers,
48 PermissionsBitField.Flags.KickMembers,
49 PermissionsBitField.Flags.ManageGuild,
50 PermissionsBitField.Flags.ModerateMembers,
51 PermissionsBitField.Flags.ManageMessages
52 ];
53 public readonly permissionMode = "or";
54 public readonly aliases: string[] = ["editsnippet", "snippetedit", "tagedit", "edittag", "updatetag"];
55 public readonly beta: boolean = true;
56 public readonly supportsInteractions = false;
57
58 async execute(message: Message, context: LegacyCommandContext): Promise<CommandReturn> {
59 if (!this.client.configManager.config[message.guildId!]) return;
60
61 const name: string = context.parsedNamedArgs.name;
62
63 if (!(await this.client.snippetManager.checkPermissionInSnippetCommands(name, message, this))) {
64 return;
65 }
66
67 const index = name !== context.args[0] ? 1 : 0;
68
69 if (!context.args[index + 1]) {
70 await this.error(message, this.validationRules[1].errors?.required!);
71 return;
72 }
73
74 const attrs = ["level", "permission", "perm", "content"] as const;
75
76 if (!attrs.includes(context.args[index + 1]?.toLowerCase() as (typeof attrs)[number])) {
77 await this.error(message, this.validationRules[1].errors?.["type:invalid"]!);
78 return;
79 }
80
81 const attr = context.args[index + 1]?.toLowerCase() as (typeof attrs)[number];
82
83 switch (attr) {
84 case "level": {
85 if (this.client.configManager.config[message.guildId!]?.permissions.mode !== "levels") {
86 await this.error(
87 message,
88 "This server does not use the level-based permission system. Please switch to the level-based system to set a permission level for this snippet. Alternatively, if you're using the named permission system, please edit the `permission` (or `perm`) attribute instead."
89 );
90 return;
91 }
92
93 if (!context.args[index + 2]) {
94 await this.error(message, "Please specify a permission level to set!");
95 return;
96 }
97
98 const level = parseInt(context.args[index + 2]);
99
100 if (isNaN(level) || level < 0 || level > 100) {
101 await this.error(message, "Please specify a valid permission level (0-100) to set!");
102 return;
103 }
104
105 const { error } = await this.client.snippetManager.updateSnippet({
106 name,
107 guildId: message.guildId!,
108 level
109 });
110
111 if (error) {
112 await this.error(message, error);
113 return;
114 }
115
116 await this.success(message, "Sucessfully updated snippet permission level.");
117 break;
118 }
119
120 case "perm":
121 case "permission": {
122 if (this.client.configManager.config[message.guildId!]?.permissions.mode !== "layered") {
123 await this.error(
124 message,
125 "This server does not use the layered permission system. Please switch to the layered permission system to set a layered permission for this snippet. Alternatively, if you're using the level-based permission system, please edit the `level` attribute instead."
126 );
127 return;
128 }
129
130 if (!context.args[index + 2]) {
131 await this.error(message, "Please specify a permission overwrite name to set!");
132 return;
133 }
134
135 const { error } = await this.client.snippetManager.updateSnippet({
136 name,
137 guildId: message.guildId!,
138 permissionRoleName: context.args[index + 2]
139 });
140
141 if (error) {
142 await this.error(message, error);
143 return;
144 }
145
146 await this.success(message, "Sucessfully updated snippet permissions.");
147 break;
148 }
149
150 case "content": {
151 if (!context.args[index + 2]) {
152 await this.error(message, "Please specify new content for the snippet!");
153 return;
154 }
155
156 const content = message.content
157 .slice(this.client.configManager.config[message.guildId!]!.prefix.length)
158 .trimStart()
159 .slice(context.argv[0].length)
160 .trimStart()
161 .slice(context.args[0].length)
162 .trimStart()
163 .slice(context.args[1].length)
164 .trimStart()
165 .slice(index === 1 ? context.args[2].length : 0)
166 .trimStart();
167
168 const { error } = await this.client.snippetManager.updateSnippet({
169 name,
170 guildId: message.guildId!,
171 content
172 });
173
174 if (error) {
175 await this.error(message, error);
176 return;
177 }
178
179 await this.success(message, "Sucessfully updated snippet content.");
180 break;
181 }
182 }
183 }
184 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26