/[sudobot]/trunk/src/commands/automation/EmbedCommand.ts
ViewVC logotype

Diff of /trunk/src/commands/automation/EmbedCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 248 by rakin, Mon Jul 29 17:29:12 2024 UTC revision 249 by rakin, Mon Jul 29 17:29:12 2024 UTC
# Line 1  Line 1 
1  import { ColorResolvable, CommandInteraction, Message, Util } from 'discord.js';  import { CommandInteraction, Message } from 'discord.js';
2  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
3  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
4    import CommandOptions from '../../types/CommandOptions';
5  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
6  import MessageEmbed from '../../client/MessageEmbed';  import { emoji } from '../../utils/Emoji';
7    
8  export default class EmbedCommand extends BaseCommand {  export default class EmbedCommand extends BaseCommand {
9      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
10      supportsLegacy: boolean = false;      supportsLegacy = false;
11        subcommands = ['send', 'schema', 'build'];
12    
13      constructor() {      constructor() {
14          super('embed', 'automation', []);          super('embed', 'settings', []);
15      }      }
16    
17      async run(client: DiscordClient, interaction: CommandInteraction, options: InteractionOptions) {      async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
18          const getString = (field: string): string | undefined => {          if (!options.isInteraction && options.args[0] === undefined) {
19              return options.options.getString(field) ?? undefined;              await message.reply(`${emoji('error')} No subcommand provided.`);
         };  
   
         const author = {  
             name: getString('author_name'),  
             iconURL: getString('author_iconurl'),  
         };  
   
         const footer = {  
             text: getString('footer_text'),  
             iconURL: getString('footer_iconurl'),  
         };  
   
         if (getString('color') && (!Util.resolveColor(getString('color') as ColorResolvable) || Util.resolveColor(getString('color') as ColorResolvable) === NaN)) {  
             await interaction.reply({ content: "Invalid color given.", ephemeral: true });  
20              return;              return;
21          }          }
22    
23          const embed = new MessageEmbed({          if (!options.isInteraction && (options.args[0] === 'send' || options.args[0] === 'schema')) {
24              author: author.name ? author : undefined,              await message.reply(`${emoji('error')} This command can not be used in legacy mode. Use slash commands instead.`);
25              title: getString('title'),              return;
26              description: getString('description'),          }
             thumbnail: getString('thumbnail') ? {  
                 url: getString('thumbnail')  
             } : undefined,  
             image: getString('image') ? {  
                 url: getString('image')  
             } : undefined,  
             footer: footer.text ? footer : undefined,  
             color: (getString('color') ?? '#007bff') as ColorResolvable,  
             timestamp: getString('timestamp') ? (getString('timestamp') === 'current' ? new Date() : new Date(getString('timestamp')!)) : undefined,  
             fields: getString('fields') ? getString('fields')!.trim().split(',').map(fieldData => {  
                 const [name, value] = fieldData.trim().split(':');  
   
                 return {  
                     name: name.trim(),  
                     value: value.trim(),  
                 };  
             }) : [],  
             url: getString('url')  
         });  
   
         try {  
             await interaction.channel?.send({  
                 embeds: [embed]  
             });  
27    
28              await interaction.reply({ content: 'Message sent.', ephemeral: true });          if (!options.isInteraction && !this.subcommands.includes(options.args[0])) {
29                await message.reply(`${emoji('error')} Invalid subcommand provided. Must be one of ${this.subcommands.map(c => `\`${c}\``)}.`);
30                return;
31          }          }
32          catch (e) {  
33              console.log(e);          const subcommand = options.isInteraction ? options.options.getSubcommand() : options.args[0];
34              interaction.reply({ content: 'Invalid options given.', ephemeral: true });  
35            const command = client.commands.get('embed__' + subcommand);
36    
37            if (command) {
38                await command.execute(client, message, options);
39          }          }
40      }      }
41  }  }

Legend:
Removed from v.248  
changed lines
  Added in v.249

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26