/[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 260 by rakin, Mon Jul 29 17:29:15 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;      subcommands = ['send', 'schema', 'build'];
11    
12      constructor() {      constructor() {
13          super('embed', 'automation', []);          super('embed', 'settings', []);
14      }      }
15    
16      async run(client: DiscordClient, interaction: CommandInteraction, options: InteractionOptions) {      async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
17          const getString = (field: string): string | undefined => {          if (!options.isInteraction && options.args[0] === undefined) {
18              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 });  
19              return;              return;
20          }          }
21    
22          const embed = new MessageEmbed({          if (!options.isInteraction && (options.args[0] === 'send' || options.args[0] === 'schema')) {
23              author: author.name ? author : undefined,              await message.reply(`${emoji('error')} This command can not be used in legacy mode. Use slash commands instead.`);
24              title: getString('title'),              return;
25              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]  
             });  
26    
27              await interaction.reply({ content: 'Message sent.', ephemeral: true });          if (!options.isInteraction && !this.subcommands.includes(options.args[0])) {
28                await message.reply(`${emoji('error')} Invalid subcommand provided. Must be one of ${this.subcommands.map(c => `\`${c}\``).join(', ')}.`);
29                return;
30          }          }
31          catch (e) {  
32              console.log(e);          const subcommand = options.isInteraction ? options.options.getSubcommand() : options.args[0];
33              interaction.reply({ content: 'Invalid options given.', ephemeral: true });  
34            const command = client.commands.get('embed__' + subcommand);
35    
36            if (command) {
37                if (!options.isInteraction)
38                    options.args.shift();
39                await command.execute(client, message, options);
40          }          }
41      }      }
42  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26