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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 248 - (show annotations)
Mon Jul 29 17:29:12 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2718 byte(s)
feat: add embed builder command (#57)
1 import { ColorResolvable, CommandInteraction, Message, Util } from 'discord.js';
2 import BaseCommand from '../../utils/structures/BaseCommand';
3 import DiscordClient from '../../client/Client';
4 import InteractionOptions from '../../types/InteractionOptions';
5 import MessageEmbed from '../../client/MessageEmbed';
6
7 export default class EmbedCommand extends BaseCommand {
8 supportsInteractions: boolean = true;
9 supportsLegacy: boolean = false;
10
11 constructor() {
12 super('embed', 'automation', []);
13 }
14
15 async run(client: DiscordClient, interaction: CommandInteraction, options: InteractionOptions) {
16 const getString = (field: string): string | undefined => {
17 return options.options.getString(field) ?? undefined;
18 };
19
20 const author = {
21 name: getString('author_name'),
22 iconURL: getString('author_iconurl'),
23 };
24
25 const footer = {
26 text: getString('footer_text'),
27 iconURL: getString('footer_iconurl'),
28 };
29
30 if (getString('color') && (!Util.resolveColor(getString('color') as ColorResolvable) || Util.resolveColor(getString('color') as ColorResolvable) === NaN)) {
31 await interaction.reply({ content: "Invalid color given.", ephemeral: true });
32 return;
33 }
34
35 const embed = new MessageEmbed({
36 author: author.name ? author : undefined,
37 title: getString('title'),
38 description: getString('description'),
39 thumbnail: getString('thumbnail') ? {
40 url: getString('thumbnail')
41 } : undefined,
42 image: getString('image') ? {
43 url: getString('image')
44 } : undefined,
45 footer: footer.text ? footer : undefined,
46 color: (getString('color') ?? '#007bff') as ColorResolvable,
47 timestamp: getString('timestamp') ? (getString('timestamp') === 'current' ? new Date() : new Date(getString('timestamp')!)) : undefined,
48 fields: getString('fields') ? getString('fields')!.trim().split(',').map(fieldData => {
49 const [name, value] = fieldData.trim().split(':');
50
51 return {
52 name: name.trim(),
53 value: value.trim(),
54 };
55 }) : [],
56 url: getString('url')
57 });
58
59 try {
60 await interaction.channel?.send({
61 embeds: [embed]
62 });
63
64 await interaction.reply({ content: 'Message sent.', ephemeral: true });
65 }
66 catch (e) {
67 console.log(e);
68 interaction.reply({ content: 'Invalid options given.', ephemeral: true });
69 }
70 }
71 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26