forked from cadence/out-of-your-element
Comparison of all the stuff I did. #6
2 changed files with 70 additions and 8 deletions
|
|
@ -222,9 +222,9 @@ async event => {
|
||||||
}
|
}
|
||||||
if (event.content["m.relates_to"]?.rel_type === "m.thread" && executedCommand !== "thread"){
|
if (event.content["m.relates_to"]?.rel_type === "m.thread" && executedCommand !== "thread"){
|
||||||
api.sendEvent(event.room_id, "m.room.message", {
|
api.sendEvent(event.room_id, "m.room.message", {
|
||||||
"body": "⚠️ **This message may not have been bridged to Discord in the way you thought it was gonna be!**\n\nIt seems like you sent this message inside a Matrix thread. Matrix threads don't work like Discord threads - they are effectively just „fancy replies”, not independent rooms/channels (any „thread-like appearance” is handled purely client-side - and even then, most Matrix clients don't handle it particularly well), and as such, they are bridged as replies to Discord. *Discord users will not be aware that you sent this message inside a thread - it will go directly onto the main channel. If the thread you sent this message in is old, such a random reply may be distracting to Discord users!*\n\nFor the sake of Discord parity (and for better support in numerous Matrix clients - as stated above, most Matrix clients don't handle threads particularly well, and they just render in-thread messages as fancy replies), it is recommended to send threaded messages inside a separate Matrix room that gets bridged to Discord. If you sent this message in a pre-existing Matrix thread, please look around to see if anyone has created such a room for it. If this is the first message (ie. the thread was just created) or nobody has made a thread-room for this thread yet, it is recommended you create one with `/thread <Thread Name>` and continue the conversation there. You can even run this command directly in this very Matrix thread, and the new Discord thread will be automatically attached to the same message as this one (alternatively, run that command outside a thread, and as a reply to a different message to attach the thread to that message or stand-alone to create a stand-alone thread).\n\n*You can read more about the rationale behind this design choice [here](https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/threads-as-rooms.md).*",
|
"body": "⚠️ **This message may not have been bridged to Discord in the way you thought it was gonna be!**\n\nIt seems like you sent this message inside a Matrix thread. Matrix threads don't work like Discord threads - they are effectively just „fancy replies”, not independent rooms/channels (any „thread-like appearance” is handled purely client-side - and even then, most Matrix clients don't handle it particularly well), and as such, they are bridged as replies to Discord. *Discord users will not be aware that you sent this message inside a thread - it will go directly onto the main channel. If the thread you sent this message in is old, such a random reply may be distracting to Discord users!*\n\nFor the sake of Discord parity (and for better support in numerous Matrix clients - as stated above, most Matrix clients don't handle threads particularly well, and they just render in-thread messages as fancy replies), it is recommended to send threaded messages inside a separate Matrix room that gets bridged to Discord. If you sent this message in a pre-existing Matrix thread, please look around to see if anyone has created such a room for it. If this is the first message (ie. the thread was just created) or nobody has made a thread-room for this thread yet, it is recommended you create one with `/thread <Thread Name>` and continue the conversation there. You can even run the command directly in this thread to attach the created Discord thread to the same message as this Matrix one (run `/thread` (with no arguments) to see other options for attachment).\n\n*You can read more about the rationale behind this design choice [here](https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/threads-as-rooms.md).*",
|
||||||
"format": "org.matrix.custom.html",
|
"format": "org.matrix.custom.html",
|
||||||
"formatted_body": "⚠️ <strong data-md=\"**\">This message may not have been bridged to Discord in the way you thought it was gonna be!</strong><br/><br/>It seems like you sent this message inside a Matrix thread. Matrix threads don't work like Discord threads - they are effectively just „fancy replies”, not independent rooms/channels (any „thread-like appearance” is handled purely client-side - and even then, most Matrix clients don't handle it particularly well), and as such, they are bridged as replies to Discord. <i data-md=\"*\">Discord users will not be aware that you sent this message inside a thread - it will go directly onto the main channel. If the thread you sent this message in is old, such a random reply may be distracting to Discord users!</i><br/><br/>For the sake of Discord parity (and for better support in numerous Matrix clients - as stated above, most Matrix clients don't handle threads particularly well, and they just render in-thread messages as fancy replies), it is recommended to send threaded messages inside a separate Matrix room that gets bridged to Discord. If you sent this message in a pre-existing Matrix thread, please look around to see if anyone has created such a room for it. If this is the first message (ie. the thread was just created) or nobody has made a thread-room for this thread yet, it is recommended you create one with <code data-md=\"`\">/thread <Thread Name></code> and continue the conversation there. You can even run this command directly in this very Matrix thread, and the new Discord thread will be automatically attached to the same message as this one (alternatively, run that command outside a thread, and as a reply to a different message to attach the thread to that message or stand-alone to create a stand-alone thread).<br/><br/><i data-md=\"*\">You can read more about the rationale behind this design choice <a data-md href=\"https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/threads-as-rooms.md\">here</a>.</i>",
|
"formatted_body": "⚠️ <strong>This message may not have been bridged to Discord in the way you thought it was gonna be!</strong><br><br>It seems like you sent this message inside a Matrix thread. Matrix threads don't work like Discord threads - they are effectively just „fancy replies”, not independent rooms/channels (any „thread-like appearance” is handled purely client-side - and even then, most Matrix clients don't handle it particularly well), and as such, they are bridged as replies to Discord. <em>Discord users will not be aware that you sent this message inside a thread - it will go directly onto the main channel. If the thread you sent this message in is old, such a random reply may be distracting to Discord users!</em><br><br>For the sake of Discord parity (and for better support in numerous Matrix clients - as stated above, most Matrix clients don't handle threads particularly well, and they just render in-thread messages as fancy replies), it is recommended to send threaded messages inside a separate Matrix room that gets bridged to Discord. If you sent this message in a pre-existing Matrix thread, please look around to see if anyone has created such a room for it. If this is the first message (ie. the thread was just created) or nobody has made a thread-room for this thread yet, it is recommended you create one with <code>/thread <Thread Name></code> and continue the conversation there. You can even run the command directly in this thread to attach the created Discord thread to the same message as this Matrix one (run <code>/thread</code> (with no arguments) to see other options for attachment).<br><br><em>You can read more about the rationale behind this design choice <a href=\"https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/threads-as-rooms.md\">here</a>.</em>",
|
||||||
"m.mentions": { "user_ids": [event.sender]},
|
"m.mentions": { "user_ids": [event.sender]},
|
||||||
"m.relates_to": {
|
"m.relates_to": {
|
||||||
"event_id": event.content["m.relates_to"].event_id,
|
"event_id": event.content["m.relates_to"].event_id,
|
||||||
|
|
|
||||||
|
|
@ -236,6 +236,19 @@ const commands = [{
|
||||||
aliases: ["thread"],
|
aliases: ["thread"],
|
||||||
execute: replyctx(
|
execute: replyctx(
|
||||||
async (event, realBody, words, ctx) => {
|
async (event, realBody, words, ctx) => {
|
||||||
|
if (words.length < 2){
|
||||||
|
return api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
...ctx,
|
||||||
|
msgtype: "m.text",
|
||||||
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
|
"body": "**`/thread` usage:**\nRun this command as `/thread <Thread Name>` to create a thread on Discord (and optionally Matrix, if one doesn't exist already). The message under which said thread will be attached, is chosen based on the following rules:\n* If ran stand-alone (not as a reply, nor in a Matrix thread), the created thread gets attached to the command-message itself.\n* If sent as a reply (outside a Matrix thread), the thread will be attached to the message to which you replied.\n* If ran inside an existing Matrix thread (regardless of whether it's a reply or not), the Discord thread will be attached to the same message as the Matrix thread already is.",
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": "<strong><code>/thread</code> usage:</strong><br>Run this command as <code>/thread <Thread Name></code> to create a thread on Discord (and optionally Matrix, if one doesn't exist already). The message under which said thread will be attached, is chosen based on the following rules:<br><ul><li>If ran stand-alone (not as a reply, nor in a Matrix thread), the created thread gets attached to the command-message itself.</li><li>If sent as a reply (outside a Matrix thread), the thread will be attached to the message to which you replied.</li><li>If ran inside an existing Matrix thread (regardless of whether it's a reply or not), the Discord thread will be attached to the same message as the Matrix thread already is.</li></ul>"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Guard
|
// Guard
|
||||||
/** @type {string} */ // @ts-ignore
|
/** @type {string} */ // @ts-ignore
|
||||||
const channelID = select("channel_room", "channel_id", {room_id: event.room_id}).pluck().get()
|
const channelID = select("channel_room", "channel_id", {room_id: event.room_id}).pluck().get()
|
||||||
|
|
@ -244,6 +257,9 @@ const commands = [{
|
||||||
return api.sendEvent(event.room_id, "m.room.message", {
|
return api.sendEvent(event.room_id, "m.room.message", {
|
||||||
...ctx,
|
...ctx,
|
||||||
msgtype: "m.text",
|
msgtype: "m.text",
|
||||||
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
body: "This room isn't bridged to the other side."
|
body: "This room isn't bridged to the other side."
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -255,7 +271,10 @@ const commands = [{
|
||||||
return api.sendEvent(event.room_id, "m.room.message", {
|
return api.sendEvent(event.room_id, "m.room.message", {
|
||||||
...ctx,
|
...ctx,
|
||||||
msgtype: "m.text",
|
msgtype: "m.text",
|
||||||
body: "This command creates a thread on Discord. But you aren't allowed to do this, because if you were a Discord user, you wouldn't have the Create Public Threads permission." // NOTE: Currently, this assumes that all Matrix users have no Discord roles, and makes that „If you were a Discord user...” claim based on that assumption. If Discord permission emulation is gonna happen in the future, this is certainly one among many places that will need to be changed.
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
|
body: "This command creates a thread on Discord. But you aren't allowed to do this, because if you were a Discord user, you wouldn't have the Create Public Threads permission." // NOTE: Currently, this assumes that all Matrix users have no Discord roles (see: empty [] in the getPermissions call above), and makes that „If you were a Discord user...” claim based on that assumption. If Discord permission emulation is gonna happen in the future, this is certainly one among many places that will need to be changed.
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -263,12 +282,55 @@ const commands = [{
|
||||||
let attachedToEvent = relation?.["m.in_reply_to"]?.event_id // By default, attempt to attach the thread to the message to which /thread was replying.
|
let attachedToEvent = relation?.["m.in_reply_to"]?.event_id // By default, attempt to attach the thread to the message to which /thread was replying.
|
||||||
if (relation?.rel_type === "m.thread") attachedToEvent = event.content["m.relates_to"]?.event_id // If /thread was sent inside a Matrix thread, attempt to attach the Discord thread to the message around which that Matrix thread was based on.
|
if (relation?.rel_type === "m.thread") attachedToEvent = event.content["m.relates_to"]?.event_id // If /thread was sent inside a Matrix thread, attempt to attach the Discord thread to the message around which that Matrix thread was based on.
|
||||||
if (!attachedToEvent) attachedToEvent = event.event_id // If /thread wasn't replying to anything (ie. attachedToEvent was undefined at initial assignment), or if the event was somehow malformed (in such a way that that - one way or another - attachedToEvent ended up being undefined, even if according to the spec it shouldn't), attach the thread to the /thread command-message that created it.
|
if (!attachedToEvent) attachedToEvent = event.event_id // If /thread wasn't replying to anything (ie. attachedToEvent was undefined at initial assignment), or if the event was somehow malformed (in such a way that that - one way or another - attachedToEvent ended up being undefined, even if according to the spec it shouldn't), attach the thread to the /thread command-message that created it.
|
||||||
|
|
||||||
assert(attachedToEvent)
|
|
||||||
const attachedToMessage = select("event_message", "message_id", {event_id: attachedToEvent}).pluck().get()
|
const attachedToMessage = select("event_message", "message_id", {event_id: attachedToEvent}).pluck().get()
|
||||||
|
|
||||||
if (attachedToMessage) await discord.snow.channel.createThreadWithMessage(channelID, attachedToMessage, {name: words.slice(1).join(" ")})
|
try {
|
||||||
else await discord.snow.channel.createThreadWithoutMessage(channelID, {type: 11, name: words.slice(1).join(" ")})
|
if (attachedToMessage) await discord.snow.channel.createThreadWithMessage(channelID, attachedToMessage, {name: words.slice(1).join(" ")})
|
||||||
|
else throw "NO_ATTACH_TARGET";
|
||||||
|
}
|
||||||
|
catch (e){
|
||||||
|
if (e === "NO_ATTACH_TARGET") {
|
||||||
|
return api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
...ctx,
|
||||||
|
msgtype: "m.text",
|
||||||
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
|
body: "Couldn't find a Discord representation of the message under which you're trying to attach this thread. 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."
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if (e.code === 160004) { // see: https://docs.discord.com/developers/topics/opcodes-and-status-codes
|
||||||
|
return api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
...ctx,
|
||||||
|
msgtype: "m.text",
|
||||||
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
|
body: "There already exist a thread for the message you ran this command on (please run `/thread` (with no arguments) for details on attachment rules, if you're unsure what „ran this command on” refers to); cannot create a new one. Please scroll up and see where the link could be."
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if (e.code === 50024) {
|
||||||
|
return api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
...ctx,
|
||||||
|
msgtype: "m.text",
|
||||||
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
|
body: "You cannot create a new thread in this type of channel. Did you try to create a thread inside a thread?"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
...ctx,
|
||||||
|
msgtype: "m.text",
|
||||||
|
"m.relates_to":{
|
||||||
|
"m.in_reply_to": event.event_id
|
||||||
|
},
|
||||||
|
body: "Unknown error occurred during thread creation. See error message below (or on the main channel, if the command was ran inside a thread) for details."
|
||||||
|
})
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}]
|
}]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue