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

Commit

Permalink
Fix crash if messages sent before init complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTalmadge committed Jul 5, 2020
1 parent c582358 commit 8e2b858
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/guild-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class GuildContext {

public verifiedRoleId: string;

private initComplete = false;

constructor(
public guild: Discord.Guild,
public guildConfig: GuildConfig,
Expand Down Expand Up @@ -70,9 +72,11 @@ export class GuildContext {
this.guildLog("Performing startup checks...");
await HealthAssuranceService.identifyAndFixHealthIssues(this);

this.guildLog("Initializing controllers...");
this.initControllers();

this.guildLog("Initialization complete.");
this.initComplete = true;
}

private initControllers(): void {
Expand All @@ -84,6 +88,11 @@ export class GuildContext {
}

public onMessageReceived(message: Discord.Message): void {
if(!this.initComplete) {
this.guildLog(`Message ID ${message.id} from ${DiscordUtils.describeUserForLogs(message.author)} dropped because init is not complete.`);
return;
}

// Channel Controllers
if (message.channel instanceof Discord.TextChannel) {
if (message.channel.id === this.guildConfig.courseSelectionChannelId) {
Expand Down
18 changes: 15 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ export class StudyBot {
return;
}

this.guildContexts[message.guild.id].onMessageReceived(<Discord.Message>message);
try {
this.guildContexts[message.guild.id].onMessageReceived(<Discord.Message>message);
} catch(err) {
console.error(`Failed to send message ID ${message.id} to guild ID ${message.guild.id} for processing:`, err);
}
}

private static onMemberJoin(member: Discord.GuildMember | Discord.PartialGuildMember): void {
Expand All @@ -120,7 +124,11 @@ export class StudyBot {

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);
try {
this.guildContexts[member.guild.id].onMemberJoin(<Discord.GuildMember>member);
} catch(err) {
console.error(`Failed to send member ID ${member.id} join event to guild ID ${member.guild.id} for processing:`, err);
}
}

private static onMemberLeave(member: Discord.GuildMember | Discord.PartialGuildMember): void {
Expand All @@ -131,7 +139,11 @@ export class StudyBot {

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);
try {
this.guildContexts[member.guild.id].onMemberLeave(<Discord.GuildMember>member);
} catch(err) {
console.error(`Failed to send member ID ${member.id} leave event to guild ID ${member.guild.id} for processing:`, err);
}
}
}

Expand Down

0 comments on commit 8e2b858

Please sign in to comment.