/[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 393 by rakin, Mon Jul 29 17:29:59 2024 UTC
# Line 1  Line 1 
1  import { ColorResolvable, CommandInteraction, Message, Util } from 'discord.js';  /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
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 { CommandInteraction, Message } from 'discord.js';
21  import BaseCommand from '../../utils/structures/BaseCommand';  import BaseCommand from '../../utils/structures/BaseCommand';
22  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
23    import CommandOptions from '../../types/CommandOptions';
24  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
25  import MessageEmbed from '../../client/MessageEmbed';  import { emoji } from '../../utils/Emoji';
26    
27  export default class EmbedCommand extends BaseCommand {  export default class EmbedCommand extends BaseCommand {
28      supportsInteractions: boolean = true;      supportsInteractions: boolean = true;
29      supportsLegacy: boolean = false;      subcommands = ['send', 'schema', 'build'];
30    
31      constructor() {      constructor() {
32          super('embed', 'automation', []);          super('embed', 'automation', []);
33      }      }
34    
35      async run(client: DiscordClient, interaction: CommandInteraction, options: InteractionOptions) {      async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
36          const getString = (field: string): string | undefined => {          if (!options.isInteraction) {
37              return options.options.getString(field) ?? undefined;              await message.reply(`${emoji('error')} This command can not be used in legacy mode. Use slash commands instead.`);
         };  
   
         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 });  
38              return;              return;
39          }          }
40    
41          const embed = new MessageEmbed({          const subcommand = options.options.getSubcommand();
             author: author.name ? author : undefined,  
             title: getString('title'),  
             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]  
             });  
42    
43              await interaction.reply({ content: 'Message sent.', ephemeral: true });          const command = client.commands.get('embed__' + subcommand);
44          }  
45          catch (e) {          if (command) {
46              console.log(e);              await command.execute(client, message, options);
             interaction.reply({ content: 'Invalid options given.', ephemeral: true });  
47          }          }
48      }      }
49  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26