wynn/ts/src/workflows/discord.ts

102 lines
3.1 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 05:36:37 +00:00
import { InteractionTypes } from '@discordeno/types';
import { handleCommandGuildInfo, handleCommandGuildOnline, handleCommandGuildLeaderboard } from './guild_messages';
2025-06-14 21:17:42 +00:00
import { handleCommandPlayerLookup } from './player_messages';
2025-06-14 05:36:37 +00:00
import { createCommandHandler } from '#/discord/botevent/command_parser';
import { SLASH_COMMANDS } from '#/discord/botevent/slash_commands';
import { InteractionCreatePayload} from '#/discord';
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 05:36:37 +00:00
// Define command handlers with type safety
2025-03-22 07:08:26 +00:00
const workflowHandleApplicationCommand = async (
2025-06-14 05:36:37 +00:00
payload: InteractionCreatePayload,
2025-03-22 07:08:26 +00:00
) => {
2025-06-14 03:52:32 +00:00
const { ref, data } = payload;
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 05:36:37 +00:00
if (!data || !data.name) {
await notFoundHandler(`Invalid command data`);
return
2025-06-14 03:52:32 +00:00
}
2025-06-14 05:36:37 +00:00
const commandHandler = createCommandHandler<typeof SLASH_COMMANDS>({
notFoundHandler: async () => {
await notFoundHandler(`command not found`);
},
2025-06-14 21:17:42 +00:00
handler: {
player: {
lookup: async (args) => {
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandPlayerLookup, {
args: [{ ref, args }],
workflowId: `${workflowId}-player-lookup`,
});
await handle.result();
},
},
2025-06-14 05:36:37 +00:00
guild: {
2025-06-14 21:17:42 +00:00
info: async (args) => {
2025-06-14 05:36:37 +00:00
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandGuildInfo, {
args: [{ ref }],
workflowId: `${workflowId}-guild-info`,
});
await handle.result();
},
2025-06-14 21:17:42 +00:00
online: async (args) => {
2025-06-14 05:36:37 +00:00
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandGuildOnline, {
args: [{ ref }],
workflowId: `${workflowId}-guild-online`,
});
await handle.result();
},
2025-06-14 21:17:42 +00:00
leaderboard: async (args) => {
2025-06-14 05:36:37 +00:00
const { workflowId } = workflowInfo();
const handle = await startChild(handleCommandGuildLeaderboard, {
args: [{ ref }],
workflowId: `${workflowId}-guild-leaderboard`,
});
await handle.result();
},
},
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: {
content: "Not implemented yet",
isPrivate: true,
}
});
},
},
2025-06-14 03:52:32 +00:00
}
2025-06-14 05:36:37 +00:00
});
await commandHandler(data);
2025-03-22 07:08:26 +00:00
}
export const workflowHandleInteractionCreate = async (
2025-06-14 05:36:37 +00:00
payload: InteractionCreatePayload,
2025-03-22 07:08:26 +00:00
) => {
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)
}
}