mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
feat(handler) improving context api
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Awaitable, ChatInputCommandInteraction, Interaction } from "discord.js";
|
||||
import { map, filter, fromEvent, Observable, of, mergeMap, tap, concatMap} from "rxjs";
|
||||
import type { Interaction } from "discord.js";
|
||||
import { map, filter, fromEvent, Observable, of, tap, concatMap} from "rxjs";
|
||||
import { None, Some } from "ts-results";
|
||||
import { CommandType } from "../sern";
|
||||
import Context from "../structures/context";
|
||||
@@ -26,7 +26,6 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
}
|
||||
if (interaction.isContextMenuCommand()) {
|
||||
return of()
|
||||
|
||||
}
|
||||
else { return of() }
|
||||
})
|
||||
|
||||
@@ -21,7 +21,6 @@ export const onReady = ( wrapper : Wrapper ) => {
|
||||
)
|
||||
.subscribe({
|
||||
complete() {
|
||||
console.log(Files.Commands);
|
||||
// on ready event, complete!
|
||||
// log stuff?
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import { onReady } from './events/readyEvent';
|
||||
import { onMessageCreate } from './events/messageEvent';
|
||||
import { onInteractionCreate } from './events/interactionCreate';
|
||||
|
||||
export function init( wrapper : Wrapper) {
|
||||
export function init( wrapper : Wrapper ) {
|
||||
const { events, client } = wrapper;
|
||||
if (events !== undefined) eventObserver(client, events);
|
||||
onReady( wrapper );
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { ApplicationCommandOptionData, Awaitable } from "discord.js";
|
||||
import type { ApplicationCommandOptionData, Awaitable, Interaction } from "discord.js";
|
||||
import type { Args } from "../../../types/handler";
|
||||
import type { CommandType } from "../../sern";
|
||||
import type Context from "../context";
|
||||
@@ -8,7 +8,7 @@ import type Context from "../context";
|
||||
export interface BaseModule {
|
||||
name? : string;
|
||||
description : string;
|
||||
execute: (ctx: Context, args: Args) => Awaitable<void>;
|
||||
execute: (ctx: Context<Interaction>, args: Args) => Awaitable<void>;
|
||||
}
|
||||
export type Text = {
|
||||
type : CommandType.TEXT;
|
||||
|
||||
@@ -2,28 +2,48 @@ import type {
|
||||
Interaction,
|
||||
Message
|
||||
} from 'discord.js';
|
||||
import { None, Option } from 'ts-results';
|
||||
import { None, Option, Some } from 'ts-results';
|
||||
|
||||
export default class Context {
|
||||
function firstSome<T>(...args : Option<T>[]) : T | null {
|
||||
for ( const op of args ) {
|
||||
if (op.some) return op.val;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export default class Context<I extends Interaction> {
|
||||
private msg: Option<Message> = None;
|
||||
private interac: Option<Interaction> = None;
|
||||
private interac: Option<I> = None;
|
||||
|
||||
constructor(message : Option<Message>, interaction: Option<Interaction> ) {
|
||||
constructor(message : Option<Message>, interaction: Option<I> ) {
|
||||
this.msg = message;
|
||||
this.interac = interaction;
|
||||
}
|
||||
|
||||
get messageUnchecked() {
|
||||
private get messageUnchecked() {
|
||||
return this.msg.unwrap();
|
||||
}
|
||||
get interactionUnchecked() {
|
||||
private get interactionUnchecked() {
|
||||
return this.interac.unwrap();
|
||||
}
|
||||
get message() {
|
||||
private get message() {
|
||||
return this.msg;
|
||||
}
|
||||
get interaction() {
|
||||
private get interaction() {
|
||||
return this.interac;
|
||||
}
|
||||
|
||||
public get channel() {
|
||||
return firstSome(
|
||||
this.message.andThen(m => Some(m.channel)),
|
||||
this.interaction.andThen(i => Some(i.channel))
|
||||
)
|
||||
}
|
||||
public get user() {
|
||||
return firstSome(
|
||||
this.message.andThen(m => Some(m.author)),
|
||||
this.interaction.andThen(i => Some(i.user))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,4 +18,4 @@
|
||||
},
|
||||
"exclude": ["node_modules", "tests", "dist"],
|
||||
"include": ["src"],
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user