woomy/index.js

123 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-04-20 04:52:50 +00:00
// Copyright 2020 Emily J. / mudkipscience and contributors. Subject to the AGPLv3 license.
2020-03-29 07:45:09 +00:00
2020-04-09 07:06:12 +00:00
'use strict'
2020-04-18 14:14:11 +00:00
// remove this after we uncomment sentry
/* eslint-disable no-unused-vars */
// Check node.js version
2020-03-30 16:01:13 +00:00
if (Number(process.version.slice(1).split('.')[0]) < 12) {
2020-03-29 07:45:09 +00:00
console.log('NodeJS 12.0.0 or higher is required. Please update NodeJS on your system.')
process.exit()
}
2020-03-31 11:40:51 +00:00
// Libraries
const Discord = require('discord.js')
2020-03-29 07:45:09 +00:00
const fs = require('fs')
2020-04-02 08:17:42 +00:00
const isDocker = require('is-docker')
2020-04-12 09:28:31 +00:00
const sentry = require('@sentry/node')
2020-03-29 07:45:09 +00:00
// Create bot client instance
const client = new Discord.Client({ disabledEvents: ['TYPING_START'] })
// Load all our useful utilities
client.configTemplate = require('./configTemplate')
client.config = require('./config')
client.version = require('./version.json')
client.db = require('./utils/mongoose')
client.logger = require('./utils/logger')
require('./utils/_functions')(client)
2020-03-31 04:09:50 +00:00
// Set missing values in config from config template
2020-04-24 19:06:17 +00:00
for (const c in client.configTemplate) {
if (typeof client.config[c] === 'undefined') {
client.logger.warn('Config value missing: ' + String(c))
client.config[c] = client.configTemplate[c]
}
}
2020-04-20 03:02:10 +00:00
if (typeof client.config.devmode !== 'undefined') { // Check if devmode is explicitly overridden
client.devmode = client.config.devmode
2020-04-24 19:07:49 +00:00
} else { // Check if Woomy is running inside a Docker container
2020-04-19 17:35:09 +00:00
if (isDocker() === false) {
client.devmode = true
} else {
client.devmode = false
}
}
if (client.devmode) {
2020-04-13 04:28:53 +00:00
client.logger.warn('Running in development mode.')
}
// Create caches for permissions, commands, cooldowns and aliases
2020-04-03 14:55:35 +00:00
client.levelCache = {}
client.commands = new Discord.Collection()
client.cooldown = new Discord.Collection()
client.aliases = new Discord.Collection()
// Main initialisation function
2020-04-04 06:59:22 +00:00
const init = async () => {
2020-04-12 09:28:31 +00:00
// initialise sentry
2020-04-13 04:28:53 +00:00
if (client.config.keys.sentry !== '' && client.devmode === false) {
2020-04-18 07:54:01 +00:00
// sentry.init({ dsn: client.config.keys.sentry })
2020-04-13 04:28:53 +00:00
}
2020-04-12 09:28:31 +00:00
2020-04-04 06:59:22 +00:00
// Command handler
fs.readdir('./commands', (err, files) => {
if (err) {
2020-04-18 14:23:50 +00:00
client.logger.error('Failed to get files in commands directory: ' + err)
2020-04-04 06:59:22 +00:00
process.exit()
}
client.logger.info(`Loading ${files.length} commands.`)
files.forEach(file => {
if (!file.endsWith('.js')) {
return
}
2020-04-18 14:14:11 +00:00
const res = client.loadCommand(file)
if (res) {
client.logger.error(res)
2020-04-04 06:59:22 +00:00
}
})
})
2020-03-29 07:45:09 +00:00
2020-04-04 06:59:22 +00:00
// Event handler
fs.readdir('./events', (err, files) => {
if (err) {
2020-04-18 14:23:50 +00:00
client.logger.error('Failed to get files in events directory: ' + err)
2020-04-04 06:59:22 +00:00
process.exit()
}
client.logger.info(`Loading ${files.length} events.`)
files.forEach(file => {
if (!file.endsWith('.js')) {
return
}
const event = require(`./events/${file}`)
client.on(file.substr(0, file.length - 3), event.bind(null, client))
})
})
2020-03-31 16:37:17 +00:00
2020-04-04 06:59:22 +00:00
// Cache client permissions
for (let i = 0; i < client.config.permLevels.length; i++) {
const thisLevel = client.config.permLevels[i]
client.levelCache[thisLevel.name] = thisLevel.level
}
2020-04-04 06:59:22 +00:00
// Initialise DB
await client.db.init(client)
// Login to Discord
2020-04-10 02:57:04 +00:00
function failedToLogin (err) {
client.logger.error('Couldn\'t login: ' + err)
2020-04-10 02:57:04 +00:00
process.exit(0)
}
2020-04-09 07:06:12 +00:00
2020-04-04 06:59:22 +00:00
if (client.devmode !== true) {
2020-04-09 07:06:12 +00:00
client.login(client.config.token).catch(failedToLogin)
2020-04-04 06:59:22 +00:00
} else {
2020-04-19 17:35:09 +00:00
client.login(client.config.devtoken).catch(failedToLogin)
2020-04-04 06:59:22 +00:00
}
2020-03-29 07:45:09 +00:00
}
2020-04-04 06:59:22 +00:00
init()