Browse Source

Send greet DM on join

ghorsington 4 years ago
parent
commit
d8792840de
2 changed files with 32 additions and 2 deletions
  1. 29 2
      bot/src/plugins/greet.ts
  2. 3 0
      shared/src/db/entity/GuildGreeting.ts

+ 29 - 2
bot/src/plugins/greet.ts

@@ -5,12 +5,38 @@ import { logger } from "src/logging";
 import { GuildGreeting } from "@shared/db/entity/GuildGreeting";
 import { GuildGreeting } from "@shared/db/entity/GuildGreeting";
 import { client } from "src/client";
 import { client } from "src/client";
 import { formatString } from "src/util";
 import { formatString } from "src/util";
+import { GuildVerification } from "@shared/db/entity/GuildVerification";
+import { tryDo } from "@shared/common/async_utils";
 
 
 @Plugin
 @Plugin
 export class GreetPlugin {
 export class GreetPlugin {
     @Event("guildMemberAdd")
     @Event("guildMemberAdd")
     async showGreetMessage(data: BotEventData, member: GuildMember | PartialGuildMember): Promise<void> {
     async showGreetMessage(data: BotEventData, member: GuildMember | PartialGuildMember): Promise<void> {
-        await this.displayGreet(member, g => g.onJoinMessage);
+        const greetInfo = await this.displayGreet(member, g => g.onJoinMessage);
+        if (!greetInfo || !greetInfo.onJoinPrivateMessage) {
+            return;
+        }
+
+        const verificationGuilds = getRepository(GuildVerification);
+        const guild = await verificationGuilds.findOne(member.guild.id);
+        if (!guild) {
+            return;
+        }
+
+        const dmResult = await tryDo(member.createDM());
+        if (!dmResult.ok) {
+            logger.warn("greet: failed to create DM with user %s#%s (%s): %s", member.user?.username, member.user?.discriminator, member.id, dmResult.error);
+            return;
+        }
+
+        const sendResult = await tryDo(dmResult.result.send(formatString(greetInfo.onJoinPrivateMessage, {
+            memberID: member.id,
+            memberTag: member.user?.tag ?? member.displayName,
+            guildName: member.guild.name
+        })));
+        if (!sendResult.ok) {
+            logger.warn("greet: failed to send DM greeting to %s#%s (%s): %s", member.user?.username, member.user?.discriminator, member.id, sendResult.error);
+        }
     }
     }
 
 
     @Event("guildMemberRemove")
     @Event("guildMemberRemove")
@@ -21,7 +47,7 @@ export class GreetPlugin {
     private async displayGreet(member: GuildMember | PartialGuildMember, select: (greeting: GuildGreeting) => string) {
     private async displayGreet(member: GuildMember | PartialGuildMember, select: (greeting: GuildGreeting) => string) {
         const result = await this.getGreeting(member);
         const result = await this.getGreeting(member);
         if (!result)
         if (!result)
-            return;
+            return undefined;
         const { guildGreeting, greetingChannel } = result;
         const { guildGreeting, greetingChannel } = result;
 
 
         await greetingChannel.send(formatString(select(guildGreeting), {
         await greetingChannel.send(formatString(select(guildGreeting), {
@@ -29,6 +55,7 @@ export class GreetPlugin {
             memberTag: member.user?.tag ?? member.displayName,
             memberTag: member.user?.tag ?? member.displayName,
             guildName: member.guild.name
             guildName: member.guild.name
         }));
         }));
+        return guildGreeting;
     }
     }
     
     
     private async getGreeting(member: GuildMember | PartialGuildMember) {
     private async getGreeting(member: GuildMember | PartialGuildMember) {

+ 3 - 0
shared/src/db/entity/GuildGreeting.ts

@@ -13,4 +13,7 @@ export class GuildGreeting {
 
 
     @Column({type: "text"})
     @Column({type: "text"})
     onLeaveMessage: string;
     onLeaveMessage: string;
+
+    @Column({ type: "text", nullable: true })
+    onJoinPrivateMessage?: string;
 }
 }