Explorar el Código

Re-add "The One" discord role

Clément Wolf hace 6 meses
padre
commit
326bd507e4
Se han modificado 6 ficheros con 48 adiciones y 7 borrados
  1. 1 1
      RUNNING.md
  2. 1 0
      config.json.example
  3. 36 0
      discord_updates.js
  4. 1 1
      elo.js
  5. 9 3
      elo_cache.js
  6. 0 2
      util/export_s1_ranks.js

+ 1 - 1
RUNNING.md

@@ -49,7 +49,7 @@ Try running the bot with `yarn start` and see if it connects successfully. You s
 
 * [Create a new Discord application](https://discord.com/developers/applications) and add the bot id and token to `config.json`
 
-* Create a Discord server with a welcome channel and a verified role, and add their IDs to `config.json` by using Right Click -> Copy ID on the server name, channel, and role
+* Create a Discord server with a welcome channel, a verified role and a "The One" role, and add their IDs to `config.json` by using Right Click -> Copy ID on the server name, channel, and role
 
 * Invite the bot to your Discord server ([invite generator](https://discordapi.com/permissions.html))
 

+ 1 - 0
config.json.example

@@ -17,6 +17,7 @@
     "discord_guild_id": "891781932067749948",
     "discord_welcome_channel_id": "892880734526795826",
     "discord_linked_account_role_id": "909777665223966750",
+    "discord_the_one_role_id": "1173532167259836486",
 
     "HOST_WEBSITE": true,
     "domain_name": "osudev.local",

+ 36 - 0
discord_updates.js

@@ -42,8 +42,44 @@ async function sync_discord_info(user, sync_reason) {
   }
 }
 
+async function update_the_one_discord_role(user, is_the_one) {
+  if (!guild) return;
+  if (!user.discord_user_id) return;
+
+  let member;
+  try {
+    member = await guild.members.fetch(user.discord_user_id);
+  } catch (err) {
+    // User left the server; we don't remove them from the database as they could re-join anytime
+    if (err.message == 'Unknown Member') {
+      return;
+    }
+
+    console.error(`[Discord] Failed to fetch '${user.username}' <@${user.discord_user_id}>: ${err}`);
+    capture_sentry_exception(err);
+    return;
+  }
+
+  if (is_the_one) {
+    try {
+      await member.roles.add(Config.discord_the_one_role_id);
+    } catch (err) {
+      console.error(`[Discord] Failed to add The One role to '${user.username}' <@${user.discord_user_id}>: ${err}`);
+      capture_sentry_exception(err);
+    }
+  } else {
+    try {
+      await member.roles.remove(Config.discord_the_one_role_id);
+    } catch (err) {
+      console.error(`[Discord] Failed to remove The One role from '${user.username}' <@${user.discord_user_id}>: ${err}`);
+      capture_sentry_exception(err);
+    }
+  }
+}
+
 
 export {
   init,
   sync_discord_info,
+  update_the_one_discord_role,
 };

+ 1 - 1
elo.js

@@ -170,7 +170,7 @@ async function save_game_and_update_rating(lobby, game) {
     'The One',
   ];
   for (const score of game.scores) {
-    update_the_one(score.user_id, score.rating.elo, game.mode_int);
+    await update_the_one(score.user_id, score.rating.elo, game.mode_int);
   }
 
   const rank_changes = [];

+ 9 - 3
elo_cache.js

@@ -1,5 +1,6 @@
 import db from './database.js';
 import Config from './util/config.js';
+import {update_the_one_discord_role} from './discord_updates.js';
 
 
 const the_ones = [];
@@ -22,7 +23,7 @@ const divisions = [
   'The One',
 ];
 
-function update_the_one(user_id, elo, mode) {
+async function update_the_one(user_id, elo, mode) {
   // Check that "The One" is not deranking after a loss
   if (user_id == the_ones[mode].user_id) {
     const the_one = db.prepare('SELECT user_id, elo FROM rating WHERE s3_scores >= ? AND mode = ? ORDER BY elo DESC LIMIT 1').get(Config.games_needed_for_rank, mode);
@@ -32,19 +33,24 @@ function update_the_one(user_id, elo, mode) {
       elo: the_one.elo,
     };
 
-    // TODO: update discord role
+    if (the_one.user_id != user_id) {
+      await update_the_one_discord_role(user_id, false);
+      await update_the_one_discord_role(the_one.user_id, true);
+    }
 
     return;
   }
 
   if (elo > the_ones[mode].elo) {
+    const the_previous_one = the_ones[mode].user_id;
     division_floors[mode][divisions.length - 1] = elo;
     the_ones[mode] = {
       user_id: user_id,
       elo: elo,
     };
 
-    // TODO: update discord role
+    await update_the_one_discord_role(the_previous_one, false);
+    await update_the_one_discord_role(user_id, true);
   }
 }
 

+ 0 - 2
util/export_s1_ranks.js

@@ -1,7 +1,5 @@
 // Checkout commit de1fdc341b09addeabd719e7a058e3ad449398bc and run on old ranks.db backup
 
-import fs from 'fs';
-
 import databases from '../database.js';
 
 const RANK_DIVISIONS = [