wynn/ts/src/workflows/discord.ts

99 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-06-14 23:04:46 +00:00
import { InteractionTypes } from '@discordeno/types'
import { proxyActivities, startChild, workflowInfo } from '@temporalio/workflow'
import type * as activities from '#/activities'
import type { InteractionCreatePayload } from '#/discord'
2025-06-14 23:54:24 +00:00
import { CommandHandlers, createCommandHandler } from '#/discord/botevent/command_parser'
import { SLASH_COMMANDS } from '#/discord/botevent/slash_commands'
2025-06-14 23:04:46 +00:00
import { handleCommandGuildInfo, handleCommandGuildLeaderboard, handleCommandGuildOnline } from './guild_messages'
import { handleCommandPlayerLookup } from './player_messages'
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',
2025-06-14 23:04:46 +00:00
})
2025-03-22 07:08:26 +00:00
2025-06-14 05:36:37 +00:00
// Define command handlers with type safety
2025-06-14 23:04:46 +00:00
const workflowHandleApplicationCommand = async (payload: InteractionCreatePayload) => {
const { ref, data } = payload
2025-06-14 03:52:32 +00:00
2025-06-14 05:36:37 +00:00
const notFoundHandler = async (content: string) => {
2025-06-14 03:52:32 +00:00
await reply_to_interaction({
ref,
type: 4,
options: {
2025-06-14 05:36:37 +00:00
content: content,
2025-06-14 03:52:32 +00:00
isPrivate: true,
2025-06-14 23:04:46 +00:00
},
})
2025-06-14 03:52:32 +00:00
}
2025-06-14 05:36:37 +00:00
if (!data || !data.name) {
2025-06-14 23:04:46 +00:00
await notFoundHandler(`Invalid command data`)
2025-06-14 05:36:37 +00:00
return
2025-06-14 03:52:32 +00:00
}
2025-06-14 23:54:24 +00:00
const commandHandler = createCommandHandler({
commands: SLASH_COMMANDS,
2025-06-14 05:36:37 +00:00
notFoundHandler: async () => {
2025-06-14 23:04:46 +00:00
await notFoundHandler(`command not found`)
2025-06-14 05:36:37 +00:00
},
2025-06-14 21:17:42 +00:00
handler: {
player: {
lookup: async (args) => {
2025-06-14 23:04:46 +00:00
const { workflowId } = workflowInfo()
2025-06-14 21:17:42 +00:00
const handle = await startChild(handleCommandPlayerLookup, {
args: [{ ref, args }],
workflowId: `${workflowId}-player-lookup`,
2025-06-14 23:04:46 +00:00
})
await handle.result()
2025-06-14 21:17:42 +00:00
},
},
2025-06-14 05:36:37 +00:00
guild: {
2025-06-14 21:17:42 +00:00
info: async (args) => {
2025-06-14 23:04:46 +00:00
const { workflowId } = workflowInfo()
2025-06-14 05:36:37 +00:00
const handle = await startChild(handleCommandGuildInfo, {
args: [{ ref }],
workflowId: `${workflowId}-guild-info`,
2025-06-14 23:04:46 +00:00
})
await handle.result()
2025-06-14 05:36:37 +00:00
},
2025-06-14 21:17:42 +00:00
online: async (args) => {
2025-06-14 23:04:46 +00:00
const { workflowId } = workflowInfo()
2025-06-14 05:36:37 +00:00
const handle = await startChild(handleCommandGuildOnline, {
args: [{ ref }],
workflowId: `${workflowId}-guild-online`,
2025-06-14 23:04:46 +00:00
})
await handle.result()
2025-06-14 05:36:37 +00:00
},
2025-06-14 21:17:42 +00:00
leaderboard: async (args) => {
2025-06-14 23:04:46 +00:00
const { workflowId } = workflowInfo()
2025-06-14 05:36:37 +00:00
const handle = await startChild(handleCommandGuildLeaderboard, {
args: [{ ref }],
workflowId: `${workflowId}-guild-leaderboard`,
2025-06-14 23:04:46 +00:00
})
await handle.result()
2025-06-14 05:36:37 +00:00
},
},
admin: {
2025-06-14 21:17:42 +00:00
set_wynn_guild: async (args) => {
2025-06-14 05:36:37 +00:00
await reply_to_interaction({
ref,
type: 4,
options: {
2025-06-14 23:04:46 +00:00
content: 'Not implemented yet',
2025-06-14 05:36:37 +00:00
isPrivate: true,
2025-06-14 23:04:46 +00:00
},
})
2025-06-14 05:36:37 +00:00
},
},
2025-06-14 23:04:46 +00:00
},
})
2025-06-14 05:36:37 +00:00
2025-06-14 23:04:46 +00:00
await commandHandler(data)
2025-03-22 07:08:26 +00:00
}
2025-06-14 23:04:46 +00:00
export const workflowHandleInteractionCreate = async (payload: InteractionCreatePayload) => {
const { ref, data } = payload
2025-03-22 07:08:26 +00:00
2025-06-14 23:04:46 +00:00
if (ref.type === InteractionTypes.ApplicationCommand) {
2025-03-22 07:08:26 +00:00
await workflowHandleApplicationCommand(payload)
}
}