wynn/ts/src/activities/discord.ts
a 7986b85d27
Some checks failed
commit-tag / commit-tag-image (map[context:./migrations file:./migrations/Dockerfile name:migrations]) (push) Successful in 14s
commit-tag / commit-tag-image (map[context:./ts file:./ts/Dockerfile name:ts]) (push) Failing after 34s
noot
2025-03-22 02:08:26 -05:00

54 lines
1.6 KiB
TypeScript

import { Bot } from "#/bot";
import {c} from "#/di"
import { InteractionResponseTypes, InteractionCallbackOptions, InteractionCallbackData, InteractionTypes, MessageFlags } from "discordeno";
export interface InteractionRef {
id: string
token: string
type: InteractionTypes
acknowledged?: boolean
}
// from https://github.com/discordeno/discordeno/blob/21.0.0/packages/bot/src/transformers/interaction.ts#L33
export const reply_to_interaction = async (props: {
ref: InteractionRef
response: InteractionCallbackData | string
options?: InteractionCallbackOptions & {isPrivate?: boolean}
}) => {
const bot = await c.getAsync(Bot);
const {
ref,
response,
options,
} = props;
let data: InteractionCallbackData
let type: InteractionResponseTypes = InteractionResponseTypes.ChannelMessageWithSource
if (typeof response === 'string') {
data = {content: response}
} else {
data = response
if(data.title) {
type = InteractionResponseTypes.Modal
}
}
if(ref.type === InteractionTypes.ApplicationCommandAutocomplete) {
type = InteractionResponseTypes.ApplicationCommandAutocompleteResult
}
if (type === InteractionResponseTypes.ChannelMessageWithSource && options?.isPrivate) {
data.flags = MessageFlags.Ephemeral
}
if(ref.acknowledged) {
return await bot.helpers.sendFollowupMessage(ref.token,data)
}
if(ref.type === InteractionTypes.ModalSubmit && type === InteractionResponseTypes.Modal) {
throw new Error('Cannot send a modal response to a modal')
}
return await bot.helpers.sendInteractionResponse(ref.id, ref.token,
{ type, data }, { withResponse: options?.withResponse })
}