adding basic reactions to discord messages
This commit is contained in:
parent
da6603d258
commit
4d8b74f61f
4 changed files with 45 additions and 6 deletions
36
d2m/actions/add-reaction.js
Normal file
36
d2m/actions/add-reaction.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const assert = require("assert")
|
||||||
|
|
||||||
|
const passthrough = require("../../passthrough")
|
||||||
|
const { discord, sync, db } = passthrough
|
||||||
|
/** @type {import("../../matrix/api")} */
|
||||||
|
const api = sync.require("../../matrix/api")
|
||||||
|
/** @type {import("./register-user")} */
|
||||||
|
const registerUser = sync.require("./register-user")
|
||||||
|
/** @type {import("../actions/create-room")} */
|
||||||
|
const createRoom = sync.require("../actions/create-room")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data
|
||||||
|
*/
|
||||||
|
async function addReaction(data) {
|
||||||
|
const user = data.member?.user
|
||||||
|
assert.ok(user && user.username)
|
||||||
|
// TODO: should add my own sent messages to event_message so they can be reacted to?
|
||||||
|
const parentID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary
|
||||||
|
if (!parentID) return // TODO: how to handle reactions for unbridged messages? is there anything I can do?
|
||||||
|
assert.equal(typeof parentID, "string")
|
||||||
|
const roomID = await createRoom.ensureRoom(data.channel_id)
|
||||||
|
const senderMxid = await registerUser.ensureSimJoined(user, roomID)
|
||||||
|
const eventID = api.sendEvent(roomID, "m.reaction", {
|
||||||
|
"m.relates_to": {
|
||||||
|
rel_type: "m.annotation",
|
||||||
|
event_id: parentID,
|
||||||
|
key: data.emoji.name
|
||||||
|
}
|
||||||
|
}, senderMxid)
|
||||||
|
return eventID
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.addReaction = addReaction
|
|
@ -42,6 +42,7 @@ async function createSim(user) {
|
||||||
/**
|
/**
|
||||||
* Ensure a sim is registered for the user.
|
* Ensure a sim is registered for the user.
|
||||||
* If there is already a sim, use that one. If there isn't one yet, register a new sim.
|
* If there is already a sim, use that one. If there isn't one yet, register a new sim.
|
||||||
|
* @param {import("discord-api-types/v10").APIUser} user
|
||||||
* @returns mxid
|
* @returns mxid
|
||||||
*/
|
*/
|
||||||
async function ensureSim(user) {
|
async function ensureSim(user) {
|
||||||
|
@ -57,6 +58,7 @@ async function ensureSim(user) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure a sim is registered for the user and is joined to the room.
|
* Ensure a sim is registered for the user and is joined to the room.
|
||||||
|
* @param {import("discord-api-types/v10").APIUser} user
|
||||||
* @returns mxid
|
* @returns mxid
|
||||||
*/
|
*/
|
||||||
async function ensureSimJoined(user, roomID) {
|
async function ensureSimJoined(user, roomID) {
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const fetch = require("node-fetch").default
|
|
||||||
const reg = require("../../matrix/read-registration.js")
|
|
||||||
|
|
||||||
const passthrough = require("../../passthrough")
|
const passthrough = require("../../passthrough")
|
||||||
const { discord, sync, db } = passthrough
|
const { discord, sync, db } = passthrough
|
||||||
/** @type {import("../converters/message-to-event")} */
|
/** @type {import("../converters/message-to-event")} */
|
||||||
|
@ -24,8 +21,9 @@ async function sendMessage(message) {
|
||||||
if (!message.webhook_id) {
|
if (!message.webhook_id) {
|
||||||
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
|
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
|
||||||
}
|
}
|
||||||
const eventID = api.sendEvent(roomID, "m.room.message", event, senderMxid)
|
const eventID = await api.sendEvent(roomID, "m.room.message", event, senderMxid)
|
||||||
|
db.prepare("INSERT INTO event_message (event_id, message_id, part) VALUES (?, ?, ?)").run(eventID, message.id, 0) // 0 is primary, 1 is supporting
|
||||||
return eventID
|
return eventID
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.sendMessage = sendMessage
|
module.exports.sendMessage = sendMessage
|
||||||
|
|
|
@ -4,6 +4,8 @@ const {sync} = require("../passthrough")
|
||||||
|
|
||||||
/** @type {import("./actions/send-message")}) */
|
/** @type {import("./actions/send-message")}) */
|
||||||
const sendMessage = sync.require("./actions/send-message")
|
const sendMessage = sync.require("./actions/send-message")
|
||||||
|
/** @type {import("./actions/add-reaction")}) */
|
||||||
|
const addReaction = sync.require("./actions/add-reaction")
|
||||||
|
|
||||||
// Grab Discord events we care about for the bridge, check them, and pass them on
|
// Grab Discord events we care about for the bridge, check them, and pass them on
|
||||||
|
|
||||||
|
@ -22,7 +24,8 @@ module.exports = {
|
||||||
* @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data
|
* @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data
|
||||||
*/
|
*/
|
||||||
onReactionAdd(client, data) {
|
onReactionAdd(client, data) {
|
||||||
|
if (data.emoji.id !== null) return // TOOD: image emoji reactions
|
||||||
console.log(data)
|
console.log(data)
|
||||||
return {}
|
addReaction.addReaction(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue