-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
86 lines (65 loc) · 2.55 KB
/
bot.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
const Discord = require("discord.js");
const fs = require("fs");
const path = require("path");
require('dotenv').config();
const bot = new Discord.Client({partials: ['MESSAGE', 'USER', 'CHANNEL', 'GUILD_MEMBER', 'REACTION']});
bot.prefix = process.env.PREFIX;
bot.invite = process.env.INVITE;
bot.status = 0;
bot.guildCount = 0;
bot.statuses = [
() => `en!h | in ${bot.guilds.cache.size} guilds!`,
() => `en!h | serving ${bot.users.cache.size} users!`
];
bot.updateStatus = async function(){
var target = bot.statuses[bot.status % bot.statuses.length];
if(typeof target == "function") bot.user.setActivity(await target());
else bot.user.setActivity(target);
bot.status++;
setTimeout(()=> bot.updateStatus(), 60 * 1000) // 5 mins
}
async function setup() {
bot.db = require(__dirname + '/stores/__db')(bot);
files = fs.readdirSync(__dirname + "/events");
files.forEach(f => bot.on(f.slice(0,-3), (...args) => require(__dirname + "/events/"+f)(...args,bot)));
bot.utils = require(__dirname + "/utils");
var data = bot.utils.loadCommands(__dirname + "/commands");
Object.keys(data).forEach(k => bot[k] = data[k]);
}
bot.parseCommand = async function(bot, msg, args) {
if(!args[0]) return undefined;
var command = bot.commands.get(bot.aliases.get(args[0].toLowerCase()));
if(!command) return {command, nargs: args};
args.shift();
if(args[0] && command.subcommands?.get(command.sub_aliases.get(args[0].toLowerCase()))) {
command = command.subcommands.get(command.sub_aliases.get(args[0].toLowerCase()));
args.shift();
}
return {command, nargs: args};
}
bot.writeLog = async (log) => {
let now = new Date();
let ndt = `${(now.getMonth() + 1).toString().length < 2 ? "0"+ (now.getMonth() + 1) : now.getMonth()+1}.${now.getDate().toString().length < 2 ? "0"+ now.getDate() : now.getDate()}.${now.getFullYear()}`;
if(!fs.existsSync('./logs')) fs.mkdirSync('./logs');
if(!fs.existsSync(`./logs/${ndt}.log`)){
fs.writeFile(`./logs/${ndt}.log`,log+"\r\n",(err)=>{
if(err) console.log(`Error while attempting to write log ${ndt}\n`+err.stack);
});
} else {
fs.appendFile(`./logs/${ndt}.log`,log+"\r\n",(err)=>{
if(err) console.log(`Error while attempting to apend to log ${ndt}\n`+err);
});
}
}
bot.on("ready", async ()=> {
console.log('Ender ready!');
bot.updateStatus();
})
bot.on('error', (err)=> {
console.log(`Error:\n${err.stack}`);
bot.writeLog(`=====ERROR=====\r\nStack: ${err.stack}`)
})
process.on("unhandledRejection", (e) => console.log(e));
setup();
bot.login(process.env.TOKEN)
.catch(e => console.log("Trouble connecting:\n"+e));