refactor(context.ts) clean up context constructing

This commit is contained in:
Jacob Nguyen
2022-03-29 00:58:49 -05:00
parent b326a91d10
commit 145fcb37fe
3 changed files with 12 additions and 5 deletions

View File

@@ -22,7 +22,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
.pipe(
filter(mod => is(mod, CommandType.SLASH)),
tap ( mod => {
const ctx = new Context(None, Some(interaction));
const ctx = Context.wrap(interaction);
(mod as SlashCommand)!.execute(ctx, ['slash', interaction.options]);
}),
);
@@ -32,7 +32,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
.pipe(
filter( mod => is(mod, CommandType.MENU_USER)),
tap ( mod => {
const ctx = new Context(None, Some(interaction));
const ctx = Context.wrap(interaction);
(mod as ContextMenuUser)!.execute(ctx);
})
)
@@ -42,7 +42,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
.pipe(
filter( mod => is(mod, CommandType.MENU_MSG)),
tap ( mod => {
const ctx = new Context(None, Some(interaction));
const ctx = Context.wrap(interaction);
(mod as ContextMenuMsg)!.execute(ctx);
})
)

View File

@@ -33,9 +33,10 @@ export const onMessageCreate = (wrapper : Wrapper) => {
)
).subscribe ({
error() {
error(e) {
//log things
console.log('Failed to finished message subscription!');
throw e;
},
next(command) {
//log on each command emitted

View File

@@ -15,10 +15,16 @@ export default class Context<I extends Interaction = Interaction> {
private msg: Option<Message> = None;
private interac: Option<I> = None;
constructor(message : Option<Message>, interaction: Option<I> ) {
private constructor(message : Option<Message>, interaction: Option<I> ) {
this.msg = message;
this.interac = interaction;
}
static wrap<I extends Interaction>(wrappable: I | Message) : Context<I> {
if ( "token" in wrappable) {
return new Context( None, Some(wrappable));
}
return new Context(Some(wrappable), None)
}
private get messageUnchecked() {
return this.msg.unwrap();