11import { BaseParser , split , type Context , type IParser , type Awaitable } from 'tagscript' ;
22
3+ import { resolveColor } from '../Utils' ;
4+
35import type { EmbedData , APIEmbed } from 'discord.js' ;
46
57/**
@@ -13,12 +15,12 @@ import type { EmbedData, APIEmbed } from 'discord.js';
1315 * @example
1416 * Using JSON
1517 * ```yaml
16- * { embed: json }
18+ * {embed: json}
1719 * ```
1820 * @example
1921 * ```yaml
20- * { embed: { "title": "Hello!", "description": "This is a test embed." } }
21- * { embed: {
22+ * {embed: { "title": "Hello!", "description": "This is a test embed." } }
23+ * {embed: {
2224 * "title": "Here's a random duck!",
2325 * "image": { "url": "https://random-d.uk/api/randomimg" },
2426 * "color": 15194415
@@ -27,14 +29,26 @@ import type { EmbedData, APIEmbed } from 'discord.js';
2729 * @example
2830 * Using properties
2931 * ```yaml
30- * { embed(property): value }
32+ * {embed(property):value}
3133 * ```
3234 * @example
3335 * ```yaml
34- * { embed(color): 0x37b2cb }
35- * { embed(title): Rules }
36- * { embed(description): Follow these rules to ensure a good experience in our server! }
37- * { embed(field): Rule 1|Respect everyone you speak to.|false }
36+ * {embed(color): 0x37b2cb}
37+ * {embed(title): Rules}
38+ * {embed(description): Follow these rules to ensure a good experience in our server!}
39+ * {embed(field): Rule 1|Respect everyone you speak to.|false}
40+ * ```
41+ * Developers need to construct the embed builder themselves with the output of the tag.
42+ * @example
43+ * ```ts
44+ * const { Interpreter } = require("tagscript")
45+ * const { EmbedParser } = require("tagscript-plugin-discord")
46+ *
47+ * const ts = new Interpreter(new EmbedParser())
48+ * const result = await ts.run('{embed: { "title": "Hello!", "description": "This is a test embed." }}')
49+ *
50+ * // You might need to change the embed object before passing to `EmbedBuilder`. Changes such as change thumbnail and image value from string to object.
51+ * const embed = new EmbedBuilder(response.actions.embed);
3852 * ```
3953 * @remarks
4054 * The return type depends on user's input. So it might not be `EmbedData | APIEmbed`. So use a typeguard to check.
@@ -63,6 +77,13 @@ export class EmbedParser extends BaseParser implements IParser {
6377 } ) ;
6478 }
6579
80+ if ( ctx . tag . parameter === 'color' ) {
81+ return this . returnEmbed ( ctx , {
82+ // This can return number but it should be handled by the dev
83+ color : resolveColor ( ctx . tag . payload ! ) as number
84+ } ) ;
85+ }
86+
6687 return this . returnEmbed ( ctx , { [ ctx . tag . parameter ] : ctx . tag . payload } ) ;
6788 }
6889
@@ -73,7 +94,9 @@ export class EmbedParser extends BaseParser implements IParser {
7394 * @returns
7495 */
7596 protected parseEmbedJSON ( payload : string ) : Awaitable < APIEmbed | EmbedData > {
76- return JSON . parse ( payload ) ;
97+ const parsedResult = JSON . parse ( payload ) ;
98+ if ( parsedResult . color ) parsedResult . color = resolveColor ( parsedResult . color ) ;
99+ return parsedResult ;
77100 }
78101
79102 private returnEmbed ( ctx : Context , data : APIEmbed | EmbedData ) : string {
0 commit comments