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 |
PermissionsBitField, |
22 |
SlashCommandBuilder, |
23 |
SlashCommandSubcommandBuilder, |
24 |
escapeCodeBlock, |
25 |
escapeInlineCode |
26 |
} from "discord.js"; |
27 |
import Command, { ArgumentType, BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command"; |
28 |
|
29 |
const addOptions = (builder: SlashCommandSubcommandBuilder) => { |
30 |
return builder |
31 |
.addStringOption(option => option.setName("author_name").setDescription("The embed author name")) |
32 |
.addStringOption(option => option.setName("author_iconurl").setDescription("The embed author icon URL")) |
33 |
.addStringOption(option => option.setName("title").setDescription("The embed title")) |
34 |
.addStringOption(option => option.setName("description").setDescription("The embed description")) |
35 |
.addStringOption(option => option.setName("thumbnail").setDescription("The embed thumbnail URL")) |
36 |
.addStringOption(option => option.setName("image").setDescription("The embed image attachment URL")) |
37 |
.addStringOption(option => option.setName("video").setDescription("The embed video attachment URL")) |
38 |
.addStringOption(option => option.setName("footer_text").setDescription("The embed footer text")) |
39 |
.addStringOption(option => option.setName("footer_iconurl").setDescription("The embed footer icon URL")) |
40 |
.addStringOption(option => |
41 |
option.setName("timestamp").setDescription("The embed timestamp, use 'current' to set current date") |
42 |
) |
43 |
.addStringOption(option => option.setName("color").setDescription("The embed color (default is #007bff)")) |
44 |
.addStringOption(option => option.setName("url").setDescription("The embed URL")) |
45 |
.addStringOption(option => |
46 |
option.setName("fields").setDescription("The embed fields, should be in `Field 1: Value 1, Field 2: Value 2` format") |
47 |
); |
48 |
}; |
49 |
|
50 |
export default class EmbedCommand extends Command { |
51 |
public readonly subcommands = ["build", "schema", "send"]; |
52 |
public readonly name = "embed"; |
53 |
|
54 |
public readonly validationRules: ValidationRule[] = [ |
55 |
{ |
56 |
types: [ArgumentType.String], |
57 |
name: "subcommand", |
58 |
errors: { |
59 |
required: `Please provide a subcommand! The available subcommands are: \`${this.subcommands.join("`, `")}\`` |
60 |
} |
61 |
}, |
62 |
{ |
63 |
types: [ArgumentType.StringRest], |
64 |
optional: true, |
65 |
name: "schema" |
66 |
} |
67 |
]; |
68 |
|
69 |
public readonly permissions = [PermissionsBitField.Flags.EmbedLinks, PermissionsBitField.Flags.ManageMessages]; |
70 |
|
71 |
public readonly description = "Create and build custom embeds/schemas."; |
72 |
|
73 |
public readonly slashCommandBuilder = new SlashCommandBuilder() |
74 |
.setName("embed") |
75 |
.setDescription("Make an embed") |
76 |
.addSubcommand(subcmd => addOptions(subcmd.setName("send").setDescription("Make and send an embed"))) |
77 |
.addSubcommand(subcmd => |
78 |
addOptions(subcmd.setName("schema").setDescription("Make and send an embed schema representation")) |
79 |
) |
80 |
.addSubcommand(subcmd => |
81 |
subcmd |
82 |
.setName("build") |
83 |
.setDescription("Build an embed from schema") |
84 |
.addStringOption(option => option.setName("json_schema").setDescription("The embed JSON schema")) |
85 |
); |
86 |
|
87 |
async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> { |
88 |
const subcommand: string = context.isLegacy ? context.parsedNamedArgs.subcommand : context.options.getSubcommand(true); |
89 |
|
90 |
if (!this.subcommands.includes(subcommand.toLowerCase())) { |
91 |
return { |
92 |
__reply: true, |
93 |
content: `\`${escapeInlineCode( |
94 |
escapeCodeBlock(subcommand) |
95 |
)}\` is not a valid subcommand! The available subcommands are: \`${this.subcommands.join("`, `")}\`` |
96 |
}; |
97 |
} |
98 |
|
99 |
await this.deferIfInteraction(message, { |
100 |
ephemeral: true |
101 |
}); |
102 |
|
103 |
const command = this.client.commands.get(`embed__${subcommand}`); |
104 |
|
105 |
if (command) { |
106 |
if (!command.supportsLegacy && context.isLegacy) { |
107 |
await this.error(message, `This command doesn't support legacy commands, please try the slash command.`); |
108 |
return; |
109 |
} |
110 |
|
111 |
await command.execute(message, context); |
112 |
} |
113 |
} |
114 |
} |