From b869b432b66ee3c72e427a3ab4972b9bcfb84bdf Mon Sep 17 00:00:00 2001 From: Guzio Date: Tue, 14 Apr 2026 20:52:29 +0000 Subject: [PATCH 1/2] This looks better (I still don't remember what was I doing) --- src/m2d/event-dispatcher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/m2d/event-dispatcher.js b/src/m2d/event-dispatcher.js index 0c2301c..489904d 100644 --- a/src/m2d/event-dispatcher.js +++ b/src/m2d/event-dispatcher.js @@ -221,10 +221,10 @@ async event => { if (bridgedTo) event.room_id = bridgedTo; else if (!await bridgeThread(event)) toRedact = null; - if (toRedact){ + if (toRedact) { api.redactEvent(toRedact, event.event_id) event.content["m.relates_to"] = undefined - api.sendEvent(event.room_id, event.type, {...event.content, body: event.content.body+"\n\n ~ "+event.sender, formatted_body: event.content.formatted_body ? event.content.formatted_body+"

~ "+event.sender :undefined }) + api.sendEvent(event.room_id, event.type, {...event.content, body: event.content.body+"\n ~ "+event.sender, formatted_body: event.content.formatted_body ? event.content.formatted_body+"
~ "+event.sender :undefined }) } } From 7eeff2faf319dad2c9504c64d4656030121e96e5 Mon Sep 17 00:00:00 2001 From: Guzio Date: Tue, 14 Apr 2026 22:49:59 +0000 Subject: [PATCH 2/2] ...So I might as well take care of this mess with commands. Notably: * Don't do the unmarshalling and switch-cases, as Cadence asked * Revert command handler returns to how they were before, now that we're not using the returned-command-name anymore. --- src/m2d/event-dispatcher.js | 4 +- src/matrix/matrix-command-handler.js | 115 ++++++++++----------------- 2 files changed, 43 insertions(+), 76 deletions(-) diff --git a/src/m2d/event-dispatcher.js b/src/m2d/event-dispatcher.js index 489904d..61597f1 100644 --- a/src/m2d/event-dispatcher.js +++ b/src/m2d/event-dispatcher.js @@ -231,10 +231,8 @@ async event => { const messageResponses = await sendEvent.sendEvent(event) if (!messageResponses.length) return - /** @type {string|undefined} */ - let executedCommand if (event.type === "m.room.message" && event.content.msgtype === "m.text" && processCommands) { - executedCommand = await matrixCommandHandler.parseAndExecute( + await matrixCommandHandler.parseAndExecute( // @ts-ignore - TypeScript doesn't know that the event.content.msgtype === "m.text" check ensures that event isn't of type Ty.Event.Outer_M_Room_Message_File (which, indeed, wouldn't fit here) event ) diff --git a/src/matrix/matrix-command-handler.js b/src/matrix/matrix-command-handler.js index d25c54c..408b70a 100644 --- a/src/matrix/matrix-command-handler.js +++ b/src/matrix/matrix-command-handler.js @@ -97,33 +97,6 @@ function replyctx(execute) { } } -/** - * @param {Error & {code?: string|number}} e - * @returns {e} -*/ -function unmarshallDiscordError(e) { - if (e.name === "DiscordAPIError"){ - try{ - const unmarshaled = JSON.parse(e.message) - return { - ...e, - ...unmarshaled - } - } catch (err) { - return { - ...err, - code: "JSON_PARSE_FAILED", - message: JSON.stringify({ - original_error_where_message_failed_to_parse: e, - json_parser_error_message: err.message, - json_parser_error_code: err.code, - }) - } - } - } - return e; -} - /** @type {Command[]} */ const commands = [{ aliases: ["emoji"], @@ -312,55 +285,52 @@ const commands = [{ } try { - if (branchedFromDiscordMessage) await discord.snow.channel.createThreadWithMessage(channelID, branchedFromDiscordMessage, {name: words.slice(1).join(" ")}) - else throw {code: "NO_BRANCH_SOURCE", was_supposed_to_be: branchedFromMxEvent}; + if (branchedFromDiscordMessage) return await discord.snow.channel.createThreadWithMessage(channelID, branchedFromDiscordMessage, {name: words.slice(1).join(" ")}) //can't just return the promise directly like in 99% of other cases here in commands, otherwise the error-handling below will not work + else {return api.sendEvent(event.room_id, "m.room.message", { + ...ctx, + msgtype: "m.text", + body: "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID `"+branchedFromMxEvent+"` on Matrix), so it wasn't created. Either you ran this command on an unbridged message (one sent by this bot or one that failed to bridge due to a previous error), or this is an error on our side and should be reported.", + format: "org.matrix.custom.html", + formatted_body: "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID "+branchedFromMxEvent+" on Matrix), so it wasn't created. Either you ran this command on an unbridged message (one sent by this bot or one that failed to bridge due to a previous error), or this is an error on our side and should be reported." + })}; } catch (e){ - switch (unmarshallDiscordError(e).code) { - case "NO_BRANCH_SOURCE": return api.sendEvent(event.room_id, "m.room.message", { - ...ctx, - msgtype: "m.text", - body: "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID `"+e.was_supposed_to_be+"` on Matrix), so it wasn't created. Either you ran this command on an unbridged message (one sent by this bot or one that failed to bridge due to a previous error), or this is an error on our side and should be reported.", - format: "org.matrix.custom.html", - formatted_body: "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID "+e.was_supposed_to_be+" on Matrix), so it wasn't created. Either you ran this command on an unbridged message (one sent by this bot or one that failed to bridge due to a previous error), or this is an error on our side and should be reported." - }) - - case (160004): // see: https://docs.discord.com/developers/topics/opcodes-and-status-codes - if (isFallingBack){ - await api.sendEvent(event.room_id, "m.room.message", { - ...ctx, - msgtype: "m.text", - body: "⚠️ Discord claims that there already exists a thread for the message you ran this command on, but that doesn't make logical sense, as it doesn't seem like you ran this command on any message. Either your Matrix client did something funny with reply/thread tags, or this is a logic error on OOYE's side. At any rate, this should be reported for further investigation. You should also attach the error message that's about to be sent below (or on the main room timeline, if the command was ran inside a thread).", - }) - throw e; - } - const thread = mxUtils.getThreadRoomFromThreadEvent(branchedFromMxEvent) - return api.sendEvent(event.room_id, "m.room.message", { - ...ctx, - msgtype: "m.text", - body: "There already exists a Discord thread for the message you ran this command on" + (thread ? " - you may join its bridged room here: https://matrix.to/#/"+thread+"?"+(await mxUtils.getViaServersQuery(thread, api)).toString() : ", so a new one cannot be crated. However, it seems like that thread isn't bridged to any Matrix rooms. Please ask the space/server admins to rectify this issue by creating the bridge. (If you're said admin and you can see that said bridge already exists, but this error message is still showing up, please report that as a bug.)") - }) - - case (50024): return api.sendEvent(event.room_id, "m.room.message", { - ...ctx, - msgtype: "m.text", - body: "You cannot create threads in a Discord channel of the type, to which this Matrix room is bridged to. Did you try to create a thread inside a thread?" - }) - - case (50035): return api.sendEvent(event.room_id, "m.room.message", { - ...ctx, - msgtype: "m.text", - body: "Specified thread name is too long - thread creation failed. Please yap a bit less in the title, the thread body is for that. ;)" - }) - - default: + /**@type {string|undefined} */ + let err = e.message // see: https://docs.discord.com/developers/topics/opcodes-and-status-codes + + if (err?.includes("160004")) { + if (isFallingBack){ await api.sendEvent(event.room_id, "m.room.message", { ...ctx, msgtype: "m.text", - body: "⚠️ Unknown error occurred during thread creation. See error message below (or on the main room timeline, if the command was ran inside a thread) for details." + body: "⚠️ Discord claims that there already exists a thread for the message you ran this command on, but that doesn't make logical sense, as it doesn't seem like you ran this command on any message. Either your Matrix client did something funny with reply/thread tags, or this is a logic error on OOYE's side. At any rate, this should be reported for further investigation. You should also attach the error message that's about to be sent below (or on the main room timeline, if the command was ran inside a thread).", }) - throw e + throw e; + } + const thread = mxUtils.getThreadRoomFromThreadEvent(branchedFromMxEvent) + return api.sendEvent(event.room_id, "m.room.message", { + ...ctx, + msgtype: "m.text", + body: "There already exists a Discord thread for the message you ran this command on" + (thread ? " - you may join its bridged room here: https://matrix.to/#/"+thread+"?"+(await mxUtils.getViaServersQuery(thread, api)).toString() : ", so a new one cannot be crated. However, it seems like that thread isn't bridged to any Matrix rooms. Please ask the space/server admins to rectify this issue by creating the bridge. (If you're said admin and you can see that said bridge already exists, but this error message is still showing up, please report that as a bug.)") + }) } + if (err?.includes("50024")) return api.sendEvent(event.room_id, "m.room.message", { + ...ctx, + msgtype: "m.text", + body: "You cannot create threads in a Discord channel of the type, to which this Matrix room is bridged to. Did you try to create a thread inside a thread?" + }) + if (err?.includes("50035")) return api.sendEvent(event.room_id, "m.room.message", { + ...ctx, + msgtype: "m.text", + body: "Specified thread name is too long - thread creation failed. Please yap a bit less in the title, the thread body is for that. ;)" + }) + + await api.sendEvent(event.room_id, "m.room.message", { + ...ctx, + msgtype: "m.text", + body: "⚠️ Unknown error occurred during thread creation. See error message below (or on the main room timeline, if the command was ran inside a thread) for details." + }) + throw e } } ) @@ -422,9 +392,9 @@ const commands = [{ /** * @param {Ty.Event.Outer_M_Room_Message} event - * @returns {Promise} the executed command's name or undefined if no command execution was performed + * @returns {Promise|undefined} the executed command's in-process promise or undefined if no command execution was performed */ -async function parseAndExecute(event) { +function parseAndExecute(event) { let realBody = event.content.body while (realBody.startsWith("> ")) { const i = realBody.indexOf("\n") @@ -444,8 +414,7 @@ async function parseAndExecute(event) { const command = commands.find(c => c.aliases.includes(commandName)) if (!command) return - await command.execute(event, realBody, words) - return words[0] + return command.execute(event, realBody, words) } module.exports.parseAndExecute = parseAndExecute