X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=4a61f685f4deb5443d9aa6457c50fd88c81c2b94;hp=3a776d1d70c392082dbd540c141782fe5ce0cbc6;hb=2c9f08fce443d57a70e886242ec391d89e450a7b;hpb=49dad26138d3dee0cacbb94ad8d3d3eff12c477a diff --git a/server/sockets.js b/server/sockets.js index 3a776d1d..4a61f685 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -1,7 +1,7 @@ -const url = require('url'); +const Discord = require('discord.js'); +const { token, channel } = require('./config/discord.json'); // Node version in Ubuntu 16.04 does not know about URL class -// NOTE: url is already transformed, without ?xxx=yyy... parts function getJsonFromUrl(url) { const query = url.substr(2); //starts with "/?" let result = {}; @@ -27,6 +27,13 @@ module.exports = function(wss) { // NOTE: only purpose of sidToPages = know when to delete keys in idToSid let sidToPages = {}; let idToSid = {}; + const discordClient = new Discord.Client(); + let discordChannel = null; + if (token.length > 0) { + discordClient.login(token).then( () => { + discordChannel = discordClient.channels.cache.get(channel); + }); + } wss.on("connection", (socket, req) => { const query = getJsonFromUrl(req.url); const sid = query["sid"]; @@ -212,6 +219,20 @@ module.exports = function(wss) { // "newgame" message can provide a page (corr Game --> Hall) notifyRoom( obj.page || page, obj.code, {data: obj.data}, obj.excluded); + if ( + obj.code == "newchallenge" && + !obj.data.to && //filter out targeted challenges + obj.data.cadence.indexOf('d') < 0 //and correspondance games + ) { + const challMsg = ( + "New challenge: **" + obj.data.vname + "** " + + "[" + obj.data.cadence + "]" + ); + if (!!discordChannel) discordChannel.send(challMsg); + else + // Log when running locally (dev, debug): + console.log(challMsg); + } break; case "rnewgame":