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 #3 from TobiasFeld22/Updates
Browse files Browse the repository at this point in the history
added boot & messagefunctions
  • Loading branch information
TobiasFeld22 authored Sep 9, 2017
2 parents 6c6e951 + d038018 commit 87b7cb6
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 46 deletions.
169 changes: 157 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@ const Discord = require("discord.js");

const client = new Discord.Client();
const setup = require("./setup.js");
const fs = require("fs")
var util = require("./src/util.js")
var developer = false;
module.exports = {};
module.exports.version = require("./package.json").version;
module.exports.start = function(config) {
console.log("Loading commands")
setup(fs, config, require("path").dirname(require.main.filename)).then((commands) => {
client.commanddata = commands;
setup(config, require("path").dirname(require.main.filename)).then((data) => {
client.commanddata = data.commands;
client.functions = data.functions
client.functions.types = {
messages: [],
commands: []
}
client.data = {};
client.data.version = module.exports.version;
client.data.util = util

client.functions.messages.messagefuncs.forEach(i => {
i.type = i.type.map(i => (i.toLowerCase()))
if (i.type == "all" && i.type.length === 1) {
client.functions.types.commands.push(i.name);
client.functions.types.messages.push(i.name);
} else if (i.type == "messages") {
client.functions.types.messages.push(i.name);
} else if (i.type == "commands") {
client.functions.types.commands.push(i.name);
}

})
client.config = config;
start(client, client.config, client.commanddata)
}).catch((err) => {
Expand Down Expand Up @@ -45,13 +65,13 @@ function start(client, config, commanddata) {
return console.warn("This wrapper doesn't support selfbots.")
}
client.fetchApplication().then((application) => {
if (application.owner == null){
if (application.owner == null) {
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.")
}
}else{
} else {
client.config.owner_id = application.owner.id
}

Expand All @@ -65,13 +85,27 @@ function start(client, config, commanddata) {
}
})


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

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


setTimeout(function() {
if (data.time > 0) {
setInterval(returnfunction, data.time)
}
return returnfunction()

}, 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!")
util.checkUpdate(module.exports).then(update => {
console.log(update)
}).catch(err => {
console.warn(err)
})



Expand All @@ -84,16 +118,40 @@ function start(client, config, commanddata) {




// commands
if (!message.content.startsWith(config.prefix)) {
dofuncs(client, message, "message").catch((data) => {
if (data) {
if (developer) {
return console.warn(data)
}
}
})
return
}
var content = message.content.replace(config.prefix, "")
var command = content.split(" ")[0].toLowerCase();
if (commanddata.commands.has(command) === true) {
doCommand(command, client, message)
message.command = commanddata.commands.get(command)
dofuncs(client, message, "command").then(() => {
doCommand(command, client, message)
}).catch(data => {
if (data) {
if (developer) {
return console.warn(data)
}
}
})
} else if (commanddata.aliases.has(command) === true) {
doCommand(commanddata.aliases.get(command), client, message);
message.command = commanddata.commands.get(commanddata.aliases.get(command))
dofuncs(client, message, "command").then(() => {
doCommand(commanddata.aliases.get(command), client, message);
}).catch(data => {
if (data) {
console.log(data)
}
})
}
})
}
Expand All @@ -120,6 +178,93 @@ function doCommand(command, client, message) {
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) {
if (type == "message") {
var funcnumber = 0;
if (client.functions.types.messages.length == 0) {
return resolve()
}
client.functions.messages.messagefuncs.forEach(i => {
var num = client.functions.messages.messagefuncs.size
if (client.functions.types.messages.includes(i.name) == false) {
done(funcnumber, num)
}
var result = i.function(client, message, message.command)
if (result == undefined) {
return done(funcnumber, num)
}
if (typeof result.then == "function") {
result.then(data => {
if (data) {
if (typeof data == "string") {
message.channel.send(data)
}
return reject()
}
done(funcnumber, num)
}).catch(err => console.warn(i.name + " | Message function just stopped working correctly. | Error: \n", err))
} else {
if (result) {
if (typeof result == "string") {
message.channel.send(result)
}
return reject()
}
done(funcnumber, num)
}
})


}
if (type == "command") {
var number = 0;
if (client.functions.types.commands.length == 0) {
return resolve()
}
client.functions.messages.messagefuncs.forEach(i => {
var num = client.functions.messages.messagefuncs.size
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") {
result.then((data) => {
if (data) {
if (typeof data == "string") {
message.channel.send(data)
}
return reject()
}
done(number, num)
}).catch(err => console.warn(i.name + " | Message function just stopped working correctly. | Error: \n", err))
} else {
if (result) {
if (typeof result == "string") {
message.channel.send(result)
}
return reject()
}
done(number, num)
}
})


}


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

});
}
13 changes: 7 additions & 6 deletions commands/createcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ var command = module.exports = {}
var path = require("path")
var fs = require("fs")
command.name = "createcommand"

// Owner only
command.level = 10;
command.command = function(client, message) {
var args = message.content.split(" ");
if (!message.author.id == client.config.owner_id) {
return
}

if (args[1] == null) {
return message.channel.send("[EDB] Please specify the name for the command. If you want to use aliases, type them space-seperated behind the name.");

Expand All @@ -17,7 +16,7 @@ command.command = function(client, message) {
aliases: [],
name: args[1]
}
if (args[1].match(/[/\\<>:*|]/g)){
if (args[1].match(/[/\\<>:*|]/g)) {
return message.channel.send("[EDB] Filenames can't include one of these characters: ` / \\ < > : * | `, please try a different filename.")
}
if (args[2] != null) {
Expand All @@ -32,7 +31,9 @@ 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
6 changes: 4 additions & 2 deletions commands/reboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ var command = module.exports = {}
command.name = "reboot";
command.aliases = ["restart"];

// Owner only
command.level = 10

command.command = function(client, message) {
// needs to be replaced with command requirements when started working on!
if (client.config.owner_id !== message.author.id) {return}



message.channel.send("[EDB] Restarting bot...")
Expand Down
7 changes: 3 additions & 4 deletions commands/reload.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var command = module.exports = {}

command.name = "reload";
// owner only
command.level = 10;

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

// needs to be replaced with command requirements when started working on!
if (client.config.owner_id !== message.author.id) {
return
}

var args = message.content.split(" ")
if (args[1] == null) {
args[1] = ""
Expand Down
14 changes: 14 additions & 0 deletions functions/boot/checkUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint no-console: 0 */
exports.name = "checkUpdate";
// Check update every every hour
exports.time = 3600000;
// Check on boot
exports.delay = 0;

exports.function = function(client) {
client.data.util.checkUpdate(client.data.version).then(update => {
console.log(update)
}).catch(err => {
console.warn(err)
})
}
13 changes: 13 additions & 0 deletions functions/messages/permissioncheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.name = "permissioncheck"
exports.type = ["commands"];
exports.function = function(client, message, command) {
switch (command.level) {
case 10:
if (message.author.id === client.config.owner_id) {
return false;
}
return true;
default:
return false;
}
}
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.0",
"version": "0.0.2-beta.3",
"description": "An easy to use discord bot, which uses small modules for getting commands and messages. ",
"main": "app.js",
"dependencies": {
Expand Down
Loading

0 comments on commit 87b7cb6

Please sign in to comment.