Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

[BUGFIX/Feature] Fixed Event Loading && Added Internal Utils #143

Merged
merged 53 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d818548
Moved loading to Utils
Feb 15, 2017
0e5b0ac
Time for more utils
Feb 15, 2017
b2eb2a5
FML
Feb 15, 2017
55f9d2a
Faith fixed the regex
Feb 15, 2017
462a3bf
Typo out of 10
Feb 15, 2017
69d7a34
regexp
Feb 15, 2017
e0ce5ed
Fixed Configs
Feb 15, 2017
630a2b3
ESlint
Feb 15, 2017
9024a39
temp
Feb 15, 2017
52c42e3
^^^^
CyberiumShadow Feb 15, 2017
85babb9
typo
Feb 15, 2017
83eb4a4
Merge branch 'Utils' of https://github.com/dirigeants/komada into Utils
Feb 15, 2017
83e6601
We are not using initialize function again
UnseenFaith Feb 17, 2017
d1a7fd6
Client.ready
UnseenFaith Feb 17, 2017
015f1fa
Added Env Var support for clientDir
Feb 18, 2017
d4535f5
Removal of client.email from clean function
Feb 20, 2017
c261229
ESlint + Updating of ~Info Command
Feb 20, 2017
150df13
...
Feb 20, 2017
567634a
Fix a typo on line 70 (#144)
Feb 23, 2017
7066c67
reload all commands
Feb 26, 2017
ede071c
Merge branch 'Utils' of https://github.com/dirigeants/komada into Utils
Feb 26, 2017
e4e6c66
whoops
Feb 26, 2017
9baea8e
Silent inhibitors (#146)
kyranet Feb 27, 2017
8783d61
Added the new Discord.js methods. (#147)
kyranet Feb 28, 2017
f9aa319
Update .travis.yml
CyberiumShadow Mar 2, 2017
8eeb108
Sentry integration
CyberiumShadow Mar 3, 2017
091a615
Update app.js
CyberiumShadow Mar 3, 2017
bc37e96
grrr
CyberiumShadow Mar 3, 2017
12c40e4
Is this it?
CyberiumShadow Mar 3, 2017
bde993f
ESLint
CyberiumShadow Mar 3, 2017
7a0c8a8
Update package.json
CyberiumShadow Mar 3, 2017
67e563a
Update CHANGELOG.md
CyberiumShadow Mar 3, 2017
efbd017
Update package.json
CyberiumShadow Mar 3, 2017
cd3a6ff
Fixed reload events
kyranet Mar 17, 2017
e2c5386
Update CHANGELOG.md
kyranet Mar 17, 2017
c039bd9
Fixed `'file' is not defined`
kyranet Mar 17, 2017
eb20633
ESLint
kyranet Mar 17, 2017
ee3d557
Merge branch 'indev' into Utils
CyberiumShadow Mar 17, 2017
7e204e9
Update package.json
CyberiumShadow Mar 17, 2017
593305b
Update app.js
CyberiumShadow Mar 17, 2017
476effd
Update package.json
CyberiumShadow Mar 17, 2017
2b5d702
Update CHANGELOG.md
CyberiumShadow Mar 17, 2017
aa36480
ESLint
CyberiumShadow Mar 17, 2017
ed73add
Fixed codeblocks and missing information (no error stack)
kyranet Mar 17, 2017
ccb902f
Fixed missing codeblocks
kyranet Mar 17, 2017
1c4df83
Update CHANGELOG.md
kyranet Mar 17, 2017
80ef225
Add error.stack to case warn.
kyranet Mar 17, 2017
6406cc6
Disconnect event should print "Disconnected" now
kyranet Mar 17, 2017
55e0a64
Update CHANGELOG.md
kyranet Mar 17, 2017
53bad46
Update app.js
kyranet Mar 17, 2017
3c60bce
util.inspect error, warn, and better disconnect message.
kyranet Mar 17, 2017
6e88507
Update CHANGELOG.md
kyranet Mar 17, 2017
6a078a5
Update app.js
kyranet Mar 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.json → .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"no-shadow": "warn",
"no-plusplus": "off",
"radix": ["error", "as-needed"],
"max-len": "warn",
"import/no-extraneous-dependencies": "off",
"import/no-unresolved": "off",
"import/no-dynamic-require": "warn",
Expand Down
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
language: node_js
node_js:
- "6"
- "7"
install: npm install
notifications:
webhooks:
urls:
- https://webhooks.gitter.im/e/0926b82fc22ea0760ede
on_success: always # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: never # options: [always|never|change] default: always
27 changes: 26 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## [0.18.1] - 2017-03-17
### Added
- Sentry Integration for Error Tracking purposes direct to Komada Devs,
- Added the new utils from `Discord.js#master`: escapeMarkdown and splitMessage are now in `client.methods`.
- Added support for silent inhibitors (if `return true`, it won't send a reply).
- Added Environmental Variable support for clientDir.
- Added regExpEscape function.

### Changed
- Add error.stack to the function log.js to avoid [object Object].
- Disconnect event should now prints a more human readable error instead of `[object Object]`.
- error and warn event errors are now inspected with depth 0, for better debug.
- loading Functions are removed from Functions folder and moved to a Utils folder. (This folder will be there for future features as well.)

### Fixed
- Reloading pieces should now return the error stack in a codeblock.
- Fixed function reload event.
- Fixed command reload all. underlying bug since 0.15.x days.
- Fixed typo in validateData function
- Fixed Default Conf initialize. (No longer outputs undefined)
- Fixed invalid regex for prefixes in parseCommand

### Removed
- client.email redaction from the clean function.

## [0.18.0] - 2017-03-16
### Added
Expand Down Expand Up @@ -178,12 +202,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Various Confs fixes from [UnseenFaith]
- Usage Addition/ParseUsage fix from [UnseenFaith]

[Unreleased]: https://github.com/eslachance/komada/compare/0.18.0...indev
[Unreleased]: https://github.com/eslachance/komada/compare/0.18.1...indev
[0.10.0]: https://github.com/eslachance/komada/compare/1627e6deb1d8c352d83e52ccd590f2330f5f8bb2...0.10.0
[0.11.0]: https://github.com/eslachance/komada/compare/0.10.0...0.11.0
[0.12.0]: https://github.com/eslachance/komada/compare/0.11.0...0.12.0
[0.12.4]: https://github.com/eslachance/komada/compare/0.12.0...0.12.4
[0.18.0]: https://github.com/eslachance/komada/compare/0.12.4...0.18
[0.18.1]: https://github.com/eslachance/komada/compare/0.12.4...0.18.1

[vzwGrey]: https://github.com/vzwGrey
[eslachance]: https://github.com/eslachance
Expand Down
39 changes: 25 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const Discord = require("discord.js");
const path = require("path");
const util = require("util");

const loadFunctions = require("./utils/loadFunctions.js");
const loadEvents = require("./utils/loadEvents.js");
const loadProviders = require("./utils/loadProviders.js");
const loadCommands = require("./utils/loadCommands.js");
const loadCommandInhibitors = require("./utils/loadCommandInhibitors.js");
const loadMessageMonitors = require("./utils/loadMessageMonitors.js");

const loadFunctions = require("./functions/loadFunctions.js");
const Config = require("./classes/Config.js");

exports.start = async (config) => {
Expand All @@ -10,7 +17,7 @@ exports.start = async (config) => {

client.config = config;

// Extend client
// Extend client
client.funcs = {};
client.helpStructure = new Map();
client.commands = new Discord.Collection();
Expand All @@ -19,37 +26,42 @@ exports.start = async (config) => {
client.messageMonitors = new Discord.Collection();
client.providers = new Discord.Collection();

// Extend Client with Native Discord.js Functions for use in our pieces.
// Extend Client with Native Discord.js Functions for use in our pieces.
client.methods = {};
client.methods.Collection = Discord.Collection;
client.methods.Embed = Discord.RichEmbed;
client.methods.MessageCollector = Discord.MessageCollector;
client.methods.Webhook = Discord.WebhookClient;
client.methods.escapeMarkdown = Discord.escapeMarkdown;
client.methods.splitMessage = Discord.splitMessage;

client.coreBaseDir = `${__dirname}${path.sep}`;
client.clientBaseDir = `${process.cwd()}${path.sep}`;
client.clientBaseDir = `${process.env.clientDir || process.cwd()}${path.sep}`;
client.guildConfs = Config.guildConfs;
client.configuration = Config;

await loadEvents(client);

client.once("ready", async () => {
client.config.prefixMention = new RegExp(`^<@!?${client.user.id}>`);
client.configuration.initialize(client);
await client.configuration.initialize(client);
await loadFunctions(client);
await client.funcs.loadProviders(client);
await client.funcs.loadCommands(client);
await client.funcs.loadCommandInhibitors(client);
await client.funcs.loadMessageMonitors(client);
await client.funcs.loadEvents(client);
await loadProviders(client);
await loadCommands(client);
await loadCommandInhibitors(client);
await loadMessageMonitors(client);
client.i18n = client.funcs.loadLocalizations;
client.i18n.init(client);
client.destroy = () => "You cannot use this within Komada, use process.exit() instead.";
client.ready = true;
});

client.on("error", e => client.funcs.log(e, "error"));
client.on("warn", w => client.funcs.log(w, "warning"));
client.on("disconnect", e => client.funcs.log(e, "error"));
client.on("error", e => client.funcs.log(util.inspect(e, { depth: 0 }), "error"));
client.on("warn", w => client.funcs.log(util.inspect(w, { depth: 0 }), "warn"));
client.on("disconnect", e => client.funcs.log(`Disconnected | ${e.code}: ${e.reason}`, "error"));

client.on("message", async (msg) => {
if (!client.ready) return;
await client.funcs.runMessageMonitors(client, msg);
msg.author.permLevel = await client.funcs.permissionLevel(client, msg.author, msg.guild);
msg.guildConf = Config.get(msg.guild);
Expand All @@ -62,7 +74,6 @@ exports.start = async (config) => {
client.login(client.config.botToken);
return client;
};

process.on("unhandledRejection", (err) => {
if (!err) return;
console.error(`Uncaught Promise Error: \n${err.stack || err}`);
Expand Down
2 changes: 1 addition & 1 deletion classes/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class Config {
.then((conf) => {
if (conf) defaultConf = conf;
})
.catch(() => fs.outputJSONAsync(`${dataDir}${path.sep}${defaultFile}`));
.catch(() => fs.outputJSONAsync(`${dataDir}${path.sep}${defaultFile}`, defaultConf));
client.guilds.forEach((guild) => {
fs.readJSONAsync(path.resolve(`${dataDir}${path.sep}${guild.id}.json`))
.then((thisConf) => {
Expand Down
2 changes: 1 addition & 1 deletion commands/System/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports.run = (client, msg, [link, piece, folder = "Downloaded"]) => {
if (client.messageMonitors.has(name)) return msg.channel.sendMessage(`<@!${msg.author.id}> | That message monitor already exists in your bot. Aborting the load.`);
break;
case "providers":
if (client.providers.has(name)) return msg.channel.sendMessage(`<@!${msg.author.id} | That provider already exists in your bot. Aborting the load.`);
if (client.providers.has(name)) return msg.channel.sendMessage(`<@!${msg.author.id}> | That provider already exists in your bot. Aborting the load.`);
break;
default:
return "This will never trigger";
Expand Down
18 changes: 17 additions & 1 deletion commands/System/info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
exports.run = (client, msg) => {
msg.channel.sendMessage("This bot is built on the Komada framework, a plug-and-play bot builder made by Dirigeant's team of dedicated developers. For more information visit: <https://komada.js.org>");
const information = `
Komada is a 'plug-and-play' framework built on top of the Discord.js library.
Most of the code is modularized, which allows developers to edit Komada to suit their needs.

Some features of Komada include:
• Fast Loading times with ES7 Support (Async/Await)
• Per-server settings for each guild, that can be extended with your own code
• Customizable Command system with automated usage parsing and easy to use reloading and downloading modules
• "Monitors" which can watch messages and act on them, like a normal message event (Swear Filters, Spam Protection, etc)
• "Inhibitors" which can prevent commands from running based on a set of parameters (Permissions, Blacklists, etc)
• "Providers" which allow you to connect with an outside database of your choosing **soon**:tm:
• Internal "Functions" which allow you to use functions anywhere where you have access to a client variable.

We hope to be a 100% customizable framework that can cater to all audiences. We do frequent updates and bugfixes when available.
If you're interested in us, check us out at https://komada.js.org
`;
msg.channel.send(information);
};

exports.conf = {
Expand Down
2 changes: 1 addition & 1 deletion commands/System/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ exports.run = async (client, msg, [type, name]) => {
case "command":
switch (name) {
case "all":
await client.funcs.loadCommands(client);
await require(`${client.coreBaseDir}utils/loadCommands.js`)(client);
msg.channel.sendMessage("✅ Reloaded all commands.");
break;
default:
Expand Down
1 change: 0 additions & 1 deletion functions/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ function sensitivePattern(client) {
let pattern = "";
if (client.token) pattern += client.token;
if (client.token) pattern += (pattern.length > 0 ? "|" : "") + client.token;
if (client.email) pattern += (pattern.length > 0 ? "|" : "") + client.email;
if (client.user.email) pattern += (pattern.length > 0 ? "|" : "") + client.user.email;
if (client.password) pattern += (pattern.length > 0 ? "|" : "") + client.password;
this.sensitivePattern = new RegExp(pattern, "gi");
Expand Down
5 changes: 4 additions & 1 deletion functions/handleCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module.exports = async (client, msg, command, args = undefined) => {
const validCommand = this.getCommand(client, command);
if (!validCommand) return;
const response = this.runInhibitors(client, msg, validCommand);
if (response) return msg.reply(response);
if (response) {
if (typeof response === "string") return msg.reply(response);
return;
}
try {
const params = await client.funcs.usage.run(client, msg, validCommand, args);
validCommand.run(client, msg, params);
Expand Down
2 changes: 1 addition & 1 deletion functions/handleMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module.exports = (client, msg, edited = false) => {
if (!client.config.selfbot && msg.author.id === client.user.id) return false; // Ignore other users if selfbot but config option is false
if (!client.config.editableCommands && edited) return false; // Ignore message if owner doesn't allow editableCommands
return true;
}
};
5 changes: 3 additions & 2 deletions functions/loadSingleCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ module.exports = (client, command, reload = false, loadPath = null) => new Promi
cmd.init(client);
}
} catch (e) {
reject(`Could not load existing command data: ${e.stack}`);
reject(`Could not load existing command data: \`\`\`js\n${e.stack}\`\`\``);
}
} else {
try {
cmd = require(loadPath);
if (cmd.conf.selfbot && !client.config.selfbot) {
return reject(`The command \`${cmd.help.name}\` is only usable in selfbots!`);
}
delete require.cache[require.resolve(loadPath)];
if (cmd.init) cmd.init(client);
let pathParts = loadPath.split(path.sep);
pathParts = pathParts.slice(pathParts.indexOf("commands") + 1);
Expand All @@ -46,7 +47,7 @@ module.exports = (client, command, reload = false, loadPath = null) => new Promi
});
client.funcs.loadSingleCommand(client, command, false, loadPath);
} else {
reject(`Could not load new command data: ${e.stack}`);
reject(`Could not load new command data: \`\`\`js\n${e.stack}\`\`\``);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion functions/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = (data, type = "log") => {
console.warn(`${clk.black.bgYellow(`[${moment().format("YYYY-MM-DD HH:mm:ss")}]`)} ${data}`);
break;
case "error":
console.error(`${clk.bgRed(`[${moment().format("YYYY-MM-DD HH:mm:ss")}]`)} ${data}`);
console.error(`${clk.bgRed(`[${moment().format("YYYY-MM-DD HH:mm:ss")}]`)} ${data.stack || data}`);
break;
case "log":
console.log(`${clk.bgBlue(`[${moment().format("YYYY-MM-DD HH:mm:ss")}]`)} ${data}`);
Expand Down
11 changes: 6 additions & 5 deletions functions/parseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ exports.getPrefix = (client, msg) => {
if (client.config.prefixMention.test(msg.content)) {
return client.config.prefixMention;
}
const prefix = msg.guildConf.prefix;
let prefix = msg.guildConf.prefix;
const escape = client.funcs.regExpEsc;
if (prefix instanceof Array) {
prefix.forEach((prefix) => {
if (msg.content.startsWith(prefix)) prefix = RegExp(`^${prefix}`);
else prefix = false;
prefix.forEach((pref) => {
if (msg.content.startsWith(pref)) prefix = pref;
else pref = false;
});
return prefix;
}
if (msg.content.startsWith(prefix)) return new RegExp(`^${prefix}`); // eslint-disable-line
if (prefix && msg.content.startsWith(prefix)) return new RegExp(`^${escape(prefix)}`); // eslint-disable-line
return false;
};
1 change: 1 addition & 0 deletions functions/regExpEsc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = str => str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
Loading