forked from cadence/out-of-your-element
sending events in matrix
This commit is contained in:
parent
6990957c9e
commit
8a0c2b5663
13 changed files with 2764 additions and 10 deletions
|
@ -0,0 +1,22 @@
|
|||
// @ts-check
|
||||
|
||||
const reg = require("../../matrix/read-registration.js")
|
||||
const fetch = require("node-fetch")
|
||||
|
||||
fetch("https://matrix.cadence.moe/_matrix/client/v3/createRoom?user_id=@_ooye_example:cadence.moe", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
invite: ["@cadence:cadence.moe"],
|
||||
is_direct: false,
|
||||
name: "New Bot User Room",
|
||||
preset: "trusted_private_chat"
|
||||
}),
|
||||
headers: {
|
||||
Authorization: `Bearer ${reg.as_token}`
|
||||
}
|
||||
}).then(res => res.text()).then(text => {
|
||||
// {"room_id":"!aAVaqeAKwChjWbsywj:cadence.moe"}
|
||||
console.log(text)
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
})
|
20
d2m/actions/register-user.js
Normal file
20
d2m/actions/register-user.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
// @ts-check
|
||||
|
||||
const reg = require("../../matrix/read-registration.js")
|
||||
const fetch = require("node-fetch")
|
||||
|
||||
fetch("https://matrix.cadence.moe/_matrix/client/v3/register", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
type: "m.login.application_service",
|
||||
username: "_ooye_example"
|
||||
}),
|
||||
headers: {
|
||||
Authorization: `Bearer ${reg.as_token}`
|
||||
}
|
||||
}).then(res => res.text()).then(text => {
|
||||
// {"user_id":"@_ooye_example:cadence.moe","home_server":"cadence.moe","access_token":"XXX","device_id":"XXX"}
|
||||
console.log(text)
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
})
|
27
d2m/actions/send-message.js
Normal file
27
d2m/actions/send-message.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
// @ts-check
|
||||
|
||||
const reg = require("../../matrix/read-registration.js")
|
||||
const makeTxnId = require("../../matrix/txnid.js")
|
||||
const fetch = require("node-fetch")
|
||||
const messageToEvent = require("../converters/message-to-event.js")
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
|
||||
*/
|
||||
function sendMessage(message) {
|
||||
const event = messageToEvent(message)
|
||||
fetch(`https://matrix.cadence.moe/_matrix/client/v3/rooms/!VwVlIAjOjejUpDhlbA:cadence.moe/send/m.room.message/${makeTxnId()}?user_id=@_ooye_example:cadence.moe`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(event),
|
||||
headers: {
|
||||
Authorization: `Bearer ${reg.as_token}`
|
||||
}
|
||||
}).then(res => res.text()).then(text => {
|
||||
// {"event_id":"$4Zxs0fMmYlbo-sTlMmSEvwIs9b4hcg6yORzK0Ems84Q"}
|
||||
console.log(text)
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = sendMessage
|
|
@ -6,21 +6,21 @@ const markdown = require("discord-markdown")
|
|||
* @param {import("discord-api-types/v10").APIMessage} message
|
||||
* @returns {import("../../types").M_Room_Message_content}
|
||||
*/
|
||||
module.exports = function(message) {
|
||||
module.exports = function messageToEvent(message) {
|
||||
const body = message.content
|
||||
const html = markdown.toHTML(body, {
|
||||
discordCallback: {
|
||||
/* discordCallback: {
|
||||
user: Function,
|
||||
channel: Function,
|
||||
role: Function,
|
||||
everyone: Function,
|
||||
here: Function
|
||||
}
|
||||
} */
|
||||
}, null, null)
|
||||
return {
|
||||
msgtype: "m.text",
|
||||
body: body,
|
||||
format: "m.custom.html",
|
||||
format: "org.matrix.custom.html",
|
||||
formatted_body: html
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
// @ts-check
|
||||
|
||||
// Discord library internals type beat
|
||||
|
||||
const passthrough = require("../passthrough")
|
||||
const { sync } = passthrough
|
||||
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
// @ts-check
|
||||
|
||||
// Grab Discord events we care about for the bridge, check them, and pass them on
|
||||
|
||||
const sendMessage = require("./actions/send-message")
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @param {import("./discord-client")} client
|
||||
|
@ -9,7 +13,7 @@ module.exports = {
|
|||
console.log(message)
|
||||
console.log(message.guild_id)
|
||||
console.log(message.member)
|
||||
return {}
|
||||
sendMessage(message)
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue