This repository has been archived on 2021-01-16. You can view files and clone it, but cannot push or open issues/pull-requests.
satyr/src/chat.ts

158 lines
4.7 KiB
TypeScript
Raw Normal View History

import * as db from "./database";
import {config} from "./config";
2020-06-27 02:36:45 -05:00
import {io} from "./http";
import * as irc from "irc";
2020-06-27 02:36:45 -05:00
import * as discord from "discord.js";
var ircClient;
var xmppClient;
var twitchClient;
var discordClient;
var liveUsers: Array<any>;
var chatIntegration: Array<any>;
async function init() {
setInterval(updateUsers, 20000);
setInterval(updateInteg, 60000);
2020-06-27 02:36:45 -05:00
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']){
2020-06-27 03:23:04 -05:00
ircClient = new irc.Client(config['chat']['irc']['server'], config['chat']['irc']['nickname'], {
userName: config['chat']['irc']['username'],
realName: config['chat']['irc']['realname'],
port: config['chat']['irc']['port'],
secure: config['chat']['irc']['tls'],
sasl: config['chat']['irc']['sasl'],
password: config['chat']['irc']['password'],
});
ircClient.addListener('error', (message) => {
console.log('IRC Client Error: ', message);
});
ircClient.once('registered', () => {
console.log("IRC Client Ready");
});
ircClient.on('message', (from, to, msg) => {
var lu = getUsr(to, 'irc');
for(var i=0;i<lu.length;i++){
sendAll(lu[i], [from, msg], "irc")
}
});
2020-06-27 02:36:45 -05:00
}
if(config['chat']['xmpp']['enabled']){
}
if(config['chat']['twitch']['enabled']){
}
}
async function updateUsers() {
liveUsers = await db.query('SELECT username FROM user_meta WHERE live=true');
return;
}
async function updateInteg() {
if(liveUsers.length === 0) {
chatIntegration = [];
return;
}
if(liveUsers.length === 1) {
2020-06-27 02:36:45 -05:00
chatIntegration = await db.query('SELECT * FROM chat_integration WHERE username='+db.raw.escape(liveUsers[0]['username']));
console.log('updated ci');
2020-06-27 03:23:04 -05:00
updateIRCChan();
return;
}
var qs: string;
for(var u in liveUsers) {
2020-06-27 02:36:45 -05:00
qs += db.raw.escape(u['username']) + " OR username=";
}
qs = qs.substring(0, qs.length - 13);
2020-06-27 02:36:45 -05:00
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);
2020-06-27 03:23:04 -05:00
updateIRCChan();
}
2020-06-27 02:36:45 -05:00
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]
2020-06-27 02:36:45 -05:00
//user string is the user whose chat is being mirrored
2020-06-27 02:36:45 -05:00
if(user === null) return;
2020-06-27 03:23:04 -05:00
if(src !== "irc") sendIRC(getCh(user, "irc"), '['+src.toUpperCase()+']'+msg[0]+': '+msg[1]);
2020-06-27 02:36:45 -05:00
//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]]);
}
2020-06-27 02:36:45 -05:00
async function sendIRC(channel: string, msg: string) {
if(!config['chat']['irc']['enabled']) return;
if(channel === null) return;
2020-06-27 03:23:04 -05:00
ircClient.say(channel, msg);
}
2020-06-27 02:36:45 -05:00
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);
}
2020-06-27 02:36:45 -05:00
async function sendXMPP(channel: string, msg: string) {
if(!config['chat']['xmpp']['enabled']) return;
if(channel === null) return;
}
2020-06-27 02:36:45 -05:00
async function sendTwitch(channel: string, msg: string) {
if(!config['chat']['twitch']['enabled']) return;
if(channel === null) return;
}
2020-06-27 02:36:45 -05:00
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});
}
2020-06-27 02:36:45 -05:00
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;
}
2020-06-27 02:36:45 -05:00
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;
}
2020-06-27 03:23:04 -05:00
async function updateIRCChan() {
var clist: Array<string> = [];
for(var i=0;i<chatIntegration.length;i++){
if(chatIntegration[i]['irc'].trim() !== "" && chatIntegration[i]['irc'] !== null) clist.push(chatIntegration[i]['irc']);
}
for(var i=0;i<clist.length;i++){
ircClient.join(clist[i]);
}
}
2020-06-27 02:36:45 -05:00
export { init, sendAll };