Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Prevent crash on private message.
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTalmadge committed Jul 5, 2020
1 parent cf1606b commit c582358
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/controllers/channel/channel-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export abstract class ChannelController {
constructor(protected guildContext: GuildContext) {
}

public abstract async onMessageReceived(message: Discord.Message | Discord.PartialMessage): Promise<void>;
public abstract async onMessageReceived(message: Discord.Message): Promise<void>;
}
4 changes: 2 additions & 2 deletions src/controllers/channel/course-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
public async onMessageReceived(message: Discord.Message): Promise<void> {
if (message.content.toLowerCase().startsWith("join")) {
this.joinOrLeaveCourses(message, "join")
.then(result => {
Expand Down Expand Up @@ -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!`);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/channel/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class VerificationChannelController extends ChannelController {
}
}

public async onMessageReceived(message: Discord.Message | Discord.PartialMessage): Promise<void> {
public async onMessageReceived(message: Discord.Message): Promise<void> {
if(!this.enabled) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/command/command-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export abstract class CommandController {

}

public abstract onMessageReceived(message: Discord.Message | Discord.PartialMessage): void;
public abstract onMessageReceived(message: Discord.Message): void;
}
2 changes: 1 addition & 1 deletion src/controllers/command/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/command/moderator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/guild-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 17 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Discord.Message>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(<Discord.GuildMember>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(<Discord.GuildMember>member);
}
Expand Down
1 change: 0 additions & 1 deletion src/services/implement/major/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/utils/discord-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit c582358

Please sign in to comment.