This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
kuro.js
119 lines (91 loc) · 2.98 KB
/
kuro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const fs = require('fs')
const path = require('path')
if (!fs.existsSync(path.join(__dirname, 'config.json'))) {
console.log('config.json file not found, please run kuro-cli or create it on your own')
process.exit()
}
const config = require('./config.json')
const Discord = require('discord.js')
const chalk = require('chalk')
const knex = require('knex')({
client: 'sqlite3',
connection: { filename: path.join(__dirname, 'db') },
useNullAsDefault: true
})
let filesDirectory = path.join(__dirname, 'files')
fs.existsSync(filesDirectory) || fs.mkdirSync(filesDirectory)
// Initializing the ultimate tan
const kuro = new Discord.Client()
// When ready
kuro.on('ready', () => {
// Create database if it doesn't exist
fs.exists('db', (exists) => exists || fs.writeFile('db', ''))
// Getting the database ready
kuro.db = knex
// Making config available on every module
kuro.config = config
kuro.loadCommands()
kuro.user.setAFK(true)
kuro.log('Kuro is ready!', 'green')
})
kuro.on('message', (msg) => {
// Ignore if the message is not ours
if (msg.author.id !== kuro.user.id) return
// Ignore if the message doesn't start with our prefix
if (!msg.content.startsWith(config.prefix)) return
// Ignore if empty command
if (msg.content.length === config.prefix.length) return
// Get all the arguments
let tmp = msg.content.substring(config.prefix.length, msg.length).split(' ')
let args = []
for (let i = 1; i < tmp.length; i++) {
args.push(tmp[i])
}
// Store the command separately
let cmd = tmp[0]
if (kuro.modules.hasOwnProperty(cmd)) return kuro.modules[cmd].run(msg, args)
if (config.commandError.sendToModule === true) {
return kuro.modules[config.commandError.module][config.commandError.function](msg, cmd)
}
return msg.delete()
})
kuro.on('disconnect', () => {
kuro.error('CLIENT: Disconnected!')
process.exit()
})
kuro.on('reconnecting', () => { kuro.log('CLIENT: Reconnecting...', 'green') })
kuro.loadCommands = function() {
kuro.modules = {}
// Load up all the modules
fs.readdirSync('./commands/').forEach((file) => {
let name = file.slice(0, -3)
delete require.cache[require.resolve(`./commands/${file}`)]
try {
kuro.modules[name] = require(`./commands/${file}`)
if (kuro.modules[name].hasOwnProperty('init')) {
kuro.modules[name].init(kuro)
}
kuro.log(`Module ${name} is ready`)
} catch (e) {
kuro.error(`Error in module ${name}:\n${e.stack}`)
}
})
}
kuro.edit = function(msg, content, timeout = 3000) {
if (timeout === 0) return msg.edit(content).catch(console.error)
return msg.edit(content).then(() => {
setTimeout(() => msg.delete().catch(console.error), timeout)
})
}
kuro.log = function(msg, color) {
if (color === undefined) console.log(`[Kuro]: ${msg}`)
else console.log(chalk[color](`[Kuro]: ${msg}`))
}
kuro.error = function(msg) {
console.log(chalk.red(`[Kuro]: ${msg}`))
}
kuro.log('Starting...', 'green')
kuro.login(config.token)
process.on('unhandledRejection', err => {
kuro.error(`Uncaught Promise Error:\n${err.stack}`)
})