diff --git a/src/handler/events/interactionCreate.ts b/src/handler/events/interactionCreate.ts index 6c088ea..528515f 100644 --- a/src/handler/events/interactionCreate.ts +++ b/src/handler/events/interactionCreate.ts @@ -40,6 +40,9 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { }), ) } + if (interaction.isModalSubmit()) { + return of(); + } else { return of() } }) ).subscribe({ @@ -48,7 +51,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => { }, next(command) { //log on each command emitted - console.log(command); + console.log(command?.name); }, }); }; diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index fda39f3..025056a 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -1,11 +1,19 @@ +import { ContextMenuCommandBuilder } from '@discordjs/builders'; +import type { APIInteractionGuildMember } from 'discord-api-types/v9'; import type { + Awaitable, + Guild, + GuildMember, Interaction, Message, - Snowflake + Snowflake, + TextBasedChannel, + User } from 'discord.js'; import { None, Option, Some } from 'ts-results'; +import type { Nullish } from '../../types/handler'; -function firstSome(...args : Option[]) : T | null { +function firstSome(...args : Option[]) : Nullish { for ( const op of args ) { if (op.some) return op.val; } @@ -41,17 +49,16 @@ export default class Context { return this.oInterac.unwrap(); } - /** * maps a general Context to Context * if interaction is None return Context.empty() */ public map_interaction( - cb : ( ctx: I ) => Context + cb : ( ctx: I ) => B ) : Context { if (this.oInterac.none) return Context.empty(); - return cb(this.oInterac.val); + return Context.wrap(cb(this.oInterac.val)); } public get id() : Snowflake { @@ -60,14 +67,13 @@ export default class Context { this.oMsg.andThen(m => Some(m.id)) )!; } - - public get channel() { + public get channel() : Nullish { return firstSome( this.oMsg.andThen(m => Some(m.channel)), this.oInterac.andThen(i => Some(i.channel)) ); } - public get user() { + public get user(): Nullish { return firstSome( this.oMsg.andThen(m => Some(m.author)), this.oInterac.andThen(i => Some(i.user)) @@ -79,8 +85,49 @@ export default class Context { this.oInterac.andThen(i => Some(i.createdTimestamp)) )!; } + + public get guild() : Nullish { + return firstSome( + this.oMsg.andThen(m => Some(m.guild)), + this.oInterac.andThen(i => Some(i.guild)) + ); + } + public get guildId() : Nullish { + return firstSome( + this.oMsg.andThen(m => Some(m.guildId)), + this.oInterac.andThen(i => Some(i.guildId)) + ); + } + public get member() : Nullish { + return firstSome( + this.oMsg.andThen(m => Some(m.member)), + this.oInterac.andThen(i => Some(i.member)) + ); + } + /* + * Returns the underlying Context but allows for doing other operations + */ + public on ( + onInteraction : ( interaction : I ) => Awaitable, + onMsg? : (message : Message) => Awaitable + ): Context { + this.oInterac.andThen(i => Some(onInteraction(i))); + this.oMsg.andThen(m => Some(onMsg?.(m))); + return this; + } + public extractInteraction( + extract : (interaction : I) => T + ): Nullish { + if(this.oInterac.none) return null; + return extract(this.oInterac.val); + } + public extractMessage( + extract : (interaction : Message) => T + ): Nullish { + if(this.oMsg.none) return null; + return extract(this.oMsg.val); + } } - diff --git a/src/handler/structures/wrapper.ts b/src/handler/structures/wrapper.ts index 96989f7..54168d1 100644 --- a/src/handler/structures/wrapper.ts +++ b/src/handler/structures/wrapper.ts @@ -8,7 +8,6 @@ import type { DiscordEvent } from '../../types/handler'; * @property {readonly string} defaultPrefix * @property {readonly string} commands * @prop {(handler : Handler) => void)} init - * @property {readonly {test: boolean, id: string}[]} privateServers * @prop { readonly DiscordEvent[] } events */ interface Wrapper { diff --git a/src/types/handler.ts b/src/types/handler.ts index 6dc461a..5c334d4 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -12,7 +12,7 @@ import type { Module } from '../handler/structures/structxports'; // Anything that can be sent in a `#send` or `#reply` export type possibleOutput = T | (MessagePayload & MessageOptions); export type execute = Module['execute']; - +export type Nullish = T | undefined | null; // Thanks @cursorsdottsx export type ParseType = { [K in keyof T]: T[K] extends unknown ? [k: K, args: T[K]] : never;