CommandManager functioning
This commit is contained in:
@@ -1 +1,33 @@
|
||||
// TODO: commandmanager
|
||||
const Logger = require('./logger');
|
||||
const Config = require('./config');
|
||||
const Commands = require('./commands/commands')
|
||||
const CommandManager = require('./main');
|
||||
const http = require('http');
|
||||
const ping = require('ping');
|
||||
const fs = require('fs');
|
||||
const Discord = require('discord.js');
|
||||
|
||||
var commands = {};
|
||||
|
||||
module.exports.commands = commands;
|
||||
|
||||
function addCommand(name, command, alt, usage, desc, requrePerms, functionReference) {
|
||||
commands[name] = {
|
||||
name: name,
|
||||
command: command,
|
||||
alt: alt,
|
||||
usage: usage,
|
||||
desc: desc,
|
||||
requrePerms: requrePerms,
|
||||
functionReference: functionReference
|
||||
}
|
||||
}
|
||||
|
||||
/*command name, command, alt, usage, description, require permission, reference*/
|
||||
module.exports.loadCommands = function() {
|
||||
//general commands
|
||||
addCommand('say', 'say', undefined, 'say [input]', 'repeats the input', false, Commands.say);
|
||||
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);
|
||||
//continue
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ const Config = require('../config');
|
||||
/*message object, messaage full, message args, discord client*/
|
||||
|
||||
module.exports.say = function(message, msg, args, discordclient) {
|
||||
|
||||
message.channel.send(msg);
|
||||
}
|
||||
|
||||
module.exports.version = function(message, msg, args, discordclient) {
|
||||
@@ -15,7 +15,6 @@ module.exports.version = function(message, msg, args, discordclient) {
|
||||
em.setTitle('Version:');
|
||||
em.setDescription(Config.getconfig().Version);
|
||||
message.channel.send(em);
|
||||
return;
|
||||
}
|
||||
|
||||
module.exports.ping = function(message, msg, args, discordclient) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const fs = require("fs");
|
||||
|
||||
var config = null;
|
||||
|
||||
var config = {};
|
||||
var rules = {};
|
||||
|
||||
module.exports.getconfig = function() {
|
||||
return config;
|
||||
@@ -17,9 +17,8 @@ module.exports.loadDefaults = function() {
|
||||
NowPlaying: "RealLife.exe",
|
||||
Prefix: "+",
|
||||
Version: "2.0.1",
|
||||
|
||||
Rules: ["Rule 0", "Rule 1"]
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
module.exports.loadFromFile = function() {
|
||||
|
||||
38
main.js
38
main.js
@@ -1,5 +1,6 @@
|
||||
const Logger = require('./logger');
|
||||
const Config = require('./config');
|
||||
const Commands = require('./commands/commands')
|
||||
const CommandManager = require('./commandmanager');
|
||||
const http = require('http');
|
||||
const ping = require('ping');
|
||||
@@ -26,11 +27,17 @@ if (!fs.existsSync('resources/config.json')) {
|
||||
Logger.log('Loading config...');
|
||||
try {
|
||||
Config.loadFromFile();
|
||||
|
||||
}
|
||||
catch (e) {
|
||||
Logger.failed(`Could not load the config: ${e.message}`);
|
||||
}
|
||||
/*loads the commands*/
|
||||
try {
|
||||
Logger.log("Loading commands...");
|
||||
CommandManager.loadCommands();
|
||||
} catch (e) {
|
||||
Logger.log(`Could not load the commands: ${e.message}`);
|
||||
}
|
||||
/*connects to discord*/
|
||||
try {
|
||||
Logger.log("Starting discord client...");
|
||||
@@ -43,37 +50,24 @@ client.on('ready', () => {
|
||||
client.user.setPresence('online');
|
||||
client.user.setActivity(Config.getconfig().NowPlaying);
|
||||
Logger.log(`Logged in as ${client.user.tag}`);
|
||||
Logger.log('Ready!')
|
||||
Logger.log('Ready!');
|
||||
console.log();
|
||||
});
|
||||
|
||||
/*on message event*/
|
||||
client.on('message', async (message) => {
|
||||
/*if it starts with prefix*/
|
||||
/*if it starts with prefix loaded from config*/
|
||||
if (message.content.startsWith(Config.getconfig().Prefix)) {
|
||||
Logger.logMSG(message);
|
||||
var msg = message.content.substring(Config.getconfig().Prefix.length);
|
||||
var args = msg.split(" ");
|
||||
|
||||
/*temp switch statement to manage commands*/
|
||||
switch (args[0].toUpperCase()) {
|
||||
case 'PING':
|
||||
ping.promise.probe("discordapp.com", {
|
||||
timeout: 10
|
||||
}).then((output) => {
|
||||
message.channel.send(`:white_check_mark: \`${output.avg}ms\``);
|
||||
});
|
||||
return;
|
||||
case 'VERSION':
|
||||
var em = new Discord.RichEmbed();
|
||||
em.setColor('BLUE');
|
||||
em.setTitle('Version:');
|
||||
em.setDescription(Config.getconfig().Version);
|
||||
message.channel.send(em);
|
||||
return;
|
||||
default:
|
||||
message.channel.send(`:no_entry: \`The command '${args[0]}' does not exist...\``);
|
||||
break;
|
||||
/*command manager checks if command exists*/
|
||||
if (CommandManager.commands[args[0]]) {
|
||||
/*sends command: message object, messaage full, message args, discord client*/
|
||||
CommandManager.commands[args[0]].functionReference(message, msg, args, client);
|
||||
} else {
|
||||
message.channel.send(`:no_entry_sign: \`The command \'${args[0]}\' does not exist\``);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user