2024-03-16 16:03:36 -04:00
|
|
|
const matrix = require('matrix-js-sdk')
|
|
|
|
const CommandSource = require('../util/command/command_source')
|
2024-03-17 23:52:58 -04:00
|
|
|
const htmlStringify = require('../util/chat/stringify/html')
|
2024-03-16 16:03:36 -04:00
|
|
|
|
|
|
|
function inject (bot, options) {
|
|
|
|
if (!options.matrix?.enabled) return
|
|
|
|
|
|
|
|
bot.matrix = {
|
|
|
|
client: options.matrix.client ?? matrix.createClient(options.matrix),
|
|
|
|
roomId: options.matrix.roomId,
|
2024-03-17 23:52:58 -04:00
|
|
|
commandPrefix: options.matrix.commandPrefix,
|
|
|
|
inviteUrl: String(options.matrix.inviteUrl)
|
2024-03-16 16:03:36 -04:00
|
|
|
}
|
|
|
|
|
2024-03-17 23:52:58 -04:00
|
|
|
const startTime = Date.now()
|
|
|
|
|
2024-04-02 17:53:10 -04:00
|
|
|
bot.on('chat_html', html => {
|
2024-08-03 21:16:03 -04:00
|
|
|
if (!bot.chat.shouldLog(html)) return
|
2024-03-17 23:52:58 -04:00
|
|
|
sendMessage(html)
|
2024-03-16 16:03:36 -04:00
|
|
|
})
|
|
|
|
|
2024-03-17 23:52:58 -04:00
|
|
|
const matrixPrefix = {
|
|
|
|
text: 'ChipmunkBot Matrix',
|
|
|
|
hoverEvent: {
|
|
|
|
action: 'show_text',
|
|
|
|
contents: 'Click to copy the invite link for the Matrix space to your clipboard!'
|
|
|
|
},
|
|
|
|
clickEvent: {
|
2024-04-02 17:53:10 -04:00
|
|
|
action: 'copy_to_clipboard', // * Minecraft, and Java's URI class in general, seem to hate `#`, so open_url does not work.
|
2024-03-17 23:52:58 -04:00
|
|
|
value: bot.matrix.inviteUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 16:03:36 -04:00
|
|
|
bot.matrix.client.on('Room.timeline', (event, room, toStartOfTimeline) => {
|
2024-05-26 19:38:43 -04:00
|
|
|
if (event.getRoomId() !== bot.matrix.roomId || event.getType() !== 'm.room.message' || event.getTs() < startTime || event.sender.userId === bot.matrix.client.getUserId() || !bot.loggedIn) return
|
2024-03-16 16:03:36 -04:00
|
|
|
|
|
|
|
const content = event.getContent()
|
2024-03-17 23:52:58 -04:00
|
|
|
const permissionLevel = event.sender.powerLevelNorm
|
2024-03-16 16:03:36 -04:00
|
|
|
let message = content.body
|
|
|
|
|
2024-04-02 17:53:10 -04:00
|
|
|
const senderText = {
|
|
|
|
text: String(event.sender.rawDisplayName || event.sender.name || event.sender.userId),
|
|
|
|
hoverEvent: {
|
|
|
|
action: 'show_text',
|
|
|
|
contents: [String(event.sender.userId), '\nPermission Level: ', String(permissionLevel), '\n\nClick to copy the User ID']
|
|
|
|
},
|
|
|
|
clickEvent: {
|
|
|
|
action: 'copy_to_clipboard',
|
|
|
|
value: String(event.sender.userId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 16:03:36 -04:00
|
|
|
if (content.url) {
|
|
|
|
message = {
|
|
|
|
text: '[Attachment]',
|
2024-04-02 17:53:10 -04:00
|
|
|
...bot.styles.primary,
|
2024-03-16 16:03:36 -04:00
|
|
|
clickEvent: {
|
|
|
|
action: 'open_url',
|
|
|
|
value: bot.matrix.client.mxcUrlToHttp(content.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (message.startsWith(bot.matrix.commandPrefix)) {
|
2024-04-02 17:53:10 -04:00
|
|
|
const source = new CommandSource({ bot, permissionLevel, sendFeedback, displayName: senderText })
|
2024-03-16 16:03:36 -04:00
|
|
|
bot.commands.execute(message.substring(bot.matrix.commandPrefix.length), source)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-17 23:52:58 -04:00
|
|
|
bot.fancyMsg(matrixPrefix, senderText, message)
|
2024-03-16 16:03:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
let dequeuingMessages = false
|
|
|
|
let queue = []
|
2024-03-17 23:52:58 -04:00
|
|
|
async function sendMessage (html) {
|
2024-03-16 16:03:36 -04:00
|
|
|
queue.push(html)
|
|
|
|
if (dequeuingMessages) return
|
|
|
|
|
|
|
|
dequeuingMessages = true
|
|
|
|
while (queue.length !== 0) {
|
|
|
|
const html = queue.join('<br>')
|
|
|
|
queue = []
|
|
|
|
await _sendMessage(html)
|
|
|
|
}
|
|
|
|
dequeuingMessages = false
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _sendMessage (html) {
|
|
|
|
const content = {
|
|
|
|
msgtype: 'm.text',
|
|
|
|
format: 'org.matrix.custom.html',
|
|
|
|
body: html,
|
|
|
|
formatted_body: html,
|
|
|
|
'm.mentions': {}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await bot.matrix.client.sendEvent(bot.matrix.roomId, 'm.room.message', content, '')
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Unable to send Matrix message:', error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendFeedback (text, sendFeedback) {
|
2024-03-17 23:52:58 -04:00
|
|
|
const html = htmlStringify(text, { lang: bot.registry.language })
|
|
|
|
sendMessage(html)
|
2024-03-16 16:03:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = inject
|