chomens-bot-js/index.js

49 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const fs = require('fs/promises')
const fileExist = require('./util/file-exists')
const path = require('path')
2022-11-27 02:35:28 -05:00
const { createBot } = require('./bot')
let config
2022-08-14 05:51:45 -04:00
function load () {
// these stuff takes time to load so i move it here
const readline = require('node:readline')
const { stdin: input, stdout: output } = require('node:process')
const rl = readline.createInterface({ input, output })
const { Client, GatewayIntentBits } = require('discord.js')
const { MessageContent, GuildMessages, Guilds } = GatewayIntentBits
2022-08-14 05:51:45 -04:00
const dcclient = new Client({ intents: [Guilds, GuildMessages, MessageContent] })
let bots = []
dcclient.on('ready', () => {
for (const server of config.servers) {
const getBots = () => bots
const setNewBot = (server, bot) => {
bots = bots.filter((eachBot) => eachBot.server.host !== server)
bots.push(bot)
}
createBot(server, config, getBots, setNewBot, dcclient, rl)
2022-11-27 02:35:28 -05:00
}
})
dcclient.login(config.discord.token)
}
// TODO: improve this thing
async function checkConfig () {
if (!await fileExist(path.join(__dirname, 'config.js'))) {
console.error('Config file doesn\'t exist, so the default one was created')
await fs.copyFile(path.join(__dirname, 'default.js'), path.join(__dirname, 'config.js'))
}
config = require('./config')
load()
}
2022-08-14 05:51:45 -04:00
checkConfig()
2022-11-20 07:19:10 -05:00
process.on('uncaughtException', (e) => {
2022-11-30 06:01:46 -05:00
console.log('uncaught ' + e.stack)
2022-11-27 02:35:28 -05:00
})