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 |
} |