Browse Source

bot: check user only in specific guilds

ghorsington 3 years ago
parent
commit
7ee88f109f
3 changed files with 25 additions and 3 deletions
  1. 16 3
      bot/src/rpc.ts
  2. 2 0
      shared/src/db/entities.ts
  3. 7 0
      shared/src/db/entity/GuildVerification.ts

+ 16 - 3
bot/src/rpc.ts

@@ -6,6 +6,8 @@ import { NoctBotService } from "@shared/rpc/backend";
 import { logger } from "./logging";
 import { client } from "./client";
 import { tryDo } from "@shared/common/async_utils";
+import { getRepository } from "typeorm";
+import { GuildVerification } from "@shared/db/entity/GuildVerification";
 
 const PORT = +(process.env.RPC_PORT ?? "8181");
 
@@ -17,9 +19,20 @@ const handler: ModuleRpcServer.ServiceHandlerFor<typeof NoctBotService> = {
     },
 
     async userInServer({ userId }): Promise<{ exists: boolean }> {
-        for (const g of client.bot.guilds.cache.values()) {
-            const res = await tryDo(g.members.fetch(userId));
-            if (res.ok) {
+        const verificationGuildRepo = getRepository(GuildVerification);
+        const guilds = await verificationGuildRepo.find({
+            select: [
+                "guildId"
+            ]
+        });
+        for (const guild of guilds) {
+            const guildInstance = await tryDo(client.bot.guilds.fetch(guild.guildId));
+            if (!guildInstance.ok) {
+                logger.error("Failed to fetch guild instance for guild %s: %s", guild.guildId, guildInstance.error);
+                continue;
+            }
+            const user = await tryDo(guildInstance.result.members.fetch(userId));
+            if (user.ok) {
                 return { exists: true };
             }
         }

+ 2 - 0
shared/src/db/entities.ts

@@ -17,6 +17,7 @@ import { RandomMessageReaction } from "./entity/RandomMesssageReaction";
 import { GuildGreeting } from "./entity/GuildGreeting";
 import { Violation, Mute, Kick, Ban } from "./entity/Violation";
 import { GuildViolationSettings } from "./entity/GuildViolationSettings";
+import { GuildVerification } from "./entity/GuildVerification";
 
 export const DB_ENTITIES = [
     AggroNewsItem,
@@ -44,4 +45,5 @@ export const DB_ENTITIES = [
     Kick,
     Ban,
     GuildViolationSettings,
+    GuildVerification,
 ];

+ 7 - 0
shared/src/db/entity/GuildVerification.ts

@@ -0,0 +1,7 @@
+import {Entity, PrimaryColumn, Column} from "typeorm";
+
+@Entity()
+export class GuildVerification {
+    @PrimaryColumn()
+    guildId: string;
+}