diff --git a/lib/DiscordMessageSender.js b/lib/DiscordMessageSender.js new file mode 100644 index 0000000..25ee99b --- /dev/null +++ b/lib/DiscordMessageSender.js @@ -0,0 +1,39 @@ +module.exports = class DiscordMessageSender { + constructor(channel) { + this.channel = channel; + this.interval = setInterval(this.flush.bind(this), 3000); + } + + //async init() { + // var webhooks = await this.channel.fetchWebhooks(); + // this.webhook = webhooks.filter(w => w.token).first() || await this.channel.createWebhook("sdfadffg"); + // this.interval = setInterval(this.flush.bind(this), 3000); + //} + + + send(text) { + this.pp ||= {content: ""}; + this.pp.content += text + '\n'; + } + + sendEmbed(embed) { + this.pp ||= {}; + this.pp.embeds = [embed]; + this.flush(); + } + + flush() { + if (!this.pp) return; + var pp = this.pp; + delete this.pp; + if (pp.content.length > 2000) { + pp.files = [{name: "message.txt", file: Buffer.from(pp.content)}]; + delete pp.content; + } + //this.webhook.send(pp).catch(error => { + //handleError(error, "webhook"); + this.channel.send(pp); + //}); + } + +} \ No newline at end of file diff --git a/mppbridger.js b/mppbridger.js index 6e6fcbf..55e7e37 100644 --- a/mppbridger.js +++ b/mppbridger.js @@ -1,30 +1,10 @@ var Client = require('./lib/Client.js'); var WebSocketMessageCollector = require("./lib/datacollector"); +var DiscordMessageSender = require("./lib/DiscordMessageSender"); global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { channel = dClient.channels.resolve(channel); - var webhooks = await channel.fetchWebhooks(); - var webhook = webhooks.filter(w => w.token).first() || await channel.createWebhook("sdfadffg"); - - //todo figure out new way to buffer - function dSend(msg, options = {}) { - if (msg) options.content = msg; - - if (options.content?.length > 2000) { - options.attachments ||= []; - options.attachments.push(new Discord.MessageAttachment(Buffer.from(options.content), "message.txt")); - delete options.content; - } - - let username = gClient.channel && gClient.channel._id || room; - if (username.length > 80) username = username.substr(0,79) + '…'; - - webhook.send(Object.assign(options, {username})).catch(error => { - handleError(error, `webhook fail in ${channel.id}`); - channel.send(options).catch(error => handleError(error, `send fail in ${channel.id} after webhook send fail`)); - }); - - } + var d = new DiscordMessageSender(channel); const gClient = new Client(uri); if (uri == "wss://mppclone.com:8443") gClient.token = config.mpc_token; //todo hmm @@ -44,14 +24,14 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { handleError(error, `[${uri}][${room}]`); error = error.toString(); if (lastError != error) { - dSend(`**${error.toString()}**`); + d.send(`**${error.toString()}**`); lastError = error; } }); var isConnected = false; // TODO use gClient.isConnected() ? gClient.on('connect', () => { console.log(`[${uri}][${room}] Connected to server`); - dSend(`**Connected to server; joining channel…**`); + d.send(`**Connected to server; joining channel…**`); isConnected = true; lastError = undefined; }); @@ -65,7 +45,7 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { gClient.on('disconnect', () => { if (isConnected) { console.log(`[${uri}][${room}] Disconnected from server`); - dSend(`**Disconnected from server**`); + d.send(`**Disconnected from server**`); isConnected = false; } }); @@ -81,12 +61,12 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { gClient.on('ch', async msg => { // announce channel join if (!lastCh) { - dSend(`**Joined channel \`${msg.ch._id}\`**`); + d.send(`**Joined channel \`${msg.ch._id}\`**`); console.log(`[${uri}][${room}] Joined channel ${msg.ch._id}`); } // announce channel change else if (msg.ch._id !== lastCh) { - dSend(`**Channel changed from \`${lastCh}\` to \`${msg.ch._id}\`**`); + d.send(`**Channel changed from \`${lastCh}\` to \`${msg.ch._id}\`**`); console.log(`[${uri}][${room}] Channel changed from ${lastCh} to ${msg.ch._id}`); } lastCh = msg.ch._id; @@ -103,7 +83,7 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { var name = sanitizeName(msg.p.name); var content = msg.a; var str = `\`${id}\` **${name}:** ${content}`; - dSend(str); + d.send(str); }); // Discord to MPP @@ -140,14 +120,14 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { if (gClient.ppl[participant.id]) { // is update let oldName = gClient.ppl[participant.id].name, newName = participant.name; if (newName != oldName) - dSend(`\`${participant._id.substr(0,6)}\` ___**${sanitizeName(oldName)}** changed their name to **${sanitizeName(newName)}**___`); + d.send(`\`${participant._id.substr(0,6)}\` ___**${sanitizeName(oldName)}** changed their name to **${sanitizeName(newName)}**___`); } else { // is join - dSend(`\`${participant._id.substr(0,6)}\` ___**${sanitizeName(participant.name)}** entered the room.___`); + d.send(`\`${participant._id.substr(0,6)}\` ___**${sanitizeName(participant.name)}** entered the room.___`); } }); gClient.prependListener("bye", async msg => { var participant = gClient.ppl[msg.p]; - dSend(`\`${participant._id.substr(0,6)}\` ___**${sanitizeName(participant.name)}** left the room.___`); + d.send(`\`${participant._id.substr(0,6)}\` ___**${sanitizeName(participant.name)}** left the room.___`); }); @@ -155,10 +135,10 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { // on notifications gClient.on('notification', async msg => { // show notification - dSend(undefined, {embeds:[{ + d.sendEmbed({ title: msg.title, description: msg.text || msg.html - }]}); + }); // handle bans if (msg.text && (msg.text.startsWith('Banned from "'+room+'"') || msg.text.startsWith('Currently banned from "'+room+'"'))) { @@ -173,7 +153,7 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) { gClient.setChannel(room); gClient.start(); }, minutes*60*1000+3000); - dSend(`**Attempting to rejoin in ${minutes} minutes.**`); + d.send(`**Attempting to rejoin in ${minutes} minutes.**`); } });