Skip to content

Commit 9db266b

Browse files
committed
feat: context, cache, macro and more
1 parent 31472c6 commit 9db266b

File tree

22 files changed

+958
-84
lines changed

22 files changed

+958
-84
lines changed

apps/test-bot/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.env
1+
.env
2+
.commandkit

apps/test-bot/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"name": "test-bot",
3+
"version": "1.0.0",
34
"scripts": {
4-
"test": "echo \"Error: no test specified\" && exit 1"
5+
"dev": "commandkit dev",
6+
"build": "commandkit build",
7+
"start": "commandkit start"
58
},
69
"dependencies": {
710
"commandkit": "workspace:*",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { SlashCommandProps, CommandData, dmOnly } from 'commandkit';
2+
3+
export const data: CommandData = {
4+
name: 'dm-only',
5+
description: 'This is a dm only command',
6+
};
7+
8+
export async function run({ interaction }: SlashCommandProps) {
9+
dmOnly();
10+
11+
await interaction.reply('This command can only be used in a dm.');
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { SlashCommandProps, CommandData, guildOnly } from 'commandkit';
2+
3+
export const data: CommandData = {
4+
name: 'guild-only',
5+
description: 'This is a guild only command.',
6+
};
7+
8+
export async function run({ interaction }: SlashCommandProps) {
9+
guildOnly();
10+
11+
await interaction.reply('This command can only be used in a guild.');
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { unstable_cache, SlashCommandProps, CommandData } from 'commandkit';
2+
import { setTimeout } from 'node:timers/promises';
3+
4+
export const data: CommandData = {
5+
name: 'help',
6+
description: 'This is a help command.',
7+
};
8+
9+
function $botVersion() {
10+
'use macro';
11+
// this function is inlined in production build
12+
const process = require('node:process');
13+
return require(`${process.cwd()}/package.json`).version;
14+
}
15+
16+
async function someExpensiveDatabaseCall() {
17+
await setTimeout(3000);
18+
return Date.now();
19+
}
20+
21+
export async function run({ interaction }: SlashCommandProps) {
22+
await unstable_cache({ name: interaction.commandName, ttl: 60_000 });
23+
24+
await interaction.deferReply();
25+
26+
const time = await someExpensiveDatabaseCall();
27+
28+
const version = $botVersion();
29+
30+
return interaction.editReply({
31+
embeds: [
32+
{
33+
title: 'Help',
34+
description: `This is a help command. The current time is \`${time}\``,
35+
color: 0x7289da,
36+
footer: {
37+
text: `Bot Version: ${version}`,
38+
},
39+
timestamp: new Date().toISOString(),
40+
},
41+
],
42+
});
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { SlashCommandProps, CommandData, after } from 'commandkit';
2+
3+
export const data: CommandData = {
4+
name: 'run-after',
5+
description: 'This is a run-after command',
6+
};
7+
8+
export async function run({ interaction }: SlashCommandProps) {
9+
after((env) => {
10+
console.log(
11+
`The command ${interaction.commandName} was executed successfully in ${env
12+
.getExecutionTime()
13+
.toFixed(2)} milliseconds!`,
14+
);
15+
});
16+
17+
await interaction.reply(
18+
'Hello, you will see a new message printed in your console after this command runs.',
19+
);
20+
}

apps/test-bot/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ new CommandKit({
1313
validationsPath: `${__dirname}/validations`,
1414
devGuildIds: process.env.DEV_GUILD_ID?.split(',') ?? [],
1515
devUserIds: process.env.DEV_USER_ID?.split(',') ?? [],
16-
bulkRegister: true,
16+
bulkRegister: false,
1717
});
1818

1919
await client.login(process.env.TOKEN);

packages/commandkit/bin/build.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
write,
1212
} from './common.mjs';
1313
import ora from 'ora';
14+
import { esbuildPluginUseMacro } from 'use-macro';
1415

1516
export async function bootstrapProductionBuild(config) {
1617
const {
@@ -44,7 +45,9 @@ export async function bootstrapProductionBuild(config) {
4445
outDir,
4546
silent: true,
4647
watch: false,
48+
cjsInterop: true,
4749
entry: [src, '!dist', '!.commandkit', `!${outDir}`],
50+
esbuildPlugins: [esbuildPluginUseMacro()],
4851
});
4952

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

packages/commandkit/bin/development.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export async function bootstrapDevelopmentServer(opts) {
6969
silent: true,
7070
entry: [src, '!dist', '!.commandkit', `!${outDir}`].filter(Boolean),
7171
watch: watchMode,
72+
cjsInterop: true,
7273
async onSuccess() {
7374
return await injectShims('.commandkit', main, false, requirePolyfill);
7475
},

packages/commandkit/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"ora": "^8.0.1",
4848
"rfdc": "^1.3.1",
4949
"rimraf": "^5.0.5",
50-
"tsup": "^8.0.1"
50+
"tsup": "^8.3.5",
51+
"use-macro": "^1.0.1"
5152
},
5253
"devDependencies": {
5354
"@types/node": "^22.10.2",
@@ -56,7 +57,6 @@
5657
"tsconfig": "workspace:*",
5758
"tsx": "^4.7.0",
5859
"typescript": "^5.7.2",
59-
"use-macro": "^1.0.1",
6060
"vitest": "^1.2.1"
6161
},
6262
"peerDependencies": {

0 commit comments

Comments
 (0)