More reliable Discord connection + show sender name when sending challenge
authorBenjamin Auder <benjamin.auder@somewhere>
Sun, 21 Mar 2021 08:23:12 +0000 (09:23 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Sun, 21 Mar 2021 08:23:12 +0000 (09:23 +0100)
client/src/views/Hall.vue
server/sockets.js

index c6b00d4..fe18941 100644 (file)
@@ -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 = {
index 4a61f68..c2b41e8 100644 (file)
@@ -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":