woomy/index.js

118 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-03-29 07:45:09 +00:00
// Woomy version 2
// Copyright 2020 mudkipscience
// 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')
const client = new Discord.Client({ disabledEvents: ['TYPING_START'] })
2020-03-29 07:45:09 +00:00
const fs = require('fs')
2020-03-31 07:59:09 +00:00
const colors = require('colors')
2020-04-02 08:17:42 +00:00
const isDocker = require('is-docker')
2020-03-29 07:45:09 +00:00
// Helpers
client.config = require('./config')
client.version = require('./version.json')
client.db = require('./helpers/mongoose')
require('./helpers/_functions')(client)
require('./helpers/music')(client)
// Initialise logger
2020-03-31 07:59:09 +00:00
client.logger = require('tracer').colorConsole({
2020-04-04 16:13:18 +00:00
transport: function (data) {
console.log(data.output)
fs.appendFile('./file.log', data.rawoutput + '\n', err => {
if (err) throw err
})
},
2020-03-31 07:59:09 +00:00
format: [
2020-04-03 06:26:16 +00:00
'{{timestamp}} | {{title}} | {{file}} | {{message}}',
2020-03-31 08:24:51 +00:00
{
2020-04-03 06:26:16 +00:00
debug: `{{timestamp}} | ${'{{title}}'.magenta} | {{file}} | {{message}}`,
log: `{{timestamp}} | ${'{{title}}'.white} | {{file}} | {{message}}`,
info: `{{timestamp}} | ${'{{title}}'.cyan} | {{file}} | {{message}}`,
ready: `{{timestamp}} | ${'{{title}}'.green} | {{file}} | {{message}}`,
warn: `{{timestamp}} | ${'{{title}}'.yellow} | {{file}} | {{message}}`,
error: `{{timestamp}} | ${'{{title}}'.red} | {{file}} | {{message}}`,
fatal: `{{timestamp}} | ${'{{title}}'.red.bold} | {{file}} | {{message}}`
2020-03-31 08:24:51 +00:00
}
2020-03-31 07:59:09 +00:00
],
2020-04-01 08:33:02 +00:00
dateformat: 'yyyy-mm-dd"T"HH:MM:ss',
2020-03-31 07:59:09 +00:00
methods: ['log', 'debug', 'info', 'ready', 'warn', 'error', 'fatal'],
2020-03-31 15:45:45 +00:00
filters: [colors.white]
2020-03-31 07:59:09 +00:00
})
2020-03-31 04:09:50 +00:00
// 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 () => {
// Command handler
fs.readdir('./commands', (err, files) => {
if (err) {
client.logger.fatal('Failed to get files in commands directory: ' + err)
process.exit()
}
client.logger.info(`Loading ${files.length} commands.`)
files.forEach(file => {
if (!file.endsWith('.js')) {
return
}
const response = client.loadCommand(file)
if (response) {
client.logger.error(response)
}
})
})
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) {
client.logger.fatal('Failed to get files in events directory: ' + err)
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
}
// Check if Woomy is running inside a Docker container
2020-04-04 06:59:22 +00:00
if (isDocker() === true) {
client.devmode = true
client.logger.warn('Running in development mode.')
} else {
client.devmode = false
}
// Initialise DB
await client.db.init(client)
// Login to Discord
if (client.devmode !== true) {
client.login(client.config.token)
} else {
client.login(client.config.token_dev)
}
2020-03-29 07:45:09 +00:00
}
2020-04-04 06:59:22 +00:00
init()