Working discord chat integration

merge-requests/8/head
knotteye 2020-06-27 02:36:45 -05:00
parent c1debc9173
commit 1ef736ca17
5 changed files with 219 additions and 1704 deletions

View File

@ -41,6 +41,9 @@ chat:
discord:
enabled: false
token:
#server name is optional, will relay from messages from all matching channels in all servers
#if not specified
server:
xmpp:
enabled: false

1816
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,13 +18,14 @@
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.4",
"dirty": "^1.1.0",
"discord.js": "^11.6.4",
"express": "^4.17.1",
"flags": "^0.1.3",
"irc": "^0.5.2",
"jose": "^1.15.1",
"mysql": "^2.17.1",
"node-media-server": ">=2.1.3 <3.0.0",
"nunjucks": "^3.2.0",
"nunjucks": "^3.2.1",
"parse-yaml": "^0.1.0",
"recursive-readdir": "^2.2.2",
"socket-anti-spam": "^2.0.0",

View File

@ -1,6 +1,8 @@
import * as db from "./database";
import {config} from "./config";
import {io} from "./http";
import * as irc from "irc";
import * as discord from "discord.js";
var ircClient;
var xmppClient;
@ -12,6 +14,27 @@ var chatIntegration: Array<any>;
async function init() {
setInterval(updateUsers, 20000);
setInterval(updateInteg, 60000);
if(config['chat']['discord']['enabled']){
discordClient = new discord.Client();
discordClient.once('ready', ()=>{ console.log('Discord bot ready')});
discordClient.on('message', (msg) => {
if(msg['author']['bot']) return;
var lu = getUsr(msg['channel']['name'], 'discord')
for(var i=0;i<lu.length;i++){
sendAll(lu[i], [msg['author']['username'], msg['content']], "discord")
}
});
discordClient.login(config['chat']['discord']['token']);
}
if(config['chat']['irc']['enabled']){
}
if(config['chat']['xmpp']['enabled']){
}
if(config['chat']['twitch']['enabled']){
}
}
async function updateUsers() {
@ -25,39 +48,79 @@ async function updateInteg() {
return;
}
if(liveUsers.length === 1) {
chatIntegration = await db.query('SELECT * FROM chat_integration WHERE username=');
chatIntegration = await db.query('SELECT * FROM chat_integration WHERE username='+db.raw.escape(liveUsers[0]['username']));
console.log('updated ci');
return;
}
var qs: string;
for(var u in liveUsers) {
qs += u['username'] + " OR username=";
qs += db.raw.escape(u['username']) + " OR username=";
}
qs = qs.substring(0, qs.length - 13);
chatIntegration = await db.query('SELECT * FROM chat_integration WHERE username='+db.raw.escape(qs));
console.log('SELECT * FROM chat_integration WHERE username='+qs);
chatIntegration = await db.query('SELECT * FROM chat_integration WHERE username='+qs);
console.log('updated integrations');
console.log(chatIntegration);
}
async function sendAll(user, msg, src) {
async function sendAll(user: string, msg: Array<string>, src: string) {
//msg should be an array containing first the username of the user who sent the message
//followed by the message text
//[sender, message]
//user string is the user whose chat is being mirrored
if(user === null) return;
//if(src !== "irc") sendIRC();
//if(src !== "twitch") sendTwitch();
if(src !== "discord") sendDiscord(getCh(user, "discord"), '['+src.toUpperCase()+']'+msg[0]+': '+msg[1]);
//if(src !== "xmpp") sendXMPP();
if(src !== "web") sendWeb(user, ['['+src.toUpperCase()+']'+msg[0], msg[1]]);
}
async function sendIRC(channel, msg) {
async function sendIRC(channel: string, msg: string) {
if(!config['chat']['irc']['enabled']) return;
if(channel === null) return;
}
async function sendDiscord(channel, msg) {
async function sendDiscord(channel: string, msg: string) {
if(!config['chat']['discord']['enabled']) return;
if(channel === null) return;
var ch = discordClient.channels.find('name', channel);
ch.send(msg);
}
async function sendXMPP(channel, msg) {
async function sendXMPP(channel: string, msg: string) {
if(!config['chat']['xmpp']['enabled']) return;
if(channel === null) return;
}
async function sendTwitch(channel, msg) {
async function sendTwitch(channel: string, msg: string) {
if(!config['chat']['twitch']['enabled']) return;
if(channel === null) return;
}
async function sendWeb(channel, msg) {
async function sendWeb(channel: string, msg: Array<string>) {
if(channel === null) return;
io.to(channel).emit('MSG', {nick: msg[0], msg: msg[1], room: channel});
}
export { init };
function getCh(usr: string, itype: string): string{
for(var i=0;i<chatIntegration.length;i++){
if(chatIntegration[i]['username'] === usr){
if(chatIntegration[i][itype].trim() !== "" && chatIntegration[i][itype] !== null) return chatIntegration[i][itype];
}
}
return null;
}
function getUsr(channel: string, ctype: string): Array<string>{
var list: Array<string> = [];
for(var i=0;i<chatIntegration.length;i++){
if(chatIntegration[i][ctype] === channel) list.push(chatIntegration[i]['username']);
}
return list;
}
export { init, sendAll };

View File

@ -8,6 +8,7 @@ import * as dirty from "dirty";
import * as socketSpam from "socket-anti-spam";
import * as api from "./api";
import * as db from "./database";
import * as chatInteg from "./chat";
import { config } from "./config";
import { readdir, readFileSync, writeFileSync } from "fs";
import { JWT, JWK } from "jose";
@ -450,6 +451,7 @@ async function initChat() {
socket.on('MSG', (data: object) => {
if(data['msg'] === "" || !data['msg'].replace(/\s/g, '').length) return;
if(socket.rooms[data['room']]) io.to(data['room']).emit('MSG', {nick: socket.nick, msg: data['msg'], room: data['room']});
chatInteg.sendAll(data['room'], [socket.nick, data['msg']], "web");
});
socket.on('KICK', (data) => {
if(socket.nick === data.room){
@ -539,4 +541,4 @@ async function initChat() {
});
}
export { init };
export { init, io };