-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Analyzers were reworked into DirectMessageProcessors. Any time that P…
…hil receives a direct message, it is sent to a DirectMessageDispatcher, which finds the first processor that is willing to accept it. That processor is run, and then that's the end of processing that message. If no processors are active or willing to accept the message, then the message is discarded without action being taken. Direct messages will not be processed as commands any longer. Fixed a bug where SubmissionSession.getActiveSession was not returning the correct results because it wasn't taking into account the time portion of NOW, just the date portion.
- Loading branch information
Showing
12 changed files
with
148 additions
and
142 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Phil } from '../phil/phil'; | ||
import { DiscordMessage } from '../phil/discord-message'; | ||
|
||
export interface IProcessorActiveToken { // TODO: come up with a better name for this | ||
readonly isActive : boolean; | ||
} | ||
|
||
export interface DirectMessageProcessor { | ||
readonly handle : string; | ||
canProcess(phil : Phil, message : DiscordMessage) : Promise<IProcessorActiveToken>; | ||
process(phil : Phil, message : DiscordMessage, token : IProcessorActiveToken) : Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DirectMessageProcessor, IProcessorActiveToken } from './@base'; | ||
import { Phil } from '../phil/phil'; | ||
import { DiscordMessage } from '../phil/discord-message'; | ||
import { TimezoneQuestionnaire } from '../phil/timezone-questionnaire'; | ||
|
||
interface TimezoneQuestionnaireToken extends IProcessorActiveToken { | ||
readonly currentStage? : TimezoneQuestionnaire.Stage; | ||
} | ||
|
||
export class TimezoneQuestionnaireProcessor implements DirectMessageProcessor { | ||
readonly handle = 'timezone-questionnaire'; | ||
|
||
async canProcess(phil : Phil, message : DiscordMessage) : Promise<TimezoneQuestionnaireToken> { | ||
const currentStage = await TimezoneQuestionnaire.getStageForUser(phil.db, message.userId); | ||
if (!currentStage) { | ||
return { | ||
isActive: false | ||
}; | ||
} | ||
|
||
if (!TimezoneQuestionnaire.isCurrentlyDoingQuestionnaire(currentStage.stage)) { | ||
return { | ||
isActive: false | ||
}; | ||
} | ||
|
||
return { | ||
isActive: true, | ||
currentStage: currentStage | ||
} | ||
} | ||
|
||
async process(phil : Phil, message : DiscordMessage, rawToken : IProcessorActiveToken) { | ||
const token = rawToken as TimezoneQuestionnaireToken; | ||
token.currentStage.processInput(phil, message); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DirectMessageProcessor, IProcessorActiveToken } from '../direct-message-processors/@base'; | ||
import { TimezoneQuestionnaireProcessor } from '../direct-message-processors/timezone-questionnaire'; | ||
|
||
import { Phil } from './phil'; | ||
import { DiscordMessage } from './discord-message'; | ||
import { DiscordPromises } from '../promises/discord'; | ||
import { ServerConfig } from './server-config'; | ||
const util = require('util'); | ||
|
||
export class DirectMessageDispatcher { | ||
private readonly processorsInPriorityOrder : DirectMessageProcessor[] = [ | ||
new TimezoneQuestionnaireProcessor() | ||
]; | ||
|
||
constructor(private readonly phil : Phil) { | ||
} | ||
|
||
async process(message : DiscordMessage) { | ||
for (let processor of this.processorsInPriorityOrder) { | ||
try { | ||
const token = await processor.canProcess(this.phil, message); | ||
if (token.isActive) { | ||
await processor.process(this.phil, message, token); | ||
return; | ||
} | ||
|
||
} catch (err) { | ||
this.reportError(err, message.serverConfig, processor); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
private reportError(err : Error, serverConfig : ServerConfig, processor : DirectMessageProcessor) { | ||
console.error(err); | ||
|
||
if (typeof(err) !== 'string') { | ||
err = util.inspect(err); | ||
} | ||
|
||
DiscordPromises.sendEmbedMessage(this.phil.bot, serverConfig.botControlChannel.id, { | ||
color: 0xCD5555, | ||
title: ':no_entry: Processor Error', | ||
description: err, | ||
footer: { | ||
text: 'processor: ' + processor.handle | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.