From 8d2c273448bbffbbb5473f821f814ee4483250bb Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Tue, 3 Nov 2020 17:46:52 +0000
Subject: [PATCH] Use intents

---
 config/config.sample.yaml |  3 +++
 config/config.schema.yaml |  2 ++
 src/clientfactory.ts      | 13 +++++++++++--
 src/config.ts             |  1 +
 4 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/config/config.sample.yaml b/config/config.sample.yaml
index b722917..11fc75e 100644
--- a/config/config.sample.yaml
+++ b/config/config.sample.yaml
@@ -38,6 +38,9 @@ auth:
   # This MUST be a string (wrapped in quotes)
   clientID: "12345"
   botToken: "foobar"
+  # You must enable "Privileged Gateway Intents" in your bot settings on discord.com (e.g.g https://discord.com/developers/applications/12345/bot)
+  # for this to work
+  usePriviledgedIntents: false
 logging:
   # What level should the logger output to the console at.
   console: "warn" #silly, verbose, info, http, warn, error, silent
diff --git a/config/config.schema.yaml b/config/config.schema.yaml
index 860f131..89fe187 100644
--- a/config/config.schema.yaml
+++ b/config/config.schema.yaml
@@ -34,6 +34,8 @@ properties:
             type: "string"
           botToken:
             type: "string"
+          usePriviledgedIntents:
+            type: "boolean"
     logging:
         type: "object"
         properties:
diff --git a/src/clientfactory.ts b/src/clientfactory.ts
index 149e562..dcf1063 100644
--- a/src/clientfactory.ts
+++ b/src/clientfactory.ts
@@ -16,7 +16,7 @@ limitations under the License.
 
 import { DiscordBridgeConfigAuth } from "./config";
 import { DiscordStore } from "./store";
-import { Client as DiscordClient, TextChannel } from "better-discord.js";
+import { Client as DiscordClient, Intents, TextChannel } from "better-discord.js";
 import { Log } from "./log";
 import { MetricPeg } from "./metrics";
 
@@ -40,8 +40,11 @@ export class DiscordClientFactory {
         // We just need to make sure we have a bearer token.
         // Create a new Bot client.
         this.botClient = new DiscordClient({
-            fetchAllMembers: true,
+            fetchAllMembers: this.config.usePriviledgedIntents,
             messageCacheLifetime: 5,
+            ws: {
+                intents: this.config.usePriviledgedIntents ? Intents.PRIVILEGED : Intents.NON_PRIVILEGED,
+            }
         });
 
         const waitPromise = new Promise((resolve, reject) => {
@@ -65,6 +68,9 @@ export class DiscordClientFactory {
         const client = new DiscordClient({
             fetchAllMembers: false,
             messageCacheLifetime: 5,
+            ws: {
+                intents: Intents.NON_PRIVILEGED,
+            }
         });
 
         await client.login(token, false);
@@ -95,6 +101,9 @@ export class DiscordClientFactory {
         const client = new DiscordClient({
             fetchAllMembers: true,
             messageCacheLifetime: 5,
+            ws: {
+                intents: Intents.NON_PRIVILEGED,
+            }
         });
 
         const jsLog = new Log("discord.js-ppt");
diff --git a/src/config.ts b/src/config.ts
index 8a77acb..dfe75e9 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -108,6 +108,7 @@ export class DiscordBridgeConfigDatabase {
 export class DiscordBridgeConfigAuth {
     public clientID: string;
     public botToken: string;
+    public usePriviledgedIntents: boolean;
 }
 
 export class DiscordBridgeConfigLogging {
-- 
GitLab