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

View file

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