Team-Silver-Sphere / SquadJS

Squad Server Script Framework
Boost Software License 1.0
163 stars 123 forks source link

Issues updating discord server status #342

Closed Sagep closed 7 months ago

Sagep commented 7 months ago

Usually runs fine for about 1/2 a day and then runs into this. Now note, this is running on a modded server. (Global Escalation). Without mods this seems to still happen however.

image

`[DiscordServerStatus][1] Generating message content for update... C:\Users\Administrator\Desktop\InvasionSquadJS\node_modules\tinygradient\index.js:198 throw new Error('Position must be between 0 and 1'); ^

Error: Position must be between 0 and 1 at computeAt (C:\Users\Administrator\Desktop\InvasionSquadJS\node_modules\tinygradient\index.js:198:15) at TinyGradient.rgbAt (C:\Users\Administrator\Desktop\InvasionSquadJS\node_modules\tinygradient\index.js:408:16) at DiscordServerStatus.generateMessage (file:///C:/Users/Administrator/Desktop/InvasionSquadJS/squad-server/plugins/discord-server-status.js:110:12) at DiscordServerStatus.updateMessages (file:///C:/Users/Administrator/Desktop/InvasionSquadJS/squad-server/plugins/discord-base-message-updater.js:151:41) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7)

Node.js v18.16.1`

Squad Information

If potentially relevant, please provide regarding the state of the Squad server at the time of error, e.g. the current layer.

System Information

mxrcode commented 7 months ago

I was getting this error, you just need to clamp the value between 0 and 1.

In discord-server-status.js file. It was like this;

   // Set gradient embed color.
    embed.setColor(
      parseInt(
        tinygradient([
          { color: '#ff0000', pos: 0 },
          { color: '#ffff00', pos: 0.5 },
          { color: '#00ff00', pos: 1 }
        ])
          .rgbAt(this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots))
          .toHex(),
        16
      )
    );

My fix:

    const ratio = this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots);
    const clampedRatio = Math.min(1, Math.max(0, ratio));

    // Set gradient embed color.
    embed.setColor(
      parseInt(
        tinygradient([
          { color: '#ff0000', pos: 0 },
          { color: '#ffff00', pos: 0.5 },
          { color: '#00ff00', pos: 1 }
        ])
          .rgbAt(clampedRatio)
          .toHex(),
        16
      )
    );