wynn/ts/src/workflows/discord.ts
a 24c7c2b9b1
Some checks failed
commit-tag / commit-tag-image (map[context:./migrations file:./migrations/Dockerfile name:migrations]) (push) Successful in 1m13s
commit-tag / commit-tag-image (map[context:./ts file:./ts/Dockerfile name:ts]) (push) Failing after 1m31s
noot
2025-06-14 00:36:37 -05:00

91 lines
2.7 KiB
TypeScript

import { proxyActivities, startChild, workflowInfo } from '@temporalio/workflow';
import type * as activities from '#/activities';
import { InteractionTypes } from '@discordeno/types';
import { handleCommandGuildInfo, handleCommandGuildOnline, handleCommandGuildLeaderboard } from './guild_messages';
import { createCommandHandler } from '#/discord/botevent/command_parser';
import { SLASH_COMMANDS } from '#/discord/botevent/slash_commands';
import { InteractionCreatePayload} from '#/discord';
const { reply_to_interaction } = proxyActivities<typeof activities>({
startToCloseTimeout: '1 minute',
});
// Define command handlers with type safety
const workflowHandleApplicationCommand = async (
payload: InteractionCreatePayload,
) => {
const { ref, data } = payload;
const notFoundHandler = async (content: string) => {
await reply_to_interaction({
ref,
type: 4,
options: {
content: content,
isPrivate: true,
}
});
}
if (!data || !data.name) {
await notFoundHandler(`Invalid command data`);
return
}
const commandHandler = createCommandHandler<typeof SLASH_COMMANDS>({
notFoundHandler: async () => {
await notFoundHandler(`command not found`);
},
handler:{
guild: {
info: async (args: {}) => {
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandGuildInfo, {
args: [{ ref }],
workflowId: `${workflowId}-guild-info`,
});
await handle.result();
},
online: async (args: {}) => {
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandGuildOnline, {
args: [{ ref }],
workflowId: `${workflowId}-guild-online`,
});
await handle.result();
},
leaderboard: async (args: {}) => {
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandGuildLeaderboard, {
args: [{ ref }],
workflowId: `${workflowId}-guild-leaderboard`,
});
await handle.result();
},
},
admin: {
set_wynn_guild: async (args: {}) => {
await reply_to_interaction({
ref,
type: 4,
options: {
content: "Not implemented yet",
isPrivate: true,
}
});
},
},
}
});
await commandHandler(data);
}
export const workflowHandleInteractionCreate = async (
payload: InteractionCreatePayload,
) => {
const {ref, data} = payload
if(ref.type === InteractionTypes.ApplicationCommand) {
await workflowHandleApplicationCommand(payload)
}
}