Skip to content

Commit

Permalink
Fixed issue with hex colors in embeds
Browse files Browse the repository at this point in the history
Embed colors can be provided as hex color codes again.
Additionally color names can be used now.
  • Loading branch information
crycode-de committed May 9, 2024
1 parent 683fa33 commit 668d628
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
44 changes: 37 additions & 7 deletions build/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/main.js.map

Large diffs are not rendered by default.

48 changes: 45 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import {
VoiceState,
ActivityType,
TextChannel,
APIEmbed,
resolveColor,
HexColorString,
} from 'discord.js';

import {
Expand Down Expand Up @@ -1984,7 +1987,7 @@ class DiscordAdapter extends Adapter {

this.log.debug(`Send to ${targetName}: ${JSON.stringify(mo)}`);
try {
const msg = await target.send(mo);
const msg = await target.send(this.prepareMessageForSend(mo));
this.log.debug(`Sent with message ID ${msg.id}`);
return true;
} catch (err) {
Expand Down Expand Up @@ -2282,7 +2285,7 @@ class DiscordAdapter extends Adapter {
}
}
try {
msg = await user.send(sendPayload.content);
msg = await user.send(this.prepareMessageForSend(sendPayload.content));
this.sendToIfCb(obj.from, obj.command, { result: `Message sent to user ${userNameOrTag(user)}`, ...sendPayload, messageId: msg.id }, obj.callback);
} catch (err) {
this.sendToIfCb(obj.from, obj.command, { error: `Error sending message to user ${userNameOrTag(user)}: ${err}`, ...sendPayload }, obj.callback);
Expand All @@ -2296,7 +2299,7 @@ class DiscordAdapter extends Adapter {
return;
}
try {
msg = await channel.send(sendPayload.content);
msg = await channel.send(this.prepareMessageForSend(sendPayload.content));
this.sendToIfCb(obj.from, obj.command, { result: `Message sent to channel ${channel.name}`, ...sendPayload, messageId: msg.id }, obj.callback);
} catch (err) {
this.sendToIfCb(obj.from, obj.command, { error: `Error sending message to channel ${channel.name}: ${err}`, ...sendPayload }, obj.callback);
Expand Down Expand Up @@ -2950,6 +2953,45 @@ ${readableInstances.join('\n')}`;
return mo;
}

/**
* Prepare a message for sending.
*
* This some message parts to be valid discord message data.
* @param msg The message as `string` or `MessageCreateOptions`.
* @returns The prepared message.
*/
private prepareMessageForSend (msg: string | MessageCreateOptions): string | MessageCreateOptions {
if (typeof msg === 'string') {
return msg;
}

// replace hexo color codes in embeds
if (msg.embeds) {
for (const embed of msg.embeds) {
// color codes may be given as string in user defined embeds
if (typeof (embed as APIEmbed).color === 'string') {
const colorStr: string = (embed as APIEmbed).color as unknown as string;
if (colorStr.match(/^\d+$/)) {
// it's a number color given as string
(embed as APIEmbed).color = parseInt(colorStr, 10);
} else {
// it's something else (maybe a color name or hexo color)
// try to resolve the color
try {
(embed as APIEmbed).color = resolveColor(colorStr as HexColorString);
} catch (err) {
// color could not be resolved, use a default color
(embed as APIEmbed).color = resolveColor('Greyple');
this.log.warn(`Error embed color '${colorStr}': ${err}`);
}
}
}
}
}

return msg;
}

/**
* Find a previous message from/to a user or in a server text channel.
* @param identifier Parameters to find the message.
Expand Down

0 comments on commit 668d628

Please sign in to comment.