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 }) }