i try new discord sender
This commit is contained in:
parent
fa1c39790a
commit
35d1299897
|
@ -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);
|
||||||
|
//});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,30 +1,10 @@
|
||||||
var Client = require('./lib/Client.js');
|
var Client = require('./lib/Client.js');
|
||||||
var WebSocketMessageCollector = require("./lib/datacollector");
|
var WebSocketMessageCollector = require("./lib/datacollector");
|
||||||
|
var DiscordMessageSender = require("./lib/DiscordMessageSender");
|
||||||
|
|
||||||
global.createMPPbridge = async function createMPPbridge({room, channel, uri}) {
|
global.createMPPbridge = async function createMPPbridge({room, channel, uri}) {
|
||||||
channel = dClient.channels.resolve(channel);
|
channel = dClient.channels.resolve(channel);
|
||||||
var webhooks = await channel.fetchWebhooks();
|
var d = new DiscordMessageSender(channel);
|
||||||
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`));
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const gClient = new Client(uri);
|
const gClient = new Client(uri);
|
||||||
if (uri == "wss://mppclone.com:8443") gClient.token = config.mpc_token; //todo hmm
|
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}]`);
|
handleError(error, `[${uri}][${room}]`);
|
||||||
error = error.toString();
|
error = error.toString();
|
||||||
if (lastError != error) {
|
if (lastError != error) {
|
||||||
dSend(`**${error.toString()}**`);
|
d.send(`**${error.toString()}**`);
|
||||||
lastError = error;
|
lastError = error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var isConnected = false; // TODO use gClient.isConnected() ?
|
var isConnected = false; // TODO use gClient.isConnected() ?
|
||||||
gClient.on('connect', () => {
|
gClient.on('connect', () => {
|
||||||
console.log(`[${uri}][${room}] Connected to server`);
|
console.log(`[${uri}][${room}] Connected to server`);
|
||||||
dSend(`**Connected to server; joining channel…**`);
|
d.send(`**Connected to server; joining channel…**`);
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
lastError = undefined;
|
lastError = undefined;
|
||||||
});
|
});
|
||||||
|
@ -65,7 +45,7 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) {
|
||||||
gClient.on('disconnect', () => {
|
gClient.on('disconnect', () => {
|
||||||
if (isConnected) {
|
if (isConnected) {
|
||||||
console.log(`[${uri}][${room}] Disconnected from server`);
|
console.log(`[${uri}][${room}] Disconnected from server`);
|
||||||
dSend(`**Disconnected from server**`);
|
d.send(`**Disconnected from server**`);
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -81,12 +61,12 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) {
|
||||||
gClient.on('ch', async msg => {
|
gClient.on('ch', async msg => {
|
||||||
// announce channel join
|
// announce channel join
|
||||||
if (!lastCh) {
|
if (!lastCh) {
|
||||||
dSend(`**Joined channel \`${msg.ch._id}\`**`);
|
d.send(`**Joined channel \`${msg.ch._id}\`**`);
|
||||||
console.log(`[${uri}][${room}] Joined channel ${msg.ch._id}`);
|
console.log(`[${uri}][${room}] Joined channel ${msg.ch._id}`);
|
||||||
}
|
}
|
||||||
// announce channel change
|
// announce channel change
|
||||||
else if (msg.ch._id !== lastCh) {
|
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}`);
|
console.log(`[${uri}][${room}] Channel changed from ${lastCh} to ${msg.ch._id}`);
|
||||||
}
|
}
|
||||||
lastCh = 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 name = sanitizeName(msg.p.name);
|
||||||
var content = msg.a;
|
var content = msg.a;
|
||||||
var str = `\`${id}\` **${name}:** ${content}`;
|
var str = `\`${id}\` **${name}:** ${content}`;
|
||||||
dSend(str);
|
d.send(str);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Discord to MPP
|
// Discord to MPP
|
||||||
|
@ -140,14 +120,14 @@ global.createMPPbridge = async function createMPPbridge({room, channel, uri}) {
|
||||||
if (gClient.ppl[participant.id]) { // is update
|
if (gClient.ppl[participant.id]) { // is update
|
||||||
let oldName = gClient.ppl[participant.id].name, newName = participant.name;
|
let oldName = gClient.ppl[participant.id].name, newName = participant.name;
|
||||||
if (newName != oldName)
|
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
|
} 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 => {
|
gClient.prependListener("bye", async msg => {
|
||||||
var participant = gClient.ppl[msg.p];
|
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
|
// on notifications
|
||||||
gClient.on('notification', async msg => {
|
gClient.on('notification', async msg => {
|
||||||
// show notification
|
// show notification
|
||||||
dSend(undefined, {embeds:[{
|
d.sendEmbed({
|
||||||
title: msg.title,
|
title: msg.title,
|
||||||
description: msg.text || msg.html
|
description: msg.text || msg.html
|
||||||
}]});
|
});
|
||||||
|
|
||||||
// handle bans
|
// handle bans
|
||||||
if (msg.text && (msg.text.startsWith('Banned from "'+room+'"') || msg.text.startsWith('Currently banned from "'+room+'"'))) {
|
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.setChannel(room);
|
||||||
gClient.start();
|
gClient.start();
|
||||||
}, minutes*60*1000+3000);
|
}, minutes*60*1000+3000);
|
||||||
dSend(`**Attempting to rejoin in ${minutes} minutes.**`);
|
d.send(`**Attempting to rejoin in ${minutes} minutes.**`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue