2024-08-26 14:17:10 +00:00
// @ts-check
const DiscordTypes = require ( "discord-api-types/v10" )
const { discord , sync , db , select , from } = require ( "../../passthrough" )
/** @type {import("../../matrix/api")} */
const api = sync . require ( "../../matrix/api" )
/** @param {DiscordTypes.APIContextMenuGuildInteraction} interaction */
2024-08-27 12:17:54 +00:00
/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */
async function interact ( { id , token , guild _id , channel , data } ) {
2024-08-26 14:17:10 +00:00
const message = from ( "event_message" ) . join ( "message_channel" , "message_id" ) . join ( "channel_room" , "channel_id" )
. select ( "name" , "nick" , "source" , "room_id" , "event_id" ) . where ( { message _id : data . target _id } ) . get ( )
if ( ! message ) {
return discord . snow . interaction . createInteractionResponse ( id , token , {
type : DiscordTypes . InteractionResponseType . ChannelMessageWithSource ,
data : {
content : "This message hasn't been bridged to Matrix." ,
flags : DiscordTypes . MessageFlags . Ephemeral
}
} )
}
2024-08-27 12:17:54 +00:00
const idInfo = ` \n -# Room ID: \` ${ message . room _id } \` \n -# Event ID: \` ${ message . event _id } \` `
2024-08-26 14:17:10 +00:00
if ( message . source === 1 ) { // from Discord
2024-08-27 12:17:54 +00:00
const userID = data . resolved . messages [ data . target _id ] . author . id
2024-08-26 14:17:10 +00:00
return discord . snow . interaction . createInteractionResponse ( id , token , {
type : DiscordTypes . InteractionResponseType . ChannelMessageWithSource ,
data : {
2024-08-27 12:17:54 +00:00
content : ` Bridged <@ ${ userID } > https://discord.com/channels/ ${ guild _id } / ${ channel . id } / ${ data . target _id } on Discord to [ ${ message . nick || message . name } ](<https://matrix.to/#/ ${ message . room _id } / ${ message . event _id } >) on Matrix. `
+ idInfo ,
2024-08-26 14:17:10 +00:00
flags : DiscordTypes . MessageFlags . Ephemeral
}
} )
}
// from Matrix
const event = await api . getEvent ( message . room _id , message . event _id )
return discord . snow . interaction . createInteractionResponse ( id , token , {
type : DiscordTypes . InteractionResponseType . ChannelMessageWithSource ,
data : {
2024-08-27 12:17:54 +00:00
content : ` Bridged [ ${ event . sender } ](<https://matrix.to/#/ ${ event . sender } >)'s message in [ ${ message . nick || message . name } ](<https://matrix.to/#/ ${ message . room _id } / ${ message . event _id } >) on Matrix to https://discord.com/channels/ ${ guild _id } / ${ channel . id } / ${ data . target _id } on Discord. `
+ idInfo ,
2024-08-26 14:17:10 +00:00
flags : DiscordTypes . MessageFlags . Ephemeral
}
} )
}
module . exports . interact = interact