mirror of
https://github.com/sern-handler/handler
synced 2026-06-19 22:32:14 +00:00
feat(handler) Add button and select menu support!
This commit is contained in:
@@ -40,8 +40,24 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
}),
|
||||
)
|
||||
}
|
||||
if (interaction.isModalSubmit()) {
|
||||
return of();
|
||||
if (interaction.isButton()) {
|
||||
return of(Files.Buttons.get(interaction.customId))
|
||||
.pipe(
|
||||
filterTap(CommandType.BUTTON, mod => {
|
||||
const ctx = Context.wrap(interaction);
|
||||
mod.execute(ctx);
|
||||
})
|
||||
)
|
||||
}
|
||||
if (interaction.isSelectMenu()) {
|
||||
return of(Files.SelectMenus.get(interaction.customId))
|
||||
.pipe(
|
||||
filterTap(CommandType.MENU_SELECT, mod => {
|
||||
const ctx = Context.wrap(interaction);
|
||||
mod.execute(ctx);
|
||||
})
|
||||
|
||||
)
|
||||
}
|
||||
else { return of() }
|
||||
})
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Message } from 'discord.js';
|
||||
import { fromEvent, Observable, of, concatMap, filter} from 'rxjs';
|
||||
import { fromEvent, Observable, of, concatMap } from 'rxjs';
|
||||
import { CommandType } from '../sern';
|
||||
import Context from '../structures/context';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from '../utilities/messageHelpers';
|
||||
import { fmt } from '../utilities/messageHelpers';
|
||||
import * as Files from '../utilities/readFile';
|
||||
import { filterTap, ignoreNonBot } from './observableHandling';
|
||||
|
||||
|
||||
@@ -45,6 +45,12 @@ const handler = ( name : string ) =>
|
||||
},
|
||||
[CommandType.MENU_MSG] : mod => {
|
||||
Files.ContextMenuMsg.set (name, mod );
|
||||
},
|
||||
[CommandType.BUTTON] : mod => {
|
||||
Files.Buttons.set(name, mod);
|
||||
},
|
||||
[CommandType.MENU_SELECT] : mod => {
|
||||
Files.SelectMenus.set(name, mod);
|
||||
}
|
||||
|
||||
} as ModuleHandlers);
|
||||
|
||||
@@ -94,9 +94,12 @@ export class Handler {
|
||||
* @enum { number };
|
||||
*/
|
||||
export enum CommandType {
|
||||
TEXT = 0b0001,
|
||||
SLASH = 0b0010,
|
||||
MENU_USER = 0b0100,
|
||||
MENU_MSG = 0b1000,
|
||||
BOTH = 0b0011,
|
||||
TEXT = 0b000001,
|
||||
SLASH = 0b000010,
|
||||
MENU_USER = 0b000100,
|
||||
MENU_MSG = 0b001000,
|
||||
BUTTON = 0b010000,
|
||||
MENU_SELECT =0b100000,
|
||||
BOTH = 0b000011,
|
||||
ANY = 0b111111
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { ApplicationCommandOptionData, Awaitable, ChatInputCommandInteraction, ContextMenuCommandInteraction, Interaction, MessageContextMenuCommandInteraction } from 'discord.js';
|
||||
import type { ApplicationCommandOptionData, Awaitable, ButtonInteraction, ChatInputCommandInteraction, ContextMenuCommandInteraction, Interaction, MessageContextMenuCommandInteraction, SelectMenuInteraction } from 'discord.js';
|
||||
import type { Args, Override } from '../../../types/handler';
|
||||
import type { CommandType } from '../../sern';
|
||||
import type Context from '../context';
|
||||
@@ -33,11 +33,20 @@ export type ContextMenuUser = {
|
||||
export type ContextMenuMsg = {
|
||||
type : CommandType.MENU_MSG;
|
||||
} & Override<BaseModule, { execute : ( ctx: Context<MessageContextMenuCommandInteraction> ) => Awaitable<void> }>;
|
||||
export type ButtonCommand = {
|
||||
type : CommandType.BUTTON;
|
||||
} & Override<BaseModule, { execute : (ctx : Context<ButtonInteraction> ) => Awaitable<void> }>;
|
||||
|
||||
export type SelectMenuCommand = {
|
||||
type : CommandType.MENU_SELECT;
|
||||
} & Override<BaseModule, { execute : (ctx : Context<SelectMenuInteraction> ) => Awaitable<void> }>;
|
||||
|
||||
export type Module =
|
||||
TextCommand
|
||||
| SlashCommand
|
||||
| BothCommand
|
||||
| ContextMenuUser
|
||||
| ContextMenuMsg;
|
||||
| ContextMenuMsg
|
||||
| ButtonCommand
|
||||
| SelectMenuCommand;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { CommandType } from '../../sern';
|
||||
import type { TextCommand, BothCommand, SlashCommand, BaseModule, ContextMenuMsg, ContextMenuUser } from './module';
|
||||
import type { TextCommand, BothCommand, ButtonCommand, SlashCommand, BaseModule, ContextMenuMsg, ContextMenuUser, SelectMenuCommand } from './module';
|
||||
|
||||
//https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union
|
||||
|
||||
@@ -9,7 +9,9 @@ export type ModuleDefs = {
|
||||
[CommandType.SLASH] : SlashCommand,
|
||||
[CommandType.BOTH] : BothCommand,
|
||||
[CommandType.MENU_MSG] : ContextMenuMsg,
|
||||
[CommandType.MENU_USER] : ContextMenuUser
|
||||
[CommandType.MENU_USER] : ContextMenuUser,
|
||||
[CommandType.BUTTON] : ButtonCommand,
|
||||
[CommandType.MENU_SELECT] : SelectMenuCommand
|
||||
}
|
||||
|
||||
//Keys of ModuleDefs
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { ContextMenuCommandBuilder } from '@discordjs/builders';
|
||||
import type { APIInteractionGuildMember } from 'discord-api-types/v9';
|
||||
import type {
|
||||
Awaitable,
|
||||
@@ -35,9 +34,6 @@ export default class Context<I extends Interaction = Interaction> {
|
||||
}
|
||||
return new Context<I>(Some(wrappable), None);
|
||||
}
|
||||
public static empty<T extends Interaction>() : Context<T> {
|
||||
return new Context<T>(None, None);
|
||||
}
|
||||
public isEmpty() {
|
||||
return this.oMsg.none && this.oInterac.none;
|
||||
}
|
||||
@@ -56,7 +52,7 @@ export default class Context<I extends Interaction = Interaction> {
|
||||
public mapInteraction<B extends Interaction = Interaction>(
|
||||
cb : ( ctx: I ) => Context<B>
|
||||
) : Context<B> {
|
||||
if (this.oInterac.none) return Context.empty();
|
||||
if (this.oInterac.none) return new Context();
|
||||
return cb(this.oInterac.val);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,13 @@ import { join } from 'path';
|
||||
import type { Module } from '../structures/commands/module';
|
||||
import { SernError } from '../structures/errors';
|
||||
|
||||
//We can look into lazily loading modules once everything is set
|
||||
export const ContextMenuUser = new Map<string, Module>();
|
||||
export const ContextMenuMsg = new Map<string, Module>();
|
||||
export const Commands = new Map<string, Module>();
|
||||
export const Alias = new Map<string, Module>();
|
||||
export const Buttons = new Map<string, Module>();
|
||||
export const SelectMenus = new Map<string, Module>();
|
||||
|
||||
// Courtesy @Townsy45
|
||||
function readPath(dir: string, arrayOfFiles: string[] = []): string[] {
|
||||
|
||||
Reference in New Issue
Block a user