From 5a70931c822688aac2e07e78753a62c0f63f0319 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Wed, 14 Apr 2021 23:07:05 +0300 Subject: [PATCH] preliminary support for discord embeds using the format composer system --- discord/plugin.json | 2 +- discord/plugin.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++- squeebot.repo.json | 2 +- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/discord/plugin.json b/discord/plugin.json index a2abc15..99efe73 100644 --- a/discord/plugin.json +++ b/discord/plugin.json @@ -2,7 +2,7 @@ "main": "plugin.js", "name": "discord", "description": "Discord Service for Squeebot 3", - "version": "1.1.1", + "version": "1.2.0", "tags": ["service", "discord"], "dependencies": ["control?"], "npmDependencies": ["discord.js@^12.5.1"] diff --git a/discord/plugin.ts b/discord/plugin.ts index c1164f5..ff9910d 100644 --- a/discord/plugin.ts +++ b/discord/plugin.ts @@ -10,6 +10,7 @@ import util from 'util'; import Discord from 'discord.js'; import { logger } from '@squeebot/core/lib/core'; +import { thousandsSeparator, timeSince, toHHMMSS } from '@squeebot/core/lib/common'; import { EMessageType, Formatter, @@ -19,6 +20,73 @@ import { Protocol } from '@squeebot/core/lib/types'; +class DiscordFormatter extends MarkdownFormatter { + public compose(objs: any): any { + const embed = new Discord.MessageEmbed(); + + for (const i in objs) { + const elem = objs[i]; + + const elemType = elem[0]; + let elemValue = elem[1]; + const elemParams = elem[2]; + + if (!elemValue || elemType !== 'field') { + continue; + } + + // Special types + if (elemParams && elemParams.type) { + switch (elemParams.type) { + case 'time': + elemValue = new Date(elemValue).toString(); + break; + case 'metric': + elemValue = thousandsSeparator(elemValue); + break; + case 'timesince': + elemValue = timeSince(elemValue); + break; + case 'duration': + elemValue = toHHMMSS(elemValue); + break; + } + } + + if (elemParams.type === 'description') { + embed.setDescription(elemValue); + continue; + } + + if (elemParams.type === 'title') { + embed.setTitle(elemValue); + if (elemParams.color) { + embed.setColor(elemParams.color.toUpperCase()); + } + continue; + } + + if (elemParams && elemParams.label) { + let label = elemParams.label; + + // If the label param is an array, choose the last element + // The last element is generally the text version, as opposed to + // the first element being an icon. + if (typeof label === 'object') { + label = elemParams.label[elemParams.label.length - 1]; + } + + embed.addField(label, elemValue, true); + } else { + embed.setDescription(embed.description += elemValue); + } + } + + // May return an object, but your protocol must support it. + return embed; + } +} + class DiscordMessageAdapter implements IMessage { public time: Date = new Date(); public resolved = false; @@ -67,7 +135,7 @@ class DiscordMessageAdapter implements IMessage { } class DiscordProtocol extends Protocol { - public format: Formatter = new MarkdownFormatter(); + public format: Formatter = new DiscordFormatter(); public type = 'DiscordProtocol'; private client = new Discord.Client(); diff --git a/squeebot.repo.json b/squeebot.repo.json index 6b8d04e..46e32cf 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -3,7 +3,7 @@ "plugins": [ { "name": "discord", - "version": "1.1.0" + "version": "1.2.0" } ], "typescript": true