Changed arround some stuff

This commit is contained in:
plane000
2018-05-20 19:05:27 +01:00
parent 9afa7c45d4
commit 3c0a57a64b
7 changed files with 113 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
const Logger = require('./logger'); const Logger = require('./logger');
const Config = require('./config'); const Config = require('./config');
const Commands = require('./commands/commands') const Commands = require('./commands/commands')
const RuleCommands = require('./commands/rulecommands')
const CommandManager = require('./main'); const CommandManager = require('./main');
const http = require('http'); const http = require('http');
const ping = require('ping'); const ping = require('ping');
@@ -12,7 +13,7 @@ var commands = {};
module.exports.commands = commands; module.exports.commands = commands;
function addCommand(name, command, alt, usage, desc, requrePerms, functionReference) { function addCommand(name, command, alt, usage, desc, requrePerms, functionReference) {
commands[name] = { commands[command] = {
name: name, name: name,
command: command, command: command,
alt: alt, alt: alt,
@@ -26,8 +27,15 @@ function addCommand(name, command, alt, usage, desc, requrePerms, functionRefere
/*command name, command, alt, usage, description, require permission, reference*/ /*command name, command, alt, usage, description, require permission, reference*/
module.exports.loadCommands = function() { module.exports.loadCommands = function() {
//general commands //general commands
addCommand('say', 'say', undefined, 'say [input]', 'repeats the input', false, Commands.say); addCommand('Say', 'say', undefined, 'say [input]', 'repeats the input', false, Commands.say);
addCommand('version', 'version', undefined, 'version', 'returns the version', false, Commands.version); addCommand('Version', 'version', undefined, 'version', 'returns the version', false, Commands.version);
addCommand('ping', 'ping', undefined, 'ping', 'returns round trip to discords serves', false, Commands.ping); addCommand('Ping', 'ping', undefined, 'ping', 'returns round trip to discords servers', false, Commands.ping);
//continue
//rule commands
addCommand('Rules', 'rules', undefined, 'rules', 'returns all the rules for the server the command was issued on', false, RuleCommands.rules);
addCommand('Rule', 'rule', undefined, 'rule [rule number]', 'returns corresponding rule for the server the command was issued on', false, RuleCommands.rule);
addCommand('AddRule', 'addrule', undefined, 'addrule [new rule]', 'adds a rule to the rule list for the server the command was issued on', true, RuleCommands.addrule);
addCommand('DelRule', 'delrule', undefined, 'delrule [rule number]', 'deletes the corresponding rule for the server the command was issued on', true, RuleCommands.delrule);
addCommand('EditRule', 'editrule', undefined, 'editrule [rune number] [new rule]', 'edits the corresponding rule for the server the command was issued on', true, RuleCommands.editrule);
} }

View File

@@ -6,7 +6,7 @@ const Config = require('../config');
/*message object, messaage full, message args, discord client*/ /*message object, messaage full, message args, discord client*/
module.exports.say = function(message, msg, args, discordclient) { module.exports.say = function(message, msg, args, discordclient) {
message.channel.send(msg); message.channel.send(msg.slice(4, msg.length));
} }
module.exports.version = function(message, msg, args, discordclient) { module.exports.version = function(message, msg, args, discordclient) {

41
commands/rulecommands.js Normal file
View File

@@ -0,0 +1,41 @@
const Discord = require('discord.js');
const ping = require('ping');
const fs = require('fs');
const Config = require('../config');
/*message object, messaage full, message args, discord client*/
module.exports.rules = function(message, msg, args, discordclient) {
var serverID = message.guild.id;
var serversConfig = Config.getservers;
message.channel.send(`Rules`);
}
module.exports.rule = function(message, msg, args, discordclient) {
var serverID = message.guild.id;
var serversConfig = Config.getservers;
message.channel.send(`Rule`);
}
module.exports.addrule = function(message, msg, args, discordclient) {
var serverID = message.guild.id;
var serversConfig = Config.getservers;
message.channel.send(`AddRule`);
}
module.exports.delrule = function(message, msg, args, discordclient) {
var serverID = message.guild.id;
var serversConfig = Config.getservers;
message.channel.send(`DelRule`);
}
module.exports.editrule = function(message, msg, args, discordclient) {
var serverID = message.guild.id;
var serversConfig = Config.getservers;
message.channel.send(`EditRule`);
}

View File

@@ -22,30 +22,32 @@ module.exports.setservers = function(con) {
module.exports.addServer = function(guild) { module.exports.addServer = function(guild) {
servers[guild.id] = { servers[guild.id] = {
name: guild.name, name: guild.name,
rules: [] rules: [],
adminroles: []
} }
} }
module.exports.loadDefaults = function() { module.exports.loadDefaults = function() {
config = { config = {
Token: "DISCORD_TOKEN", Token: 'DISCORD_TOKEN',
NowPlaying: "RealLife.exe", NowPlaying: 'RealLife.exe',
Prefix: "+", Prefix: '+',
Version: "2.0.1", Version: '2.0.1',
}; };
servers['SERVER ID'] = { servers['SERVER ID'] = {
name: 'SERVER NAME', name: 'SERVER NAME',
rules: ['RULE 0', 'RULE 1'] rules: ['RULE 0', 'RULE 1'],
adminroles: ['123456789101112', '123456789101112']
} }
}; };
module.exports.loadFromFile = function() { module.exports.loadFromFile = function() {
config = JSON.parse(fs.readFileSync("resources/config.json")); config = JSON.parse(fs.readFileSync('resources/config.json'));
servers = JSON.parse(fs.readFileSync("resources/servers.json")); servers = JSON.parse(fs.readFileSync('resources/servers.json'));
} }
module.exports.writeToFile = function() { module.exports.writeToFile = function() {
fs.writeFileSync("resources/config.json", JSON.stringify(config, null, 4)); fs.writeFileSync('resources/config.json', JSON.stringify(config, null, 4));
fs.writeFileSync("resources/servers.json", JSON.stringify(servers, null, 4)); fs.writeFileSync('resources/servers.json', JSON.stringify(servers, null, 4));
} }

0
helper.js Normal file
View File

View File

@@ -1,6 +1,7 @@
const colors = require('colors'); const colors = require('colors');
colors.setTheme({ colors.setTheme({
brace: 'white',
input: 'grey', input: 'grey',
prompt: 'cyan', prompt: 'cyan',
info: 'green', info: 'green',
@@ -44,7 +45,9 @@ module.exports.success = function(msg) {
console.log( console.log(
'[' + time + '] ' '[' + time + '] '
+ msg + msg
+ ' [SUCCESS]'.info + ' ['.brace
+ 'SUCCESS' .info
+ ']'.brace
); );
} }
@@ -53,7 +56,20 @@ module.exports.failed = function(msg) {
console.log( console.log(
'[' + time + '] ' '[' + time + '] '
+ msg + msg
+ ' [WARN]'.warn + ' ['.brace
+ 'FAILED' .error
+ ']'.brace
);
}
module.exports.warn = function(msg) {
var time = getTime();
console.log(
'[' + time + '] '
+ msg
+ ' ['.brace
+ 'WARN' .warn
+ ']'.brace
); );
} }
@@ -68,6 +84,9 @@ module.exports.welcome = function() {
__/ | __/ |
|___/ |___/
`.rainbow); `.rainbow);
console.log('Invite JefferyBot to your server! '
+ 'https://discordapp.com/api/oauth2/authorize?client_id=423592273151000576&permissions=506981494&scope=bot' .debug);
console.log();
} }
function pad(n, width, z) { function pad(n, width, z) {
@@ -78,6 +97,6 @@ function pad(n, width, z) {
function getTime() { function getTime() {
var t = new Date(); var t = new Date();
var time = (pad(t.getHours(), 2) + ":" + pad(t.getMinutes(), 2) + ":" + pad(t.getSeconds(), 2)) var time = (pad(t.getHours(), 2) + ':' + pad(t.getMinutes(), 2) + ':' + pad(t.getSeconds(), 2))
return time; return time;
} }

38
main.js
View File

@@ -11,6 +11,7 @@ const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
Logger.welcome(); Logger.welcome();
/*checks if config exists*/ /*checks if config exists*/
if (!fs.existsSync('resources/config.json') || !fs.existsSync('resources/servers.json')) { if (!fs.existsSync('resources/config.json') || !fs.existsSync('resources/servers.json')) {
Logger.log('Creating the config...'); Logger.log('Creating the config...');
@@ -23,7 +24,7 @@ if (!fs.existsSync('resources/config.json') || !fs.existsSync('resources/servers
Config.writeToFile(); Config.writeToFile();
} }
catch (e) { catch (e) {
Logger.failed(`Could not create the config: ${e.message}`); Logger.warn(`Could not create the config: ${e.message}`);
} }
} }
/*loads config*/ /*loads config*/
@@ -33,32 +34,41 @@ try {
Logger.success('Congig loaded'); Logger.success('Congig loaded');
} }
catch (e) { catch (e) {
Logger.failed(`Could not load the config: ${e.message}`); Logger.warn(`Could not load the config: ${e.message}`);
} }
/*loads the commands*/ /*loads the commands*/
try { try {
Logger.log("Loading commands..."); Logger.log('Loading commands...');
CommandManager.loadCommands(); CommandManager.loadCommands();
Logger.success('Commands loaded'); Logger.success('Commands loaded');
} catch (e) { } catch (e) {
Logger.log(`Could not load the commands: ${e.message}`); Logger.warn;(`Could not load the commands: ${e.message}`);
} }
/*connects to discord*/ /*connects to discord*/
try { try {
Logger.log("Starting discord client..."); Logger.log('Starting discord client...');
client.login(Config.getconfig().Token); client.login(Config.getconfig().Token);
} catch (e) { } catch (e) {
Logger.failed(`Could not connect to discord: ${e.message}`) 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*/ /*once connected*/
client.on('ready', () => { client.on('ready', () => {
try { try {
Logger.success('Connected to discords API'); Logger.success('Connected to discords API');
/*adds all servers not in config to config*/
Logger.log('Setting up the server spesific commands...')
client.guilds.array().forEach((g) => {
if (!Config.getservers()[g.id]) {
Config.addServer(g);
}
});
Config.writeToFile();
Logger.success('Server commands set up');
Logger.log('Logging in...') Logger.log('Logging in...')
client.user.setPresence('online'); client.user.setPresence('online');
client.user.setActivity(Config.getconfig().NowPlaying); client.user.setActivity(Config.getconfig().NowPlaying);
@@ -66,7 +76,7 @@ client.on('ready', () => {
Logger.log('Ready!'); Logger.log('Ready!');
console.log(); console.log();
} catch (e) { } catch (e) {
Logger.failed('Somthing went wrong with discords API') Logger.failed(`Somthing went wrong with discords API: ${e.message}`)
} }
}); });
@@ -77,8 +87,8 @@ client.on('message', async (message) => {
/*if it starts with prefix loaded from config*/ /*if it starts with prefix loaded from config*/
if (message.content.startsWith(Config.getconfig().Prefix)) { if (message.content.startsWith(Config.getconfig().Prefix)) {
Logger.logMSG(message); Logger.logMSG(message);
var msg = message.content.substring(Config.getconfig().Prefix.length); var msg = message.content.toLowerCase().substring(Config.getconfig().Prefix.length).toLowerCase();
var args = msg.split(" "); var args = msg.toLowerCase().split(" ");
/*command manager checks if command exists*/ /*command manager checks if command exists*/
if (CommandManager.commands[args[0]]) { if (CommandManager.commands[args[0]]) {
@@ -109,5 +119,5 @@ client.on('guildDelete', async (guild) => {
}); });
client.on('error', async (error) => { client.on('error', async (error) => {
Logger.warn(error); Logger.warn('Somthing went wrong');
}) })