This commit is contained in:
Evert Prants 2021-09-03 20:22:33 +03:00
parent 5a70931c82
commit 215215c536
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 741 additions and 507 deletions

View File

@ -2,8 +2,8 @@
"main": "plugin.js", "main": "plugin.js",
"name": "discord", "name": "discord",
"description": "Discord Service for Squeebot 3", "description": "Discord Service for Squeebot 3",
"version": "1.2.0", "version": "1.2.1",
"tags": ["service", "discord"], "tags": ["service", "discord"],
"dependencies": ["control?"], "dependencies": ["control?"],
"npmDependencies": ["discord.js@^12.5.1"] "npmDependencies": ["discord.js@^13.1.0"]
} }

View File

@ -7,7 +7,7 @@ import {
import util from 'util'; import util from 'util';
import Discord from 'discord.js'; import Discord, { Intents } 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 { thousandsSeparator, timeSince, toHHMMSS } from '@squeebot/core/lib/common';
@ -31,12 +31,12 @@ class DiscordFormatter extends MarkdownFormatter {
let elemValue = elem[1]; let elemValue = elem[1];
const elemParams = elem[2]; const elemParams = elem[2];
if (!elemValue || elemType !== 'field') { if (!elemValue || (elemType !== 'field' && elemType !== 'bold')) {
continue; continue;
} }
// Special types // Special types
if (elemParams && elemParams.type) { if (elemParams?.type) {
switch (elemParams.type) { switch (elemParams.type) {
case 'time': case 'time':
elemValue = new Date(elemValue).toString(); elemValue = new Date(elemValue).toString();
@ -53,12 +53,15 @@ class DiscordFormatter extends MarkdownFormatter {
} }
} }
if (elemParams.type === 'description') { if (elemParams?.type === 'description') {
embed.setDescription(elemValue); embed.setDescription(embed.description
? embed.description += `\n${elemValue}`
: elemValue
);
continue; continue;
} }
if (elemParams.type === 'title') { if (elemParams?.type === 'title') {
embed.setTitle(elemValue); embed.setTitle(elemValue);
if (elemParams.color) { if (elemParams.color) {
embed.setColor(elemParams.color.toUpperCase()); embed.setColor(elemParams.color.toUpperCase());
@ -66,7 +69,7 @@ class DiscordFormatter extends MarkdownFormatter {
continue; continue;
} }
if (elemParams && elemParams.label) { if (elemParams?.label) {
let label = elemParams.label; let label = elemParams.label;
// If the label param is an array, choose the last element // If the label param is an array, choose the last element
@ -78,7 +81,10 @@ class DiscordFormatter extends MarkdownFormatter {
embed.addField(label, elemValue, true); embed.addField(label, elemValue, true);
} else { } else {
embed.setDescription(embed.description += elemValue); embed.setDescription(embed.description
? embed.description += `\n${elemValue}`
: elemValue
);
} }
} }
@ -138,7 +144,19 @@ class DiscordProtocol extends Protocol {
public format: Formatter = new DiscordFormatter(); public format: Formatter = new DiscordFormatter();
public type = 'DiscordProtocol'; public type = 'DiscordProtocol';
private client = new Discord.Client(); private client = new Discord.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
],
});
private eventsAttached = false; private eventsAttached = false;
@ -151,8 +169,17 @@ class DiscordProtocol extends Protocol {
} }
this.emit('running'); this.emit('running');
}); });
this.attachEvents(); this.attachEvents();
this.client.login(this.config.token);
try {
this.client.login(this.config.token);
} catch (e: any) {
this.client.on('error', (e: Error) => {
this.emit('error', e.message);
});
this.client.on('disconnect', () => this.stop(true));
}
} }
public stop(force = false): void { public stop(force = false): void {
@ -176,10 +203,16 @@ class DiscordProtocol extends Protocol {
if (this.eventsAttached) { if (this.eventsAttached) {
return; return;
} }
this.eventsAttached = true; this.eventsAttached = true;
this.client.on('error', (e) => this.emit('error', e));
this.client.on('error', (e: Error) => {
this.emit('error', e.message);
});
this.client.on('disconnect', () => this.stop(true)); this.client.on('disconnect', () => this.stop(true));
this.client.on('message', (message) => {
this.client.on('messageCreate', (message) => {
if (this.me && this.me.id && message.author.id === this.me.id) { if (this.me && this.me.id && message.author.id === this.me.id) {
return; return;
} }
@ -231,7 +264,7 @@ class DiscordProtocol extends Protocol {
if (Array.isArray(data[0])) { if (Array.isArray(data[0])) {
try { try {
response = this.format.compose(data[0]); response = this.format.compose(data[0]);
} catch (e) { } catch (e: any) {
logger.error('[%s] Failed to compose message:', this.fullName, e.message); logger.error('[%s] Failed to compose message:', this.fullName, e.message);
return; return;
} }
@ -241,7 +274,14 @@ class DiscordProtocol extends Protocol {
response = data[0]; response = data[0];
} }
msg.data.channel.send(response).catch((e: Error) => { const toDiscord: { embeds?: object[]; content?: string } = {};
if (typeof response === 'object') {
toDiscord.embeds = [ response ];
} else {
toDiscord.content = response;
}
msg.data.channel.send(toDiscord).catch((e: Error) => {
logger.error(e); logger.error(e);
}); });
} }

1168
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,8 +11,8 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@squeebot/core": "file:../core", "@squeebot/core": "^3.3.1",
"discord.js": "^12.5.1", "discord.js": "^13.1.0",
"typescript": "^4.1.2" "typescript": "^4.4.2"
} }
} }

View File

@ -3,7 +3,7 @@
"plugins": [ "plugins": [
{ {
"name": "discord", "name": "discord",
"version": "1.2.0" "version": "1.2.1"
} }
], ],
"typescript": true "typescript": true