From c582358e0ca33f169f30aed9a44799e502aef113 Mon Sep 17 00:00:00 2001 From: Mitch Talmadge Date: Sat, 4 Jul 2020 18:39:06 -0600 Subject: [PATCH] Prevent crash on private message. --- src/controllers/channel/channel-controller.ts | 2 +- src/controllers/channel/course-selection.ts | 4 ++-- src/controllers/channel/verification.ts | 2 +- src/controllers/command/command-controller.ts | 2 +- src/controllers/command/dev.ts | 2 +- src/controllers/command/moderator.ts | 2 +- src/guild-context.ts | 2 +- src/main.ts | 24 +++++++++++++------ src/services/implement/major/implement.ts | 1 - src/utils/discord-message.ts | 2 +- 10 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/controllers/channel/channel-controller.ts b/src/controllers/channel/channel-controller.ts index f74b692..2fb4434 100644 --- a/src/controllers/channel/channel-controller.ts +++ b/src/controllers/channel/channel-controller.ts @@ -5,5 +5,5 @@ export abstract class ChannelController { constructor(protected guildContext: GuildContext) { } - public abstract async onMessageReceived(message: Discord.Message | Discord.PartialMessage): Promise; + public abstract async onMessageReceived(message: Discord.Message): Promise; } \ No newline at end of file diff --git a/src/controllers/channel/course-selection.ts b/src/controllers/channel/course-selection.ts index 682ee24..4d46a5c 100644 --- a/src/controllers/channel/course-selection.ts +++ b/src/controllers/channel/course-selection.ts @@ -11,7 +11,7 @@ import { ChannelController } from "./channel-controller"; export class CourseSelectionChannelController extends ChannelController { // TODO: async-ify - public async onMessageReceived(message: Discord.Message | Discord.PartialMessage): Promise { + public async onMessageReceived(message: Discord.Message): Promise { if (message.content.toLowerCase().startsWith("join")) { this.joinOrLeaveCourses(message, "join") .then(result => { @@ -65,7 +65,7 @@ export class CourseSelectionChannelController extends ChannelController { } // TODO: async-ify - private joinOrLeaveCourses(message: Discord.Message | Discord.PartialMessage, action: "join" | "leave"): Promise<{validCourses: Course[], invalidCourseNames: string[]}> { + private joinOrLeaveCourses(message: Discord.Message, action: "join" | "leave"): Promise<{validCourses: Course[], invalidCourseNames: string[]}> { const separatorIndex = message.content.indexOf(" "); if(separatorIndex === -1) { return Promise.reject(`${message.author}, I didn't see any course numbers in your request!`); diff --git a/src/controllers/channel/verification.ts b/src/controllers/channel/verification.ts index e4ebe62..9afc1ed 100644 --- a/src/controllers/channel/verification.ts +++ b/src/controllers/channel/verification.ts @@ -29,7 +29,7 @@ export class VerificationChannelController extends ChannelController { } } - public async onMessageReceived(message: Discord.Message | Discord.PartialMessage): Promise { + public async onMessageReceived(message: Discord.Message): Promise { if(!this.enabled) { return; } diff --git a/src/controllers/command/command-controller.ts b/src/controllers/command/command-controller.ts index f69d7b4..f5c94b4 100644 --- a/src/controllers/command/command-controller.ts +++ b/src/controllers/command/command-controller.ts @@ -6,5 +6,5 @@ export abstract class CommandController { } - public abstract onMessageReceived(message: Discord.Message | Discord.PartialMessage): void; + public abstract onMessageReceived(message: Discord.Message): void; } \ No newline at end of file diff --git a/src/controllers/command/dev.ts b/src/controllers/command/dev.ts index accc73f..309d8da 100644 --- a/src/controllers/command/dev.ts +++ b/src/controllers/command/dev.ts @@ -4,7 +4,7 @@ import { EmailService } from "services/email"; import { CommandController } from "./command-controller"; export class DevCommandController extends CommandController { - public onMessageReceived(message: Discord.Message | Discord.PartialMessage): void { + public onMessageReceived(message: Discord.Message): void { if (process.env.NODE_ENV !== "development") return; diff --git a/src/controllers/command/moderator.ts b/src/controllers/command/moderator.ts index d8e5110..118c554 100644 --- a/src/controllers/command/moderator.ts +++ b/src/controllers/command/moderator.ts @@ -10,7 +10,7 @@ import { VerifierServiceFactory } from "services/verification/verifier-factory"; import { CommandController } from "./command-controller"; export class ModeratorCommandController extends CommandController { - public onMessageReceived(message: Discord.Message | Discord.PartialMessage): void { + public onMessageReceived(message: Discord.Message): void { if(message.channel.id !== this.guildContext.guildConfig.moderatorCommandChannelId) return; diff --git a/src/guild-context.ts b/src/guild-context.ts index 44ba678..4702422 100644 --- a/src/guild-context.ts +++ b/src/guild-context.ts @@ -83,7 +83,7 @@ export class GuildContext { this.commandControllers.push(new ModeratorCommandController(this)); } - public onMessageReceived(message: Discord.Message | Discord.PartialMessage): void { + public onMessageReceived(message: Discord.Message): void { // Channel Controllers if (message.channel instanceof Discord.TextChannel) { if (message.channel.id === this.guildConfig.courseSelectionChannelId) { diff --git a/src/main.ts b/src/main.ts index 3144bec..2fe5b5f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -98,28 +98,38 @@ export class StudyBot { private static onMessageReceived(message: Discord.Message | Discord.PartialMessage): void { if (message.author.id === this.client.user.id) return; + if(message.partial) { + console.log(`Dropping partial message with ID ${message.id}.`); + return; + } + + // Handle private messages + if(message.channel.type === "dm") { + message.reply("Hey there! Messages here don't get read. Please use the guild channels to join courses or do other things :) If you need help, PM a moderator! Have a good day!"); + return; + } - this.guildContexts[message.guild.id].onMessageReceived(message); + this.guildContexts[message.guild.id].onMessageReceived(message); } private static onMemberJoin(member: Discord.GuildMember | Discord.PartialGuildMember): void { - const guildContext = this.guildContexts[member.guild.id]; if(member.partial) { - guildContext.guildDebug(`Skipping partial member join for ${DiscordUtils.describeUserForLogs(member.user)}`); + console.log(`Dropping partial member join for member ID ${member.id}.`); return; } - + + const guildContext = this.guildContexts[member.guild.id]; guildContext.guildLog(`Member ${DiscordUtils.describeUserForLogs(member.user)} joined the guild.`); this.guildContexts[member.guild.id].onMemberJoin(member); } private static onMemberLeave(member: Discord.GuildMember | Discord.PartialGuildMember): void { - const guildContext = this.guildContexts[member.guild.id]; if(member.partial) { - guildContext.guildDebug(`Skipping partial member leave for ${DiscordUtils.describeUserForLogs(member.user)}`); + console.log(`Dropping partial member leave for member ID ${member.id}`); return; } - + + const guildContext = this.guildContexts[member.guild.id]; guildContext.guildLog(`Member ${DiscordUtils.describeUserForLogs(member.user)} left the guild.`); this.guildContexts[member.guild.id].onMemberLeave(member); } diff --git a/src/services/implement/major/implement.ts b/src/services/implement/major/implement.ts index 9065f42..35a5edb 100644 --- a/src/services/implement/major/implement.ts +++ b/src/services/implement/major/implement.ts @@ -4,7 +4,6 @@ import { GuildContext } from "guild-context"; import { CourseImplementChannelType, ICourseImplement } from "models/implement/course"; import { IMajorImplement } from "models/implement/major"; import { Major } from "models/major"; -import { CourseService } from "services/course"; import { GuildStorageDatabaseService } from "services/database/guild-storage"; import { DiscordUtils } from "utils/discord"; diff --git a/src/utils/discord-message.ts b/src/utils/discord-message.ts index 463875f..e38dcd1 100644 --- a/src/utils/discord-message.ts +++ b/src/utils/discord-message.ts @@ -7,7 +7,7 @@ export class DiscordMessageUtils { * @param message The message to delete. * @param delayMs How long to wait before deleting the message. */ - public static purgeMessage(message: Discord.Message | Discord.PartialMessage, delayMs: number = 0): void { + public static purgeMessage(message: Discord.Message, delayMs: number = 0): void { message.delete({ timeout: delayMs, reason: "StudyBot automatic message purge."