Skip to content

Commit d601434

Browse files
committed
feat: global listener for buttonkit
1 parent 8a3e2c8 commit d601434

File tree

21 files changed

+757
-183
lines changed

21 files changed

+757
-183
lines changed

apps/test-bot/commandkit.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import { defineConfig } from 'commandkit';
44

55
export default defineConfig({
6-
main: 'index.mjs',
6+
main: 'index.js',
77
src: 'src',
88
});

apps/test-bot/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "test-bot",
33
"version": "1.0.0",
4+
"type": "module",
45
"scripts": {
56
"dev": "commandkit dev",
67
"build": "commandkit build",
78
"start": "commandkit start"
89
},
910
"dependencies": {
1011
"commandkit": "workspace:*",
11-
"discord.js": "^14.16.3",
12+
"discord.js": "^14.17.3",
1213
"dotenv": "^16.4.7"
1314
},
1415
"devDependencies": {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import CommandKit, {
2+
Button,
3+
ActionRow,
4+
SlashCommandProps,
5+
CommandData,
6+
OnButtonKitClick,
7+
} from 'commandkit';
8+
import { MessageFlags } from 'discord.js';
9+
10+
export const data: CommandData = {
11+
name: 'commandkit',
12+
description: 'This is a commandkit command.',
13+
};
14+
15+
const handleButtonClick: OnButtonKitClick = async (interaction) => {
16+
const { customId } = interaction;
17+
18+
await interaction.reply({
19+
content: `You clicked the "${customId}" button!`,
20+
flags: MessageFlags.Ephemeral,
21+
});
22+
};
23+
24+
function ButtonGrid({ message }) {
25+
return (
26+
<>
27+
{Array.from({ length: 5 }, (_, i) => (
28+
<ActionRow>
29+
{Array.from({ length: 5 }, (_, j) => (
30+
<Button
31+
onClick={handleButtonClick}
32+
customId={`button ${i * 5 + j + 1}`}
33+
>
34+
{i * 5 + j + 1}
35+
</Button>
36+
))}
37+
</ActionRow>
38+
))}
39+
</>
40+
);
41+
}
42+
43+
export async function run({ interaction }: SlashCommandProps) {
44+
const { resource } = await interaction.deferReply({
45+
withResponse: true,
46+
});
47+
48+
const buttons = <ButtonGrid message={resource.message} />;
49+
50+
await interaction.editReply({
51+
content: 'Click the button below to test CommandKit buttons.',
52+
components: buttons,
53+
});
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import CommandKit, {
2+
Button,
3+
ActionRow,
4+
SlashCommandProps,
5+
CommandData,
6+
OnButtonKitClick,
7+
} from 'commandkit';
8+
import { ButtonStyle, MessageFlags } from 'discord.js';
9+
10+
export const data: CommandData = {
11+
name: 'confirm',
12+
description: 'This is a confirm command.',
13+
};
14+
15+
const handleConfirm: OnButtonKitClick = async (interaction) => {
16+
await interaction.reply({
17+
content: 'The item was deleted successfully.',
18+
flags: MessageFlags.Ephemeral,
19+
});
20+
};
21+
22+
const handleCancel: OnButtonKitClick = async (interaction) => {
23+
await interaction.reply({
24+
content: 'The item was not deleted.',
25+
flags: MessageFlags.Ephemeral,
26+
});
27+
};
28+
29+
export async function run({ interaction }: SlashCommandProps) {
30+
const buttons = (
31+
<ActionRow>
32+
<Button onClick={handleCancel} style={ButtonStyle.Primary}>
33+
Cancel
34+
</Button>
35+
<Button onClick={handleConfirm} style={ButtonStyle.Danger}>
36+
Confirm
37+
</Button>
38+
</ActionRow>
39+
);
40+
41+
await interaction.reply({
42+
content: 'Are you sure you want to delete this item?',
43+
components: [buttons],
44+
});
45+
}

apps/test-bot/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const client = new Client({
77
});
88

99
new CommandKit({
10+
// @ts-ignore
1011
client,
1112
commandsPath: `${__dirname}/commands`,
1213
eventsPath: `${__dirname}/events`,

apps/test-bot/tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "NodeNext",
5+
"moduleResolution": "nodenext",
6+
"lib": ["ESNext", "DOM"],
7+
"jsx": "react",
8+
"jsxFactory": "CommandKit.createElement",
9+
"jsxFragmentFactory": "CommandKit.Fragment",
10+
"skipLibCheck": true,
11+
"skipDefaultLibCheck": true,
12+
"allowJs": true,
13+
"alwaysStrict": false,
14+
"checkJs": false
15+
},
16+
"include": ["src"]
17+
}

packages/commandkit/src/CommandKit.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import type {
99
import colors from './utils/colors';
1010
import { CacheProvider } from './cache/CacheProvider';
1111
import { MemoryCache } from './cache/MemoryCache';
12+
import { createElement, Fragment } from './components';
13+
import { EventInterceptor } from './components/common/EventInterceptor';
1214

1315
export class CommandKit extends EventEmitter {
1416
#data: CommandKitData;
17+
public readonly eventInterceptor: EventInterceptor;
18+
19+
public static readonly createElement = createElement;
20+
public static readonly Fragment = Fragment;
1521

1622
static instance: CommandKit | undefined = undefined;
1723

@@ -55,6 +61,8 @@ export class CommandKit extends EventEmitter {
5561
options.cacheProvider = new MemoryCache();
5662
}
5763

64+
this.eventInterceptor = new EventInterceptor(options.client);
65+
5866
this.#data = options;
5967

6068
this.#init().then(() => {

packages/commandkit/src/cli/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export async function bootstrapProductionBuild(config: any) {
4242
cjsInterop: true,
4343
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
4444
esbuildPlugins: [commandkitPlugin()],
45+
jsxFactory: 'CommandKit.createElement',
46+
jsxFragment: 'CommandKit.Fragment',
4547
});
4648

4749
await injectShims(outDir, main, antiCrash, polyfillRequire);

packages/commandkit/src/cli/development.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export async function bootstrapDevelopmentServer(opts: any) {
6565
entry: [src, '!dist', '!.commandkit', `!${outDir}`].filter(Boolean),
6666
watch: watchMode,
6767
cjsInterop: true,
68+
jsxFactory: 'CommandKit.createElement',
69+
jsxFragment: 'CommandKit.Fragment',
6870
async onSuccess() {
6971
return await injectShims('.commandkit', main, false, requirePolyfill);
7072
},

packages/commandkit/src/cli/esbuild-plugins/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { readFile } from 'node:fs/promises';
55
const defaultConfig = {
66
'use-macro': true,
77
'use-cache': true,
8+
'jsx-importsource': true,
89
};
910

1011
interface CommandKitEsbuildPluginConfig {
1112
'use-macro'?: boolean;
13+
'jsx-importsource'?: boolean;
1214
'use-cache'?: boolean;
1315
}
1416

0 commit comments

Comments
 (0)