Compare commits

..

No commits in common. "7eeff2faf319dad2c9504c64d4656030121e96e5" and "44fb3f9f647b2cfe73ac5f3b801528308115d3be" have entirely different histories.

2 changed files with 78 additions and 45 deletions

View file

@ -221,18 +221,20 @@ 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 ~ "+event.sender, formatted_body: event.content.formatted_body ? event.content.formatted_body+"<br> ~ "+event.sender :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+"<br><br> ~ "+event.sender :undefined })
}
}
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) {
await matrixCommandHandler.parseAndExecute(
executedCommand = 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
)

View file

@ -97,6 +97,33 @@ 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"],
@ -285,52 +312,55 @@ const commands = [{
}
try {
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 <code>"+branchedFromMxEvent+"</code> 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."
})};
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};
}
catch (e){
/**@type {string|undefined} */
let err = e.message // see: https://docs.discord.com/developers/topics/opcodes-and-status-codes
if (err?.includes("160004")) {
if (isFallingBack){
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 <code>"+e.was_supposed_to_be+"</code> 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:
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).",
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;
}
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.)")
})
throw e
}
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
}
}
)
@ -392,9 +422,9 @@ const commands = [{
/**
* @param {Ty.Event.Outer_M_Room_Message} event
* @returns {Promise<any>|undefined} the executed command's in-process promise or undefined if no command execution was performed
* @returns {Promise<string|undefined>} the executed command's name or undefined if no command execution was performed
*/
function parseAndExecute(event) {
async function parseAndExecute(event) {
let realBody = event.content.body
while (realBody.startsWith("> ")) {
const i = realBody.indexOf("\n")
@ -414,7 +444,8 @@ function parseAndExecute(event) {
const command = commands.find(c => c.aliases.includes(commandName))
if (!command) return
return command.execute(event, realBody, words)
await command.execute(event, realBody, words)
return words[0]
}
module.exports.parseAndExecute = parseAndExecute