mirror of
https://github.com/sern-handler/handler
synced 2026-06-20 23:02:15 +00:00
feat(interactionCreate) add basic context menu handling
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import type { ChatInputCommandInteraction, CommandInteraction, Interaction } from 'discord.js';
|
||||
import { map, filter, fromEvent, Observable, of, tap, concatMap} from 'rxjs';
|
||||
|
||||
import type { Interaction } from 'discord.js';
|
||||
import { filter, fromEvent, Observable, of, tap, concatMap} from 'rxjs';
|
||||
import { None, Some } from 'ts-results';
|
||||
import type { SlashCommand } from '../..';
|
||||
import { CommandType } from '../sern';
|
||||
import type { ContextMenuMsg, ContextMenuUser } from '../structures/commands/module';
|
||||
import Context from '../structures/context';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import * as Files from '../utilities/readFile';
|
||||
|
||||
import { is } from './interactionHandling';
|
||||
|
||||
|
||||
export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
@@ -16,11 +18,9 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
.pipe(
|
||||
concatMap ( interaction => {
|
||||
if (interaction.isChatInputCommand()) {
|
||||
return of(interaction.commandName).pipe(
|
||||
map ( name => Files.Commands.get(name) ),
|
||||
filter( mod => mod !== undefined
|
||||
&& (mod.type & CommandType.SLASH) != 0
|
||||
),
|
||||
return of(Files.Commands.get(interaction.commandName))
|
||||
.pipe(
|
||||
filter(mod => is(mod, CommandType.SLASH)),
|
||||
tap ( mod => {
|
||||
const ctx = new Context(None, Some(interaction));
|
||||
(mod as SlashCommand)!.execute(ctx, ['slash', interaction.options]);
|
||||
@@ -28,10 +28,24 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
|
||||
);
|
||||
}
|
||||
if (interaction.isContextMenuCommand()) {
|
||||
return of();
|
||||
return of(Files.ContextMenuUser.get(interaction.commandName))
|
||||
.pipe(
|
||||
filter( mod => is(mod, CommandType.MENU_USER)),
|
||||
tap ( mod => {
|
||||
const ctx = new Context(None, Some(interaction));
|
||||
(mod as ContextMenuUser)!.execute(ctx);
|
||||
})
|
||||
)
|
||||
}
|
||||
if (interaction.isMessageContextMenuCommand()) {
|
||||
return of();
|
||||
return of(Files.ContextMenuMsg.get(interaction.commandName))
|
||||
.pipe(
|
||||
filter( mod => is(mod, CommandType.MENU_MSG)),
|
||||
tap ( mod => {
|
||||
const ctx = new Context(None, Some(interaction));
|
||||
(mod as ContextMenuMsg)!.execute(ctx);
|
||||
})
|
||||
)
|
||||
}
|
||||
else { return of(); }
|
||||
})
|
||||
|
||||
7
src/handler/events/interactionHandling.ts
Normal file
7
src/handler/events/interactionHandling.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { CommandType } from "../sern";
|
||||
import type { Module } from "../structures/structxports";
|
||||
|
||||
|
||||
export function is(mod: Module | undefined, type : CommandType) : boolean {
|
||||
return mod !== undefined && (mod.type & type) != 0;
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import Context from '../structures/context';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from '../utilities/messageHelpers';
|
||||
import * as Files from '../utilities/readFile';
|
||||
import { is } from './interactionHandling';
|
||||
|
||||
export const onMessageCreate = (wrapper : Wrapper) => {
|
||||
const { client, defaultPrefix } = wrapper;
|
||||
@@ -24,7 +25,7 @@ export const onMessageCreate = (wrapper : Wrapper) => {
|
||||
args
|
||||
] as const
|
||||
),
|
||||
filter( ([mod]) => mod !== undefined && (mod.type & CommandType.TEXT) != 0 ),
|
||||
filter( ([mod]) => is( mod, CommandType.TEXT) ),
|
||||
tap ( ([ mod, ctx, args ]) => {
|
||||
(mod as TextCommand)!.execute(ctx, ['text', args]);
|
||||
}),
|
||||
|
||||
@@ -41,11 +41,10 @@ const handler = ( name : string ) =>
|
||||
mod.alias.forEach (a => Files.Alias.set(a, mod));
|
||||
},
|
||||
[CommandType.MENU_USER] : mod => {
|
||||
Files.Commands.set ( name, mod );
|
||||
Files.ContextMenuUser.set ( name, mod );
|
||||
},
|
||||
[CommandType.MENU_MSG] : mod => {
|
||||
|
||||
Files.Commands.set (name, mod );
|
||||
Files.ContextMenuMsg.set (name, mod );
|
||||
}
|
||||
} as ModuleHandlers);
|
||||
|
||||
|
||||
@@ -19,8 +19,6 @@ export function init( wrapper : Wrapper ) {
|
||||
onReady( wrapper );
|
||||
onMessageCreate( wrapper );
|
||||
onInteractionCreate ( wrapper );
|
||||
|
||||
|
||||
}
|
||||
|
||||
function eventObserver(client: Client, events: DiscordEvent[] ) {
|
||||
@@ -32,22 +30,6 @@ function eventObserver(client: Client, events: DiscordEvent[] ) {
|
||||
|
||||
export class Handler {
|
||||
/**
|
||||
.on('messageCreate', async (message: Message) => {
|
||||
const module = this.findModuleFrom(message);
|
||||
if (module === undefined) {
|
||||
this.defaultLogger.log(
|
||||
sEvent.MISUSE_CMD,
|
||||
message.guildId!,
|
||||
`Unknown legacy command.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
const cmdResult = await this.commandResult(module, message);
|
||||
if (cmdResult === undefined) return;
|
||||
|
||||
message.channel.send(cmdResult);
|
||||
})
|
||||
|
||||
.on('interactionCreate', async (interaction) => {
|
||||
if (!interaction.isCommand()) return;
|
||||
if (interaction.guild === null) return; // TODO : handle dms
|
||||
|
||||
@@ -3,7 +3,8 @@ import { join } from 'path';
|
||||
import type { Module } from '../structures/commands/module';
|
||||
import { SernError } from '../structures/errors';
|
||||
|
||||
|
||||
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>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user