funny-irc-bot/index.js
2025-08-12 18:39:05 +02:00

47 lines
No EOL
1.4 KiB
JavaScript

const config = require('./config.json')
const IRC = require('irc-framework')
const fs = require('node:fs')
const bot = new IRC.Client()
// for convinience's sake
bot.config = config
bot.prefix = config.bot.prefix
bot.commands = []
const commandsFolder = fs.readdirSync('./commands/')
for (const file of commandsFolder) {
const command = require(`./commands/${file}`)
if ('data' in command && 'execute' in command) {
bot.commands[command.data.name] = command
console.log(`loaded command: ${command.data.name}`)
} else {
console.log(`failed to load file ${file}, it's missing "data" or "execute" property`)
}
}
bot.connect({
host: config.irc.server,
port: config.irc.port,
nick: config.irc.nick,
password: config.irc.password
})
bot.on('registered', function() {
console.log('connected ok!')
bot.join(config.irc.channel)
})
bot.on('message', async function(event) {
if (event.message.toString().startsWith(bot.prefix)) {
let sentCommand = event.message.toString().split(bot.prefix)[1].toString().split(' ')[0]
if (bot.commands[sentCommand]) {
try {
await bot.commands[sentCommand].execute(event, bot)
console.log(event)
} catch (err) {
event.reply('Oops, something went horribly wrong when executing this command...')
console.error(err)
}
}
}
})