wynn/ts/src/workflows/discord.ts

124 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-06-14 03:52:32 +00:00
import { proxyActivities, startChild, workflowInfo } from '@temporalio/workflow';
2025-03-22 07:08:26 +00:00
import type * as activities from '#/activities';
2025-06-14 03:52:32 +00:00
import { ApplicationCommandOptionTypes, InteractionTypes } from 'discordeno';
import { handleCommandGuildInfo, handleCommandGuildOnline, handleCommandGuildLeaderboard } from './guild-messages';
import { BotType } from '#/bot';
2025-03-22 07:08:26 +00:00
2025-06-14 03:52:32 +00:00
const { reply_to_interaction } = proxyActivities<typeof activities>({
2025-03-22 07:08:26 +00:00
startToCloseTimeout: '1 minute',
});
interface HandleInteractionCreatePayload {
ref: activities.InteractionRef
2025-06-14 03:52:32 +00:00
data: Parameters<NonNullable<BotType['events']['interactionCreate']>>[0]['data']
2025-03-22 07:08:26 +00:00
}
const workflowHandleApplicationCommand = async (
2025-06-14 03:52:32 +00:00
payload: HandleInteractionCreatePayload,
2025-03-22 07:08:26 +00:00
) => {
2025-06-14 03:52:32 +00:00
const { ref, data } = payload;
if (!data || !data.name) {
await reply_to_interaction({
ref,
type: 4,
options: {
content: "Invalid command data",
isPrivate: true,
}
});
return;
}
// Build command path
const commandPath: string[] = [data.name];
if (data.options) {
for (const option of data.options) {
if (option.type === ApplicationCommandOptionTypes.SubCommand) {
commandPath.push(option.name);
}
}
}
// Route to appropriate child workflow based on command path
const fullCommand = commandPath.join('.');
const { workflowId } = workflowInfo();
try {
switch (fullCommand) {
case 'guild.info': {
const handle = await startChild(handleCommandGuildInfo, {
args: [{ ref }],
workflowId: `${workflowId}-guild-info`,
});
await handle.result();
break;
}
case 'guild.online': {
const handle = await startChild(handleCommandGuildOnline, {
args: [{ ref }],
workflowId: `${workflowId}-guild-online`,
});
await handle.result();
break;
}
2025-03-22 07:08:26 +00:00
2025-06-14 03:52:32 +00:00
case 'guild.leaderboard': {
const handle = await startChild(handleCommandGuildLeaderboard, {
args: [{ ref }],
workflowId: `${workflowId}-guild-leaderboard`,
});
await handle.result();
break;
}
case 'admin.set_wynn_guild': {
await reply_to_interaction({
ref,
type: 4,
options: {
content: "Not implemented yet",
isPrivate: true,
}
});
break;
}
default: {
await reply_to_interaction({
ref,
type: 4,
options: {
content: `Command not implemented: ${fullCommand}`,
isPrivate: true,
}
});
}
}
} catch (error) {
await reply_to_interaction({
ref,
type: 4,
options: {
content: `Error executing command: ${error}`,
isPrivate: true,
}
});
}
2025-03-22 07:08:26 +00:00
}
export const workflowHandleInteractionCreate = async (
payload: HandleInteractionCreatePayload,
) => {
2025-06-14 03:52:32 +00:00
const {ref, data} = payload
2025-03-22 07:08:26 +00:00
if(ref.type === InteractionTypes.ApplicationCommand) {
await workflowHandleApplicationCommand(payload)
}
}