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",
|
"main": "plugin.js",
|
||||||
"name": "discord",
|
"name": "discord",
|
||||||
"description": "Discord Service for Squeebot 3",
|
"description": "Discord Service for Squeebot 3",
|
||||||
"version": "1.1.1",
|
"version": "1.2.0",
|
||||||
"tags": ["service", "discord"],
|
"tags": ["service", "discord"],
|
||||||
"dependencies": ["control?"],
|
"dependencies": ["control?"],
|
||||||
"npmDependencies": ["discord.js@^12.5.1"]
|
"npmDependencies": ["discord.js@^12.5.1"]
|
||||||
|
@ -10,6 +10,7 @@ import util from 'util';
|
|||||||
import Discord from 'discord.js';
|
import Discord from 'discord.js';
|
||||||
|
|
||||||
import { logger } from '@squeebot/core/lib/core';
|
import { logger } from '@squeebot/core/lib/core';
|
||||||
|
import { thousandsSeparator, timeSince, toHHMMSS } from '@squeebot/core/lib/common';
|
||||||
import {
|
import {
|
||||||
EMessageType,
|
EMessageType,
|
||||||
Formatter,
|
Formatter,
|
||||||
@ -19,6 +20,73 @@ import {
|
|||||||
Protocol
|
Protocol
|
||||||
} from '@squeebot/core/lib/types';
|
} 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 {
|
class DiscordMessageAdapter implements IMessage {
|
||||||
public time: Date = new Date();
|
public time: Date = new Date();
|
||||||
public resolved = false;
|
public resolved = false;
|
||||||
@ -67,7 +135,7 @@ class DiscordMessageAdapter implements IMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DiscordProtocol extends Protocol {
|
class DiscordProtocol extends Protocol {
|
||||||
public format: Formatter = new MarkdownFormatter();
|
public format: Formatter = new DiscordFormatter();
|
||||||
public type = 'DiscordProtocol';
|
public type = 'DiscordProtocol';
|
||||||
|
|
||||||
private client = new Discord.Client();
|
private client = new Discord.Client();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"plugins": [
|
"plugins": [
|
||||||
{
|
{
|
||||||
"name": "discord",
|
"name": "discord",
|
||||||
"version": "1.1.0"
|
"version": "1.2.0"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"typescript": true
|
"typescript": true
|
||||||
|
Loading…
Reference in New Issue
Block a user