Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #4 from TobiasFeld22/Updates
Browse files Browse the repository at this point in the history
Update v0.0.2
  • Loading branch information
TobiasFeld22 authored Sep 16, 2017
2 parents 87b7cb6 + fd2cee0 commit 13284bc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 62 deletions.
64 changes: 12 additions & 52 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint no-console: 0 */
const Discord = require("discord.js");

const client = new Discord.Client();
const setup = require("./setup.js");
var util = require("./src/util.js")
var developer = false;
client.developer = false;
module.exports = {};
module.exports.version = require("./package.json").version;
module.exports.start = function(config) {
Expand All @@ -30,26 +29,16 @@ module.exports.start = function(config) {
} else if (i.type == "commands") {
client.functions.types.commands.push(i.name);
}

})
client.config = config;
start(client, client.config, client.commanddata)
}).catch((err) => {
if (developer === false) {
if (client.developer === false) {
return console.error("An error occurred while loading commands.", err)
}
console.log(err, err.stack)

})




}




function start(client, config, commanddata) {
var startedAt = new Date();
client.login(config.token)
Expand All @@ -74,25 +63,20 @@ function start(client, config, commanddata) {
} else {
client.config.owner_id = application.owner.id
}

}).catch((err) => {
if (developer) {
if (client.developer) {
console.warn("Can't fetch application, error: \n", err)
} else if (client.config.owner_id == null) {
return console.error("Can't fetch the owner's id, please follow instructions here <page_link>.")
} else if (client.users.get(client.config.owner_id) == undefined) {
return console.error("The bot can't find the owner_id set up inside your file, \nThis could be because it's not valid, or because you are not in a server with it, please invite it to a server where you are on.")
}
})


client.functions.boot.bootfuncs.forEach((data) => {

function returnfunction() {
return data.function(client)
}


setTimeout(function() {
if (data.time > 0) {
setInterval(returnfunction, data.time)
Expand All @@ -101,29 +85,15 @@ function start(client, config, commanddata) {

}, data.delay);
})



console.log(commanddata.commands.size + " commands | " + commanddata.aliases.size + " aliases, bot online")
console.log("To add new commands, type \"" + config.prefix + "createcommand <name> <alias1> <alias2> <alias3>\" to generate a new template!")




})



client.on("message", (message) => {




// commands
if (!message.content.startsWith(config.prefix)) {
dofuncs(client, message, "message").catch((data) => {
if (data) {
if (developer) {
if (client.developer) {
return console.warn(data)
}
}
Expand All @@ -138,7 +108,7 @@ function start(client, config, commanddata) {
doCommand(command, client, message)
}).catch(data => {
if (data) {
if (developer) {
if (client.developer) {
return console.warn(data)
}
}
Expand All @@ -155,8 +125,6 @@ function start(client, config, commanddata) {
}
})
}


function doCommand(command, client, message) {
command = client.commanddata.commands.get(command);
if (command === undefined) {
Expand All @@ -165,25 +133,24 @@ function doCommand(command, client, message) {
try {
command.command(client, message);
} catch (err) {
if (developer) {
if (client.developer) {
console.warn("Command: " + command.name + " | had an error while executing.", err)
} else if (err.code == "MODULE_NOT_FOUND") {
var mod = err.stack.split("\n")[0].replace("Error: Cannot find module ", "")
console.warn("Command: " + command.name + " | Requires the " + mod + " package to be installed.\nTo install this package, close the script and type: 'npm install " + mod.slice(1, -1) + "'")
if (message.author.id == client.config.owner_id) {
message.channel.send("[EDB] Command: **" + command.name + "** | Requires the " + mod + " package to be installed.\nTo install this package, close the script and type: `npm install " + mod.slice(1, -1) + "`")
}

} else {
console.warn("Command: " + command.name + " | had an error. Show the developer of the command module that you are getting this error code: \n" + err)
}
}
}

function dofuncs(client, message, type) {
return new Promise(function(resolve, reject) {
var funcnumber = 0;
var number = 0;
if (type == "message") {
var funcnumber = 0;
if (client.functions.types.messages.length == 0) {
return resolve()
}
Expand All @@ -196,7 +163,7 @@ function dofuncs(client, message, type) {
if (result == undefined) {
return done(funcnumber, num)
}
if (typeof result.then == "function") {
if (result instanceof Promise) {
result.then(data => {
if (data) {
if (typeof data == "string") {
Expand All @@ -220,7 +187,6 @@ function dofuncs(client, message, type) {

}
if (type == "command") {
var number = 0;
if (client.functions.types.commands.length == 0) {
return resolve()
}
Expand All @@ -229,12 +195,11 @@ function dofuncs(client, message, type) {
if (client.functions.types.commands.includes(i.name) == false) {
done(number, num)
}

var result = i.function(client, message, message.command)
if (result == undefined) {
return done(number, num)
}
if (typeof result.then == "function") {
if (result instanceof Promise) {
result.then((data) => {
if (data) {
if (typeof data == "string") {
Expand All @@ -254,17 +219,12 @@ function dofuncs(client, message, type) {
done(number, num)
}
})


}


function done(number, num) {
number = number + 1
function done(xnumber, num) {
number = xnumber + 1
if (number == num) {
return resolve()
}
}

});
}
4 changes: 1 addition & 3 deletions commands/createcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ command.command = function(client, message) {
return message.channel.send("[EDB] This file does already exist. Please try a different name. ")
}

fs.writeFile(path.resolve(path.dirname(require.main.filename), "commands/" + data.name + ".js"), "exports.name = \"" + data.name + "\" \nexports.aliases = " + JSON.stringify(data.aliases) + "\nexports.command = function(client, message){\n\n//Write your command functions here.\n\n} ", {
options: "utf8"
}, (err) => {
fs.writeFile(path.resolve(path.dirname(require.main.filename), "commands/" + data.name + ".js"), "exports.name = \"" + data.name + "\" \nexports.aliases = " + JSON.stringify(data.aliases) + "\nexports.command = function(client, message){\n\n//Write your command functions here.\n\n} ", {options: "utf8"}, (err) => {
if (err) {
return message.channel.send("[EDB] Failed to create this file, try to create it manually using this template: ```javascript\nexports.name = \"" + data.name + "\" \nexports.aliases = \"" + JSON.stringify(data.aliases) + "\"\nexports.command = function(client, message){\n\n//Write your command functions here.\n\n} \n```")
}
Expand Down
33 changes: 28 additions & 5 deletions commands/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ command.name = "reload";
command.level = 10;

const setup = require("../setup.js");
const fs = require("fs")
command.command = function(client, message) {


Expand All @@ -17,17 +16,20 @@ command.command = function(client, message) {
case "commands":
reloadcommands(client, client.config, message)
break;
case "functions":
reloadfunctions(client, message)
break;
default:
message.channel.send("You forgot to specify what to reload, choose from: `commands`")
message.channel.send("You forgot to specify what to reload, choose from: `commands` `functions`")
}
}

function reloadcommands(client, config, message) {

message.channel.send("[EDB] reloading commands")
.then(m => {
setup(fs, config, require("path").dirname(require.main.filename), true).then((commands) => {
client.commanddata = commands;
setup(config, require("path").dirname(require.main.filename), true).then((commands) => {
client.commanddata = commands.commands;
client.config = config;
if (commands.issues > 1) {
return m.edit("[EDB] Reloaded **" + client.commanddata.commands.size + "** commands succesfully.\n**" + commands.issues + "** commands failed to load. See the console for more info.")
Expand All @@ -37,7 +39,28 @@ function reloadcommands(client, config, message) {
m.edit("[EDB] Reloaded **" + client.commanddata.commands.size + "** commands succesfully.")

}).catch((err) => {
err.stack.toLowerCase()
m.edit(err.stack.toLowerCase())
})
})

}

function reloadfunctions(client, message) {

message.channel.send("[EDB] reloading functions")
.then(m => {
var temp = client.config;
setup(temp, require("path").dirname(require.main.filename), true).then((data) => {
client.functions = data.functions
client.config = temp;
if ((data.functions.messages.issues + data.functions.boot.issues) > 1) {
return m.edit("[EDB] Reloaded **" + client.functions.messages.messagefuncs.size + "** messagefunctions and **" + client.functions.boot.bootfuncs.size + "** bootfunctions succesfully.\n**" + (data.functions.messages.issues + data.functions.boot.issues) + "** issues while loading. See the console for more info.")
} else if ((data.functions.messages.issues + data.functions.boot.issues) === 1) {
return m.edit("[EDB] Reloaded **" + client.functions.messages.messagefuncs.size + "** messagefunctions and **" + client.functions.boot.bootfuncs.size + "** bootfunctions succesfully.\n**" + (data.functions.messages.issues + data.functions.boot.issues) + "** issue while loading. See the console for more info.")
}
m.edit("[EDB] Reloaded **" + client.functions.messages.messagefuncs.size + "** messagefunctions and **" + client.functions.boot.bootfuncs.size + "** bootfunctions succesfully.")
}).catch((err) => {
m.edit(err.stack.toLowerCase())
})
})

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-discord-bot",
"version": "0.0.2-beta.3",
"version": "0.0.2",
"description": "An easy to use discord bot, which uses small modules for getting commands and messages. ",
"main": "app.js",
"dependencies": {
Expand Down

0 comments on commit 13284bc

Please sign in to comment.