prettified code

This commit is contained in:
Niko Storni 2017-11-23 02:01:52 +01:00
parent a199564da6
commit 95ea4f403e
2 changed files with 34 additions and 46 deletions

View file

@ -1,82 +1,70 @@
'use strict';
// Load up libraries
const Discord = require("discord.js");
const Discord = require('discord.js');
// Load config!
let config = require('config');
config = config.get('bot');
var aliases;
try {
aliases = require("./alias.json");
aliases = require('./alias.json');
} catch (e) {
//No aliases defined
aliases = {
"test": {
process: function(bot,msg){
test: {
process: function(bot, msg) {
msg.channel.send('test');
}
}
};
}
}
var commands = {
};
var commands = {};
var bot = new Discord.Client();
bot.on("ready", function() {
console.log(
"Logged in! Serving in " + bot.guilds.array().length + " servers"
);
require("./plugins.js").init();
console.log("type " + config.prefix + "help in Discord for a commands list.");
bot.user.setGame(
config.prefix + "tip"
);
bot.on('ready', function() {
console.log('Logged in! Serving in ' + bot.guilds.array().length + ' servers');
require('./plugins.js').init();
console.log('type ' + config.prefix + 'help in Discord for a commands list.');
bot.user.setGame(config.prefix + 'tip');
});
bot.on("disconnected", function() {
console.log("Disconnected!");
bot.on('disconnected', function() {
console.log('Disconnected!');
process.exit(1); //exit node.js with an error
});
function checkMessageForCommand(msg, isEdit) {
//check if message is a command
if (msg.author.id != bot.user.id && msg.content.startsWith(config.prefix)) {
console.log(
"treating " + msg.content + " from " + msg.author + " as command"
);
var cmdTxt = msg.content.split(" ")[0].substring(config.prefix.length);
var suffix = msg.content.substring(
cmdTxt.length + config.prefix.length + 1
); //add one for the ! and one for the space
console.log('treating ' + msg.content + ' from ' + msg.author + ' as command');
var cmdTxt = msg.content.split(' ')[0].substring(config.prefix.length);
var suffix = msg.content.substring(cmdTxt.length + config.prefix.length + 1); //add one for the ! and one for the space
if (msg.isMentioned(bot.user)) {
try {
cmdTxt = msg.content.split(" ")[1];
suffix = msg.content.substring(
bot.user.mention().length + cmdTxt.length + config.prefix.length + 1
);
cmdTxt = msg.content.split(' ')[1];
suffix = msg.content.substring(bot.user.mention().length + cmdTxt.length + config.prefix.length + 1);
} catch (e) {
//no command
msg.channel.send("Yes?");
msg.channel.send('Yes?');
return;
}
}
let alias = aliases[cmdTxt];
if (alias) {
var cmd = alias;
var cmd = alias;
} else {
var cmd = commands[cmdTxt];
var cmd = commands[cmdTxt];
}
if (cmd) {
// Add permission check here later on ;)
try {
cmd.process(bot, msg, suffix, isEdit);
} catch (e) {
var msgTxt = "command " + cmdTxt + " failed :(";
var msgTxt = 'command ' + cmdTxt + ' failed :(';
if (config.debug) {
msgTxt += "\n" + e.stack;
msgTxt += '\n' + e.stack;
}
msg.channel.send(msgTxt);
}
@ -89,13 +77,13 @@ function checkMessageForCommand(msg, isEdit) {
}
if (msg.author != bot.user && msg.isMentioned(bot.user)) {
msg.channel.send("yes?"); //using a mention here can lead to looping
msg.channel.send('yes?'); //using a mention here can lead to looping
} else {
}
}
}
bot.on("message", msg => checkMessageForCommand(msg, false));
bot.on('message', msg => checkMessageForCommand(msg, false));
/*bot.on("messageUpdate", (oldMessage, newMessage) => {
checkMessageForCommand(newMessage, true);
});*/
@ -113,9 +101,9 @@ exports.addCustomFunc = function(customFunc) {
} catch (err) {
console.log(err);
}
}
};
exports.commandCount = function() {
return Object.keys(commands).length;
};
bot.login(config.token);
bot.login(config.token);

View file

@ -1,12 +1,12 @@
'use strict';
const fs = require("fs"),
path = require("path");
const fs = require('fs'),
path = require('path');
function getPlugins(srcpath) {
return fs.readdirSync(srcpath);
}
let plugin_directory = path.join(__dirname, "modules");
let plugin_directory = path.join(__dirname, 'modules');
let plugins = getPlugins(plugin_directory);
exports.init = function init() {
@ -14,7 +14,7 @@ exports.init = function init() {
};
function load_plugins() {
const dbot = require("./bot.js");
const dbot = require('./bot.js');
let commandCount = 0;
let otherFunc = 0;
for (let i = 0; i < plugins.length; i++) {
@ -25,7 +25,7 @@ function load_plugins() {
console.log(`Improper setup of the '${plugins[i]}' plugin. : ${err}`);
}
if (plugin) {
if ("commands" in plugin) {
if ('commands' in plugin) {
for (let j = 0; j < plugin.commands.length; j++) {
if (plugin.commands[j] in plugin) {
dbot.addCommand(plugin.commands[j], plugin[plugin.commands[j]]);
@ -33,14 +33,14 @@ function load_plugins() {
}
}
}
if("custom" in plugin){
if ('custom' in plugin) {
for (let j = 0; j < plugin.custom.length; j++) {
if (plugin.custom[j] in plugin) {
dbot.addCustomFunc(plugin[plugin.custom[j]]);
otherFunc++;
}
}
}
}
}
}
console.log(`Loaded ${dbot.commandCount()} chat commands and ${otherFunc} custom functions.`);