chipmunkbot3/plugins/irc.js

70 lines
1.9 KiB
JavaScript

const irc = require('matrix-org-irc')
const ircStringify = require('../util/chat/stringify/irc')
const CommandSource = require('../util/command/command_source')
function inject (bot, options) {
if (!options.irc?.enabled) return
bot.irc = {
client: options.irc.client,
channel: options.irc.channel,
commandPrefix: options.irc.commandPrefix,
receivedMotd: false
}
if (!bot.irc.client) {
bot.irc.client = new irc.Client(options.irc.server, options.irc.nick, options.irc)
}
const ircPrefix = {
text: 'ChipmunkBot IRC',
hoverEvent: {
action: 'show_text',
value: [
'Server: ',
String(bot.irc.client.server),
'\nChannel: ',
String(bot.irc.channel)
]
}
}
bot.on('chat_irc', (string, msg) => {
if (!bot.irc.receivedMotd || !bot.chat.shouldLog(msg)) return
bot.irc.client.say(bot.irc.channel, string)
})
bot.irc.client.on('motd', () => {
bot.irc.receivedMotd = true
bot.irc.client.once('end', () => { bot.irc.receivedMotd = false })
})
bot.irc.client.on('message' + bot.irc.channel, (sender, msg, command) => {
const senderText = {
text: sender,
insertion: command.prefix,
hoverEvent: {
action: 'show_text',
value: command.prefix
}
}
if (msg.startsWith(bot.irc.commandPrefix)) {
function sendFeedback (text, broadcast) {
const string = ircStringify(text, { lang: bot.registry?.language })
bot.irc.client.say(bot.irc.channel, sender + ': ' + string)
}
const source = new CommandSource({ bot, permissionLevel: 0, sendFeedback, displayName: senderText })
bot.commands.execute(msg.substring(bot.irc.commandPrefix.length), source)
return
}
bot.fancyMsg(ircPrefix, senderText, msg)
})
bot.irc.client.on('error', error => bot.console.error('IRC Error:\n' + error.stack || error))
}
module.exports = inject