From 4ac41c44a18777ad3003abf0c51955f81e5e9f0a Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sun, 21 Mar 2021 09:23:12 +0100
Subject: [PATCH] More reliable Discord connection + show sender name when
 sending challenge

---
 client/src/views/Hall.vue |  4 +++-
 server/sockets.js         | 32 +++++++++++++++++---------------
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index c6b00d48..fe189413 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -1134,7 +1134,9 @@ export default {
           this.challenges.splice(challToDelIdx, 1);
         }
         this.send("newchallenge", {
-          data: Object.assign({ from: this.st.user.sid }, chall)
+          data: Object.assign(
+            // Temporarily add sender infos to display challenge on Discord.
+            { from: this.st.user.sid, sender: this.st.user.name }, chall)
         });
         // Add new challenge:
         chall.from = {
diff --git a/server/sockets.js b/server/sockets.js
index 4a61f685..c2b41e88 100644
--- a/server/sockets.js
+++ b/server/sockets.js
@@ -30,7 +30,8 @@ module.exports = function(wss) {
   const discordClient = new Discord.Client();
   let discordChannel = null;
   if (token.length > 0) {
-    discordClient.login(token).then( () => {
+    discordClient.login(token);
+    discordClient.once("ready", () => {
       discordChannel = discordClient.channels.cache.get(channel);
     });
   }
@@ -217,22 +218,23 @@ module.exports = function(wss) {
         case "rematchoffer":
         case "draw":
           // "newgame" message can provide a page (corr Game --> Hall)
+          if (obj.code == "newchallenge") {
+            // Filter out targeted challenges and correspondance games:
+            if (!obj.data.to && obj.data.cadence.indexOf('d') < 0) {
+              const challMsg = (
+                (obj.data.sender || "@nonymous") + " : " +
+                "**" + obj.data.vname + "** " +
+                "[" + obj.data.cadence + "] "
+              );
+              if (!!discordChannel) discordChannel.send(challMsg);
+              else
+                // Log when running locally (dev, debug):
+                console.log(challMsg);
+            }
+            delete obj.data["sender"];
+          }
           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":
-- 
2.44.0