-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some caching stuff, better example of statefulCommands, need to a…
…dd more docs, also fixed tests
- Loading branch information
1 parent
0c04e02
commit 916f66b
Showing
7 changed files
with
159 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Message } from "discord.js"; | ||
import { inject, injectable } from "tsyringe"; | ||
import { | ||
CachingService, | ||
GuildAndMemberScopedIndex, | ||
GuildMemberAndChannelScopedIndex, | ||
Index, | ||
PersistentCachingService | ||
} from "../services"; | ||
import { StatefulCommand } from "./statefulCommand"; | ||
|
||
interface State { | ||
step: Step; | ||
firstName: string; | ||
} | ||
|
||
interface PersistentState { | ||
nameSet: boolean; | ||
name: string; | ||
} | ||
|
||
@injectable() | ||
export class SimpleMultipartExample extends StatefulCommand<State, PersistentState> { | ||
constructor( | ||
@inject("CachingService") cachingService: CachingService, | ||
@inject("PersistentCachingService") persistentCachingService: PersistentCachingService, | ||
@inject(GuildAndMemberScopedIndex) private readonly guildAndMemberScopedIndex: Index, | ||
@inject(GuildMemberAndChannelScopedIndex) private readonly guildMemberAndChannelScopedIndex: Index | ||
) { | ||
super( | ||
cachingService, | ||
persistentCachingService, | ||
{ step: Step.None, firstName: "" }, | ||
{ nameSet: false, name: "" } | ||
); | ||
this.guildAndMemberScopedIndex = this.guildAndMemberScopedIndex.addScope("SimpleMultipartExample"); | ||
this.guildMemberAndChannelScopedIndex = this.guildMemberAndChannelScopedIndex.addScope( | ||
"SimpleMultipartExample" | ||
); | ||
} | ||
async check(message: Message): Promise<boolean> { | ||
return ( | ||
/^(sayName|setName)$/i.test(message.content) || | ||
(await this.getState(this.guildMemberAndChannelScopedIndex)).step !== Step.None | ||
); | ||
} | ||
async execute(message: Message): Promise<void> { | ||
const currentState = await this.getState(this.guildMemberAndChannelScopedIndex); | ||
if (currentState.step === Step.SetFirstName) { | ||
await this.setState(this.guildMemberAndChannelScopedIndex, { | ||
firstName: message.content, | ||
step: Step.SetLastName | ||
}); | ||
await message.channel.send("Enter last name"); | ||
return; | ||
} | ||
if (currentState.step === Step.SetLastName) { | ||
await this.setState(this.guildMemberAndChannelScopedIndex, { | ||
step: Step.None | ||
}); | ||
const state = await this.getState(this.guildMemberAndChannelScopedIndex); | ||
await this.setPersistentState(this.guildAndMemberScopedIndex, { | ||
name: `${state.firstName} ${message.content}`, | ||
nameSet: true | ||
}); | ||
await message.channel.send("Type `sayName`"); | ||
return; | ||
} | ||
|
||
if (/^sayName$/i.test(message.content)) { | ||
const currentPersistentState = await this.getPersistentState(this.guildAndMemberScopedIndex); | ||
if (currentPersistentState.nameSet) { | ||
await message.channel.send(currentPersistentState.name); | ||
} | ||
return; | ||
} | ||
|
||
if (/^setName$/i.test(message.content)) { | ||
await this.setPersistentState(this.guildAndMemberScopedIndex, { nameSet: false }); | ||
await this.setState(this.guildMemberAndChannelScopedIndex, { | ||
step: Step.SetFirstName | ||
}); | ||
await message.channel.send("Enter first name"); | ||
} | ||
} | ||
} | ||
|
||
enum Step { | ||
None, | ||
SetFirstName, | ||
SetLastName | ||
} |
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,41 @@ | ||
import { CachingService, Index, PersistentCachingService } from "../services"; | ||
import { Command } from "./command"; | ||
|
||
export abstract class StatefulCommand<State, PersistentState> extends Command { | ||
constructor( | ||
private readonly cachingService: CachingService, | ||
private readonly persistentCachingService: PersistentCachingService, | ||
private readonly defaultState: State, | ||
private readonly defaultPersistentState: PersistentState | ||
) { | ||
super(); | ||
} | ||
async getState(index: Index): Promise<State> { | ||
return await this.cachingService.getOrAdd(index.getKey(Keys.State), this.defaultState); | ||
} | ||
async setState(index: Index, newStateChanges: Partial<State>): Promise<void> { | ||
const currentState = await this.cachingService.getOrAdd<State>(index.getKey(Keys.State), this.defaultState); | ||
const newState = Object.assign(currentState, newStateChanges); | ||
await this.cachingService.set(index.getKey(Keys.State), newState); | ||
} | ||
|
||
async getPersistentState(index: Index): Promise<PersistentState> { | ||
return await this.persistentCachingService.getOrAdd<PersistentState>( | ||
index.getKey(Keys.State), | ||
this.defaultPersistentState | ||
); | ||
} | ||
async setPersistentState(index: Index, newStateChanges: Partial<PersistentState>): Promise<void> { | ||
const currentState = await this.persistentCachingService.getOrAdd<PersistentState>( | ||
index.getKey(Keys.State), | ||
this.defaultPersistentState | ||
); | ||
const newState = Object.assign(currentState, newStateChanges); | ||
await this.persistentCachingService.set(index.getKey(Keys.State), newState); | ||
} | ||
} | ||
|
||
/** @ignore */ | ||
class Keys { | ||
static State = "State"; | ||
} |
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