preliminary support for discord embeds using the format composer system
This commit is contained in:
parent
a3452fbebb
commit
5a70931c82
@ -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"]
|
||||
|
@ -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();
|
||||
|
@ -3,7 +3,7 @@
|
||||
"plugins": [
|
||||
{
|
||||
"name": "discord",
|
||||
"version": "1.1.0"
|
||||
"version": "1.2.0"
|
||||
}
|
||||
],
|
||||
"typescript": true
|
||||
|
Loading…
Reference in New Issue
Block a user