-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
107 lines (92 loc) · 3.8 KB
/
index.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
//Global Defined
global.Discord = require("discord.js");
global.fs = require("node-fs-extra");
global.spawn = require('child_process').spawn;
global.client = new Discord.Client({disableEveryone: true});
global.config = require("./config.json");
global.exec = require('child_process').exec;
global.child_children = [];
//Database - Quick.DB
const db = require('quick.db');
global.settings = new db.table("settings"); //Guild settings, Stored: Prefix, GuildID, Admins only
global.userSettings = new db.table("userSettings"); //User settings, Stored: Premium User, Dedicated Port, Premium Ram, Server
global.servers = new db.table("servers"); //Running servers, Stopped servers and also a console link
//Post stats to websites
const DanBotHosting = require("danbot-hosting");
//Client event ready
client.on("ready", async () => {
console.log(client.user.username + " is online with " + client.guilds.cache.size + " servers!");
await client.user.setActivity('Minecraft | Free Minecraft Servers!')
settings.set('476731541167407106', {
"prefix": config.DiscordBot.defaultprefix,
"adminonly": "false"
});
//Start posting stats (DanBotHosting)
const API = new DanBotHosting.Client(config.DanBotHosting.apikey, client);
// Start posting
let initalPost = await API.autopost();
if (initalPost) {
console.error(initalPost); // console the error
};
//Automatic 30second git pull.
setInterval(() => {
exec(`git pull`, (error, stdout) => {
let response = (error || stdout);
if (!error) {
if (response.includes("Already up to date.")) {
//console.log('Bot already up to date. No changes since last pull')
} else {
client.channels.cache.get('783799656722595842').send('**[AUTOMATIC]** \nNew update on GitHub. Pulling. \n\nLogs: \n```' + response + "```" + "\n\n\n**Restarting bot**")
setTimeout(() => {
process.exit();
}, 1000)
};
}
})
}, 30000)
});
//Client event guild join
client.on("guildCreate", (guild) => {
//set guild settings
settings.set(guild.id, {
"prefix": config.DiscordBot.defaultprefix,
"adminonly": "false"
});
//Log new guild join in console
client.channels.cache.get('783799548395913246').send('Joined: ' + guild.name + "(ID: " + guild.id + ") \nGuild owner: " + guild.owner + " \n\nTotal: " + client.guilds.cache.size)
});
//Command handler
client.on('message', message => {
if (message.channel.type === 'dm') {
minecraftServerProcess.stdin.write(message.content);
}
if (message.author.bot) return;
if (message.author.id === "229367793479319553") {
const prefix = settings.get(message.guild.id).prefix;
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const commandargs = message.content.split(' ').slice(1).join(' ');
const command = args.shift().toLowerCase();
console.log(`[${message.author.username}] [${message.author.id}] >> ${prefix}${command} ${commandargs}`);
try {
let commandFile = require(`./commands/${command}.js`);
commandFile.run(client, message, args);
} catch (err) {
if (err instanceof Error && err.code === "MODULE_NOT_FOUND") {
return;
}
}
}
})
//Stop all servers on bot shutdown
process.on('exit', function() {
console.log('killing', child_children.length, 'child processes');
child_children.forEach(function(child) {
child.kill();
});
servers.set('totalstats', {
running: "0",
stopped: "0"
})
});
client.login(config.DiscordBot.token);