import { InteractionTypes } from '@discordeno/types' import { proxyActivities, startChild, workflowInfo } from '@temporalio/workflow' import type * as activities from '#/activities' import type { InteractionCreatePayload } from '#/discord' import { CommandHandlers, createCommandHandler } from '#/discord/botevent/command_parser' import { SLASH_COMMANDS } from '#/discord/botevent/slash_commands' import { handleCommandGuildInfo, handleCommandGuildLeaderboard, handleCommandGuildOnline } from './guild_messages' import { handleCommandPlayerLookup } from './player_messages' const { reply_to_interaction } = proxyActivities({ 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({ commands: SLASH_COMMANDS, notFoundHandler: async () => { await notFoundHandler(`command not found`) }, handler: { player: { lookup: async (args) => { const { workflowId } = workflowInfo() const handle = await startChild(handleCommandPlayerLookup, { args: [{ ref, args }], workflowId: `${workflowId}-player-lookup`, }) await handle.result() }, }, 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) } }