Skip to content

Commit 2cdcb65

Browse files
authored
Merge pull request #371 from underctrl-io/tasks-plugin
feat: tasks plugin
2 parents d25266b + d7dc06b commit 2cdcb65

36 files changed

+2835
-11
lines changed

.github/workflows/publish-dev.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
"@commandkit/analytics:packages/analytics"
6262
"@commandkit/ai:packages/ai"
6363
"@commandkit/queue:packages/queue"
64+
"@commandkit/tasks:packages/tasks"
6465
)
6566
6667
for entry in "${PACKAGES[@]}"; do
@@ -84,6 +85,7 @@ jobs:
8485
"@commandkit/analytics"
8586
"@commandkit/ai"
8687
"@commandkit/queue"
88+
"@commandkit/tasks"
8789
)
8890
8991
for pkg in "${PACKAGES[@]}"; do

.github/workflows/publish-latest.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
"@commandkit/devtools:packages/devtools"
4141
"@commandkit/cache:packages/cache"
4242
"@commandkit/queue:packages/queue"
43+
"@commandkit/tasks:packages/tasks"
4344
)
4445
4546
for entry in "${PACKAGES[@]}"; do

apps/test-bot/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.env
22
.commandkit
33
compiled-commandkit.config.mjs
4-
commandkit*.db*
4+
*.db*

apps/test-bot/commandkit.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { i18n } from '@commandkit/i18n';
44
import { devtools } from '@commandkit/devtools';
55
import { cache } from '@commandkit/cache';
66
import { ai } from '@commandkit/ai';
7+
import { tasks } from '@commandkit/tasks';
78

89
export default defineConfig({
910
plugins: [
@@ -12,5 +13,6 @@ export default defineConfig({
1213
devtools(),
1314
cache(),
1415
ai(),
16+
tasks(),
1517
],
1618
});

apps/test-bot/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
"@commandkit/devtools": "workspace:*",
1717
"@commandkit/i18n": "workspace:*",
1818
"@commandkit/legacy": "workspace:*",
19+
"@commandkit/tasks": "workspace:*",
1920
"commandkit": "workspace:*",
2021
"discord.js": "catalog:discordjs",
2122
"dotenv": "^16.4.7",
23+
"ms": "^2.1.3",
2224
"zod": "^3.25.56"
2325
},
2426
"devDependencies": {
27+
"@types/ms": "^2.1.0",
2528
"tsx": "^4.7.0"
2629
}
2730
}

apps/test-bot/src/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Client } from 'discord.js';
22
import { Logger, commandkit } from 'commandkit';
3+
import { setDriver } from '@commandkit/tasks';
4+
import { HyperCronDriver } from '@commandkit/tasks/hypercron';
35
import './ai.ts';
46

57
const client = new Client({
@@ -12,6 +14,8 @@ const client = new Client({
1214
],
1315
});
1416

17+
setDriver(new HyperCronDriver());
18+
1519
Logger.log('Application bootstrapped successfully!');
1620

1721
commandkit.setPrefixResolver((message) => {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type { CommandData, ChatInputCommand, MessageCommand } from 'commandkit';
2+
import { ApplicationCommandOptionType } from 'discord.js';
3+
import ms from 'ms';
4+
import { createTask } from '@commandkit/tasks';
5+
import { RemindTaskData } from '../tasks/remind';
6+
7+
export const command: CommandData = {
8+
name: 'remind',
9+
description: 'remind command',
10+
options: [
11+
{
12+
name: 'time',
13+
description: 'The time to remind after. Eg: 6h, 10m, 1d',
14+
type: ApplicationCommandOptionType.String,
15+
required: true,
16+
},
17+
{
18+
name: 'message',
19+
description: 'The message to remind about.',
20+
type: ApplicationCommandOptionType.String,
21+
required: true,
22+
},
23+
],
24+
};
25+
26+
export const chatInput: ChatInputCommand = async (ctx) => {
27+
const time = ctx.options.getString('time', true);
28+
const message = ctx.options.getString('message', true);
29+
const timeMs = Date.now() + ms(time as `${number}`);
30+
31+
await createTask({
32+
name: 'remind',
33+
data: {
34+
userId: ctx.interaction.user.id,
35+
message,
36+
channelId: ctx.interaction.channelId,
37+
setAt: Date.now(),
38+
} satisfies RemindTaskData,
39+
schedule: {
40+
type: 'date',
41+
value: timeMs,
42+
},
43+
});
44+
45+
await ctx.interaction.reply(
46+
`I will remind you <t:${Math.floor(timeMs / 1000)}:R> for \`${message}\``,
47+
);
48+
};
49+
50+
export const message: MessageCommand = async (ctx) => {
51+
const [time, ...messageParts] = ctx.args();
52+
const message = messageParts.join(' ');
53+
const timeMs = Date.now() + ms(time as `${number}`);
54+
55+
await createTask({
56+
name: 'remind',
57+
data: {
58+
userId: ctx.message.author.id,
59+
message,
60+
channelId: ctx.message.channelId,
61+
setAt: Date.now(),
62+
} satisfies RemindTaskData,
63+
schedule: {
64+
type: 'date',
65+
value: timeMs,
66+
},
67+
});
68+
69+
await ctx.message.reply(
70+
`I will remind you <t:${Math.floor(timeMs / 1000)}:R> for \`${message}\``,
71+
);
72+
};

apps/test-bot/src/app/tasks/remind.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { task } from '@commandkit/tasks';
2+
3+
export interface RemindTaskData {
4+
userId: string;
5+
message: string;
6+
channelId: string;
7+
setAt: number;
8+
}
9+
10+
export default task<RemindTaskData>({
11+
name: 'remind',
12+
async execute(ctx) {
13+
const { userId, message, channelId, setAt } = ctx.data;
14+
15+
const channel = await ctx.client.channels.fetch(channelId);
16+
17+
if (!channel?.isTextBased() || !channel.isSendable()) return;
18+
19+
await channel.send({
20+
content: `<@${userId}>`,
21+
embeds: [
22+
{
23+
title: `You asked me <t:${Math.floor(setAt / 1000)}:R> to remind you about:`,
24+
description: message,
25+
color: 0x0099ff,
26+
timestamp: new Date(setAt).toISOString(),
27+
},
28+
],
29+
});
30+
},
31+
});

apps/website/docs/api-reference/ai/functions/configure-ai.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## configureAI
1515

16-
<GenerationInfo sourceFile="packages/ai/src/configure.ts" sourceLine="183" packageName="@commandkit/ai" />
16+
<GenerationInfo sourceFile="packages/ai/src/configure.ts" sourceLine="197" packageName="@commandkit/ai" />
1717

1818
Configures the AI plugin with the provided options.
1919
This function allows you to set a message filter, select an AI model, and generate a system prompt.

apps/website/docs/api-reference/ai/functions/get-aiconfig.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## getAIConfig
1515

16-
<GenerationInfo sourceFile="packages/ai/src/configure.ts" sourceLine="174" packageName="@commandkit/ai" />
16+
<GenerationInfo sourceFile="packages/ai/src/configure.ts" sourceLine="188" packageName="@commandkit/ai" />
1717

1818
Retrieves the current AI configuration.
1919

0 commit comments

Comments
 (0)