Individaul server config set up

This commit is contained in:
plane000
2018-05-20 11:35:49 +01:00
parent 500bd67044
commit 9afa7c45d4
6 changed files with 110 additions and 9 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/resources/
/node_modules/
helper.txt

View File

@@ -1,7 +1,7 @@
const fs = require("fs");
var config = {};
var rules = {};
var servers = {};
module.exports.getconfig = function() {
return config;
@@ -11,6 +11,21 @@ module.exports.setconfig = function(con) {
config = con;
};
module.exports.getservers = function() {
return servers;
};
module.exports.setservers = function(con) {
servers = con;
};
module.exports.addServer = function(guild) {
servers[guild.id] = {
name: guild.name,
rules: []
}
}
module.exports.loadDefaults = function() {
config = {
Token: "DISCORD_TOKEN",
@@ -19,12 +34,18 @@ module.exports.loadDefaults = function() {
Version: "2.0.1",
};
servers['SERVER ID'] = {
name: 'SERVER NAME',
rules: ['RULE 0', 'RULE 1']
}
};
module.exports.loadFromFile = function() {
config = JSON.parse(fs.readFileSync("resources/config.json"));
servers = JSON.parse(fs.readFileSync("resources/servers.json"));
}
module.exports.writeToFile = function() {
fs.writeFileSync("resources/config.json", JSON.stringify(config, null, 4));
fs.writeFileSync("resources/servers.json", JSON.stringify(servers, null, 4));
}

View File

@@ -1,3 +1,15 @@
const colors = require('colors');
colors.setTheme({
input: 'grey',
prompt: 'cyan',
info: 'green',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
module.exports.logMSG = function(msg) {
var time = getTime();
console.log(
@@ -10,22 +22,29 @@ module.exports.logMSG = function(msg) {
+ msg.author.discriminator
+ ' issued the command: '
+ msg.content
.input
);
}
module.exports.logJOIN = function() {
}
module.exports.log = function(msg) {
var time = getTime();
console.log(
'[' + time + '] '
+ msg
.prompt
);
}
module.exports.success = function() {
module.exports.success = function(msg) {
var time = getTime();
console.log(
'[' + time + '] '
+ msg
+ ' [SUCCESS]'.info
);
}
@@ -34,9 +53,23 @@ module.exports.failed = function(msg) {
console.log(
'[' + time + '] '
+ msg
+ ' [WARN]'.warn
);
}
module.exports.welcome = function() {
console.log(`
_ __ __ ____ _
| | / _|/ _| | _ \\ | |
| | ___| |_| |_ ___ _ __ _ _| |_) | ___ | |_
_ | |/ _ \\ _| _/ _ \\ '__| | | | _ < / _ \\| __|
| |__| | __/ | | || __/ | | |_| | |_) | (_) | |_
\\____/ \\___|_| |_| \\___|_| \\__, |____/ \\___/ \\__|
__/ |
|___/
`.rainbow);
}
function pad(n, width, z) {
z = z || '0';
n = n + '';

54
main.js
View File

@@ -1,3 +1,4 @@
const colors = require('colors');
const Logger = require('./logger');
const Config = require('./config');
const Commands = require('./commands/commands')
@@ -8,15 +9,17 @@ const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
Logger.welcome();
/*checks if config exists*/
if (!fs.existsSync('resources/config.json')) {
if (!fs.existsSync('resources/config.json') || !fs.existsSync('resources/servers.json')) {
Logger.log('Creating the config...');
try {
if (!fs.existsSync('resources/')) {
fs.mkdirSync('resources/');
}
Config.loadDefaults();
/*gets all current servers info*/
Config.writeToFile();
}
catch (e) {
@@ -24,9 +27,10 @@ if (!fs.existsSync('resources/config.json')) {
}
}
/*loads config*/
Logger.log('Loading config...');
try {
Logger.log('Loading config...');
Config.loadFromFile();
Logger.success('Congig loaded');
}
catch (e) {
Logger.failed(`Could not load the config: ${e.message}`);
@@ -35,6 +39,7 @@ catch (e) {
try {
Logger.log("Loading commands...");
CommandManager.loadCommands();
Logger.success('Commands loaded');
} catch (e) {
Logger.log(`Could not load the commands: ${e.message}`);
}
@@ -45,15 +50,28 @@ try {
} catch (e) {
Logger.failed(`Could not connect to discord: ${e.message}`)
}
/*adds all servers not in config to config*/
client.guilds.array().forEach((g) => {
Config.addServer(g);
Config.writeToFile();
});
/*once connected*/
client.on('ready', () => {
client.user.setPresence('online');
client.user.setActivity(Config.getconfig().NowPlaying);
Logger.log(`Logged in as ${client.user.tag}`);
Logger.log('Ready!');
console.log();
try {
Logger.success('Connected to discords API');
Logger.log('Logging in...')
client.user.setPresence('online');
client.user.setActivity(Config.getconfig().NowPlaying);
Logger.success(`Logged in as ${client.user.tag}`);
Logger.log('Ready!');
console.log();
} catch (e) {
Logger.failed('Somthing went wrong with discords API')
}
});
/*on message event*/
client.on('message', async (message) => {
/*if it starts with prefix loaded from config*/
@@ -71,3 +89,25 @@ client.on('message', async (message) => {
}
}
});
/*on join server event*/
client.on('guildCreate', async (guild) => {
Logger.log(`JefferyBot was invited to the ${guild.name} server!`);
try {
Logger.log(`Setting up the config for ${guild.name}`)
Config.addServer(guild);
Config.writeToFile();
Logger.success(`Set up the config for the ${guild.name} server`)
} catch (e) {
Logger.failed('Could not set up the server config');
}
});
/*on leave server event*/
client.on('guildDelete', async (guild) => {
Logger.log(`JefferyBot left the ${guild.name} server!`)
});
client.on('error', async (error) => {
Logger.warn(error);
})

5
package-lock.json generated
View File

@@ -9,6 +9,11 @@
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
},
"colors": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.2.5.tgz",
"integrity": "sha512-erNRLao/Y3Fv54qUa0LBB+//Uf3YwMUmdJinN20yMXm9zdKKqH9wt7R9IIVZ+K7ShzfpLV/Zg8+VyrBJYB4lpg=="
},
"discord.js": {
"version": "11.3.2",
"resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.3.2.tgz",

View File

@@ -21,6 +21,7 @@
},
"homepage": "https://github.com/plane000/JefferyBot#readme",
"dependencies": {
"colors": "^1.2.5",
"discord.js": "^11.3.2",
"fs": "0.0.1-security",
"http": "0.0.0",