Skip to content

Commit 55415cd

Browse files
committed
feat: [alpha] slash command
1 parent 869de8f commit 55415cd

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { event } from 'ecstar';
2+
import { Events } from 'discord.js';
3+
import { slashCommandContext } from 'ecstar/context/slashCommand';
4+
5+
export default event(() => ({
6+
name: Events.InteractionCreate,
7+
run({ client }, [interaction]) {
8+
if (!interaction.isChatInputCommand()) return;
9+
const command = client.commands.get(interaction.commandName);
10+
const ctx = slashCommandContext(client, interaction);
11+
if (command?.run && ctx) command.run(ctx);
12+
},
13+
}));

src/lib/context/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { commandContext } from 'ecstar/context/command';
66
import { eventContext } from 'ecstar/context/event';
77
import { argumentContext } from 'ecstar/context/argument';
88

9-
type ContextType = keyof Structures | 'unknown';
9+
type ContextType = keyof Structures | string | 'unknown';
1010

1111
export interface ContextBase {
1212
name: string;

src/lib/context/slashCommand.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Interaction, Message, Snowflake, TextChannel } from 'discord.js';
2+
import { Client } from 'ecstar';
3+
import { ContextBase } from 'ecstar/context';
4+
5+
export interface SlashCommandContext extends ContextBase {
6+
type: 'slashCommand';
7+
send(content: string): void;
8+
}
9+
10+
export const slashCommandContext = (
11+
client: Client,
12+
interaction: Interaction
13+
): SlashCommandContext | undefined => {
14+
if (!interaction.isChatInputCommand()) return;
15+
return {
16+
name: interaction.commandName,
17+
type: 'slashCommand',
18+
client,
19+
send(content) {
20+
interaction.reply(content);
21+
},
22+
};
23+
};

src/lib/structures/command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { CommandContext } from 'ecstar/context/command';
22
import { PermissionResolvable } from 'discord.js';
3+
import { SlashCommandContext } from 'ecstar/context/slashCommand';
34

45
export type commandOptions = {
56
name: string;
67
aliases?: string[];
78
guildOnly?: boolean;
89
permissions?: { bot?: PermissionResolvable[]; user?: PermissionResolvable[] };
9-
run?(context: CommandContext): void;
10+
run?(context: CommandContext | SlashCommandContext): void;
1011
/** @deprecated Replaced by run */
1112
render?(context: CommandContext): void;
1213
};

0 commit comments

Comments
 (0)