k44Eqha/ddpbridge.js

65 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-11-05 08:02:20 +01:00
var WebSocket = require('ws');
var Discord = require('discord.js');
var createWsMessageCollector = require('./datacollector');
2022-01-04 05:40:43 +01:00
var webhook = new Discord.WebhookClient({url: config.webhooks.ddp});
2018-11-05 08:02:20 +01:00
var ws;
var wasConnected = false;
//var myId;
2018-11-05 08:02:20 +01:00
var collectWsMessage = createWsMessageCollector(async function(data, startDate, endDate){
2018-12-08 08:39:30 +01:00
await webhook.send({files:[{
attachment: data,
name: `daydun piano main raw data recording from ${startDate.toISOString()} to ${endDate.toISOString()} .txt.gz`
}]});
});
2018-11-05 08:02:20 +01:00
(function connect() {
ws = new WebSocket("wss://daydun.com:5012/?nick=%5Bdiscord.gg%2Fk44Eqha%5D");
ws.on("open", () => {
if (!wasConnected) send2discord("**Connected**");
wasConnected = true;
});
ws.on("message", message => {
collectWsMessage(message);
if (typeof message != 'string') return;
var transmission = JSON.parse(message);
2018-11-05 08:02:20 +01:00
if (transmission.type == 'chat') {
let chatmsg = transmission.message;
if (chatmsg.type == "message") {
//if (chatmsg.id != myId)
if (!chatmsg.content.startsWith('\u034f'))
send2discord(`**${sanitizeName(chatmsg.user.nick)}:** ${escapeDiscordMentions(chatmsg.content)}`);
2018-11-05 08:02:20 +01:00
} else if (chatmsg.type == "join") {
send2discord(`__***${sanitizeName(chatmsg.nick || chatmsg.id)} joined.***__`);
2018-11-05 08:02:20 +01:00
} else if (chatmsg.type == "leave") {
send2discord(`__***${sanitizeName(chatmsg.nick || chatmsg.id)} left.***__`);
2018-11-05 08:02:20 +01:00
}
} /*else if (transmission.type == 'load') {
2018-11-06 09:24:10 +01:00
myId = transmission.id;
}*/
2018-11-05 08:02:20 +01:00
});
2022-01-04 05:40:43 +01:00
ws.on("error", error => handleError(error));
2018-11-05 08:02:20 +01:00
ws.on("close", () => {
if (wasConnected) send2discord("**Disconnected**");
wasConnected = false;
setTimeout(connect, 5000);
});
})();
function send2discord(message) {
2022-01-04 05:40:43 +01:00
webhook.send(message.substring(0,2000));
2018-11-05 08:02:20 +01:00
}
function send2ddp(message) {
2018-11-06 08:53:00 +01:00
if (ws.readyState == WebSocket.OPEN) ws.send(JSON.stringify({type:"chat",message}));
2018-11-05 08:02:20 +01:00
}
2022-01-04 05:40:43 +01:00
dClient.on("messageCreate", message => {
2018-11-05 08:32:05 +01:00
if (message.channel.id != "508890674138054667" || message.author.bot) return;
2018-11-07 22:18:53 +01:00
var x = message.cleanContent;
2022-01-04 05:40:43 +01:00
if (message.attachments.size > 0) x += ' ' + message.attachments.map(a => a.url).join(' ');
2018-11-07 22:18:53 +01:00
send2ddp(`\u034f${message.member.displayName}#${message.author.discriminator}: ${x}`);
2018-11-05 08:02:20 +01:00
});