wynn/ts/src/activities/discord.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-03-03 03:56:43 +00:00
import { Bot } from "#/bot";
import {c} from "#/di"
2025-03-03 04:39:51 +00:00
import { InteractionResponseTypes, InteractionCallbackOptions, InteractionCallbackData, InteractionTypes, MessageFlags } from "discordeno";
2025-03-03 03:56:43 +00:00
2025-03-03 04:39:51 +00:00
export interface InteractionRef {
2025-03-03 03:56:43 +00:00
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 })
}