2023-06-28 12:06:56 +00:00
const assert = require ( "assert" ) . strict
2023-08-17 13:22:14 +00:00
const util = require ( "util" )
2023-07-03 12:39:42 +00:00
const { sync , db } = require ( "../passthrough" )
2023-05-04 20:25:00 +00:00
/** @type {import("./actions/send-message")}) */
const sendMessage = sync . require ( "./actions/send-message" )
2023-08-17 04:41:28 +00:00
/** @type {import("./actions/edit-message")}) */
const editMessage = sync . require ( "./actions/edit-message" )
2023-08-17 13:22:14 +00:00
/** @type {import("./actions/delete-message")}) */
const deleteMessage = sync . require ( "./actions/delete-message" )
2023-05-09 05:13:59 +00:00
/** @type {import("./actions/add-reaction")}) */
const addReaction = sync . require ( "./actions/add-reaction" )
2023-08-21 11:31:40 +00:00
/** @type {import("./actions/announce-thread")}) */
const announceThread = sync . require ( "./actions/announce-thread" )
2023-08-19 11:12:36 +00:00
/** @type {import("./actions/create-room")}) */
const createRoom = sync . require ( "./actions/create-room" )
2023-08-23 00:37:25 +00:00
/** @type {import("./actions/create-space")}) */
const createSpace = sync . require ( "./actions/create-space" )
2023-08-17 13:22:14 +00:00
/** @type {import("../matrix/api")}) */
const api = sync . require ( "../matrix/api" )
2023-08-24 05:09:25 +00:00
/** @type {import("./discord-command-handler")}) */
const discordCommandHandler = sync . require ( "./discord-command-handler" )
2023-08-17 13:22:14 +00:00
let lastReportedEvent = 0
2023-05-04 20:25:00 +00:00
2023-08-19 06:39:23 +00:00
function isGuildAllowed ( guildID ) {
return [ "112760669178241024" , "497159726455455754" , "1100319549670301727" ] . includes ( guildID )
}
2023-05-04 20:25:00 +00:00
// Grab Discord events we care about for the bridge, check them, and pass them on
2023-04-30 12:57:30 +00:00
2023-04-25 20:06:08 +00:00
module . exports = {
2023-08-17 13:22:14 +00:00
/ * *
* @ param { import ( "./discord-client" ) } client
* @ param { Error } e
* @ param { import ( "cloudstorm" ) . IGatewayMessage } gatewayMessage
* /
onError ( client , e , gatewayMessage ) {
console . error ( "hit event-dispatcher's error handler with this exception:" )
console . error ( e ) // TODO: also log errors into a file or into the database, maybe use a library for this? or just wing it? definitely need to be able to store the formatted event body to load back in later
console . error ( ` while handling this ${ gatewayMessage . t } gateway event: ` )
2023-08-18 04:58:46 +00:00
console . dir ( gatewayMessage . d , { depth : null } )
2023-08-17 13:22:14 +00:00
2023-08-19 06:39:23 +00:00
if ( Date . now ( ) - lastReportedEvent < 5000 ) return
lastReportedEvent = Date . now ( )
const channelID = gatewayMessage . d . channel _id
if ( ! channelID ) return
const roomID = db . prepare ( "SELECT room_id FROM channel_room WHERE channel_id = ?" ) . pluck ( ) . get ( channelID )
if ( ! roomID ) return
let stackLines = e . stack . split ( "\n" )
let cloudstormLine = stackLines . findIndex ( l => l . includes ( "/node_modules/cloudstorm/" ) )
if ( cloudstormLine !== - 1 ) {
stackLines = stackLines . slice ( 0 , cloudstormLine - 2 )
2023-08-17 13:22:14 +00:00
}
2023-08-19 06:39:23 +00:00
api . sendEvent ( roomID , "m.room.message" , {
msgtype : "m.text" ,
body : "\u26a0 Bridged event from Discord not delivered. See formatted content for full details." ,
format : "org.matrix.custom.html" ,
formatted _body : "\u26a0 <strong>Bridged event from Discord not delivered</strong>"
+ ` <br>Gateway event: ${ gatewayMessage . t } `
+ ` <br> ${ e . toString ( ) } `
+ ` <details><summary>Error trace</summary> `
+ ` <pre> ${ stackLines . join ( "\n" ) } </pre></details> `
+ ` <details><summary>Original payload</summary> `
+ ` <pre> ${ util . inspect ( gatewayMessage . d , false , 4 , false ) } </pre></details> ` ,
"m.mentions" : {
user _ids : [ "@cadence:cadence.moe" ]
}
} )
2023-08-17 13:22:14 +00:00
} ,
2023-08-19 10:54:23 +00:00
/ * *
* When logging back in , check if we missed any conversations in any channels . Bridge up to 49 missed messages per channel .
* If more messages were missed , only the latest missed message will be posted . TODO : Consider bridging more , or post a warning when skipping history ?
* This can ONLY detect new messages , not any other kind of event . Any missed edits , deletes , reactions , etc will not be bridged .
* @ param { import ( "./discord-client" ) } client
* @ param { import ( "discord-api-types/v10" ) . GatewayGuildCreateDispatchData } guild
* /
async checkMissedMessages ( client , guild ) {
if ( guild . unavailable ) return
const bridgedChannels = db . prepare ( "SELECT channel_id FROM channel_room" ) . pluck ( ) . all ( )
const prepared = db . prepare ( "SELECT message_id FROM event_message WHERE channel_id = ? AND message_id = ?" ) . pluck ( )
for ( const channel of guild . channels . concat ( guild . threads ) ) {
if ( ! bridgedChannels . includes ( channel . id ) ) continue
if ( ! channel . last _message _id ) continue
const latestWasBridged = prepared . get ( channel . id , channel . last _message _id )
if ( latestWasBridged ) continue
/** More recent messages come first. */
console . log ( ` [check missed messages] in ${ channel . id } ( ${ guild . name } / ${ channel . name } ) because its last message ${ channel . last _message _id } is not in the database ` )
const messages = await client . snow . channel . getChannelMessages ( channel . id , { limit : 50 } )
let latestBridgedMessageIndex = messages . findIndex ( m => {
return prepared . get ( channel . id , m . id )
} )
console . log ( ` [check missed messages] got ${ messages . length } messages; last message that IS bridged is at position ${ latestBridgedMessageIndex } in the channel ` )
if ( latestBridgedMessageIndex === - 1 ) latestBridgedMessageIndex = 1 // rather than crawling the ENTIRE channel history, let's just bridge the most recent 1 message to make it up to date.
for ( let i = Math . min ( messages . length , latestBridgedMessageIndex ) - 1 ; i >= 0 ; i -- ) {
const simulatedGatewayDispatchData = {
guild _id : guild . id ,
mentions : [ ] ,
... messages [ i ]
}
await module . exports . onMessageCreate ( client , simulatedGatewayDispatchData )
}
}
} ,
2023-08-20 20:07:05 +00:00
/ * *
2023-08-21 05:25:51 +00:00
* Announces to the parent room that the thread room has been created .
* See notes . md , "Ignore MESSAGE_UPDATE and bridge THREAD_CREATE as the announcement"
2023-08-20 20:07:05 +00:00
* @ param { import ( "./discord-client" ) } client
2023-08-21 05:25:51 +00:00
* @ param { import ( "discord-api-types/v10" ) . APIThreadChannel } thread
2023-08-20 20:07:05 +00:00
* /
async onThreadCreate ( client , thread ) {
2023-08-21 11:31:40 +00:00
const parentRoomID = db . prepare ( "SELECT room_id FROM channel_room WHERE channel_id = ?" ) . pluck ( ) . get ( thread . parent _id )
2023-08-20 20:07:05 +00:00
if ( ! parentRoomID ) return // Not interested in a thread if we aren't interested in its wider channel
2023-08-21 05:25:51 +00:00
const threadRoomID = await createRoom . syncRoom ( thread . id ) // Create room (will share the same inflight as the initial message to the thread)
await announceThread . announceThread ( parentRoomID , threadRoomID , thread )
2023-08-20 20:07:05 +00:00
} ,
2023-08-23 00:37:25 +00:00
/ * *
* @ param { import ( "./discord-client" ) } client
* @ param { import ( "discord-api-types/v10" ) . GatewayGuildUpdateDispatchData } guild
* /
async onGuildUpdate ( client , guild ) {
const spaceID = db . prepare ( "SELECT space_id FROM guild_space WHERE guild_id = ?" ) . pluck ( ) . get ( guild . id )
if ( ! spaceID ) return
await createSpace . syncSpace ( guild . id )
} ,
2023-08-20 20:07:05 +00:00
/ * *
2023-08-19 11:12:36 +00:00
* @ param { import ( "./discord-client" ) } client
* @ param { import ( "discord-api-types/v10" ) . GatewayChannelUpdateDispatchData } channelOrThread
* @ param { boolean } isThread
* /
async onChannelOrThreadUpdate ( client , channelOrThread , isThread ) {
const roomID = db . prepare ( "SELECT room_id FROM channel_room WHERE channel_id = ?" ) . get ( channelOrThread . id )
if ( ! roomID ) return // No target room to update the data on
await createRoom . syncRoom ( channelOrThread . id )
} ,
2023-04-25 20:06:08 +00:00
/ * *
* @ param { import ( "./discord-client" ) } client
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageCreateDispatchData } message
* /
2023-08-18 04:58:46 +00:00
async onMessageCreate ( client , message ) {
2023-07-03 12:39:42 +00:00
if ( message . webhook _id ) {
const row = db . prepare ( "SELECT webhook_id FROM webhook WHERE webhook_id = ?" ) . pluck ( ) . get ( message . webhook _id )
if ( row ) {
// The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
return
}
}
2023-07-04 20:41:15 +00:00
/** @type {import("discord-api-types/v10").APIGuildChannel} */
const channel = client . channels . get ( message . channel _id )
if ( ! channel . guild _id ) return // Nothing we can do in direct messages.
const guild = client . guilds . get ( channel . guild _id )
2023-08-19 06:39:23 +00:00
if ( ! isGuildAllowed ( guild . id ) ) return
2023-08-24 05:09:25 +00:00
await Promise . all ( [
sendMessage . sendMessage ( message , guild ) ,
discordCommandHandler . execute ( message , channel , guild )
] )
2023-04-25 20:06:08 +00:00
} ,
2023-08-17 04:41:28 +00:00
/ * *
* @ param { import ( "./discord-client" ) } client
2023-08-19 10:54:23 +00:00
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageUpdateDispatchData } data
2023-08-17 04:41:28 +00:00
* /
2023-08-18 04:58:46 +00:00
async onMessageUpdate ( client , data ) {
2023-08-17 07:03:09 +00:00
if ( data . webhook _id ) {
2023-08-17 13:22:14 +00:00
const row = db . prepare ( "SELECT webhook_id FROM webhook WHERE webhook_id = ?" ) . pluck ( ) . get ( data . webhook _id )
2023-08-17 07:03:09 +00:00
if ( row ) {
// The update was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
return
}
}
2023-08-17 04:41:28 +00:00
// Based on looking at data they've sent me over the gateway, this is the best way to check for meaningful changes.
// If the message content is a string then it includes all interesting fields and is meaningful.
if ( typeof data . content === "string" ) {
/** @type {import("discord-api-types/v10").GatewayMessageCreateDispatchData} */
const message = data
/** @type {import("discord-api-types/v10").APIGuildChannel} */
const channel = client . channels . get ( message . channel _id )
if ( ! channel . guild _id ) return // Nothing we can do in direct messages.
const guild = client . guilds . get ( channel . guild _id )
2023-08-19 06:39:23 +00:00
if ( ! isGuildAllowed ( guild . id ) ) return
2023-08-18 04:58:46 +00:00
await editMessage . editMessage ( message , guild )
2023-08-17 04:41:28 +00:00
}
} ,
2023-04-25 20:06:08 +00:00
/ * *
* @ param { import ( "./discord-client" ) } client
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageReactionAddDispatchData } data
* /
2023-08-18 04:58:46 +00:00
async onReactionAdd ( client , data ) {
2023-07-04 20:41:15 +00:00
if ( data . user _id === client . user . id ) return // m2d reactions are added by the discord bot user - do not reflect them back to matrix.
2023-08-25 04:01:19 +00:00
discordCommandHandler . onReactionAdd ( data )
2023-06-28 11:38:58 +00:00
if ( data . emoji . id !== null ) return // TODO: image emoji reactions
2023-08-18 04:58:46 +00:00
await addReaction . addReaction ( data )
2023-08-17 13:22:14 +00:00
} ,
/ * *
* @ param { import ( "./discord-client" ) } client
* @ param { import ( "discord-api-types/v10" ) . GatewayMessageDeleteDispatchData } data
* /
2023-08-18 04:58:46 +00:00
async onMessageDelete ( client , data ) {
await deleteMessage . deleteMessage ( data )
2023-04-25 20:06:08 +00:00
}
}