Permalink
Browse files

blacklist, await, and tons of fixes after the first test

  • Loading branch information...
1 parent b1c7909 commit ad873c09bf8d304d3badfcd65490f86014246d5d @brussell98 committed Nov 7, 2016
View
@@ -1,13 +1,15 @@
engines:
eslint:
enabled: true
- channel: "eslint-2"
+ channel: "eslint-3"
config: .eslintrc
duplication:
enabled: false
config:
languages:
- javascript
+ fixme:
+ enabled: true
ratings:
paths:
- "**.js"
View
@@ -2,3 +2,4 @@
*.env
.vscode
node_modules
+config.json
View
@@ -12,6 +12,13 @@
"plugins/markdown"
],
"templates": {
+ "default": {
+ "outputSourceFiles": false,
+ "includeDate": false
+ },
+ "docdash": {
+ "sort": true
+ },
"cleverLinks": false,
"monospaceLinks": true
},
@@ -20,6 +27,6 @@
"encoding": "utf8",
"private": true,
"recurse": true,
- "template": "./node_modules/minami"
+ "template": "./node_modules/docdash"
}
}
View
@@ -1,7 +1,9 @@
![Mirai Bot](http://i.imgur.com/BO18mRW.png)
[![David](https://img.shields.io/david/brussell98/Mirai.svg?maxAge=2592000)](https://david-dm.org/brussell98/Mirai) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/brussell98/Mirai/master/LICENSE) [![Code Climate](https://codeclimate.com/github/brussell98/Mirai/badges/gpa.svg)](https://codeclimate.com/github/brussell98/Mirai)
-An easy to use bot framework using the [Eris](https://github.com/abalabahaha/eris/) library. NodeJS version 6+ is ***REQUIRED***
+A powerful Discord bot base using [Eris](https://github.com/abalabahaha/eris/). At least NodeJS v6 *Argon* is **REQUIRED**
+
+> By using this you agree to not ask any questions unless you have a basic understanding of installing programs, running node applications, and developing JavaScript applications.
#### [Website](http://miraibot.ml) | [Support my projects on Patreon](http://patreon.com/brussell98) | [Documentation](http://brussell98.tk/Mirai/index.html) | [Discord Server](https://discord.gg/rkWPSdu) | [Eris Docs](https://abal.moe/Eris/docs/index.html) | [Todo List](https://trello.com/b/Uw5wZLzJ)
View
@@ -0,0 +1,26 @@
+var beautify_html = require('js-beautify').html,
+ fs = require('fs');
+
+fs.readdir(__dirname + '/docs/', (error, files) => {
+ if (error)
+ throw error;
+ files.filter(f => f.endsWith('.html')).map((filename, i) => {
+ fs.readFile(__dirname + '/docs/' + filename, 'utf8', (err, data) => {
+ if (err)
+ throw err;
+ fs.writeFile(__dirname + '/docs/' + filename, beautify_html(data, {
+ brace_style: 'collapse',
+ indent_with_tabs: true,
+ preserve_newlines: false,
+ end_with_newline: true
+ }), e => {
+ if (e)
+ throw e;
+ if (i === files.length - 1) {
+ console.log('HTML files beautified');
+ process.exit(0);
+ }
+ });
+ });
+ });
+});
@@ -0,0 +1,46 @@
+var fs = require('fs'),
+ AbstractCommandPlugin = require('../../../lib/base/AbstractCommandPlugin');
+
+class GeneralCommands extends AbstractCommandPlugin {
+ constructor() {
+ super();
+ this.commands = {}
+ }
+
+ get name() {
+ return 'general';
+ }
+
+ load(bot) {
+ return new Promise((resolve, reject) => {
+ super.load(bot);
+ fs.readdir(__dirname + '/general/', (error, files) => {
+ if (error)
+ return reject(error);
+ for (let file of files) {
+ try {
+ let command = new (require(__dirname + '/general/' + file))();
+ command.load(this).then(command => {
+ this.commands[command.name] = command;
+ });
+ } catch (e) {
+ this.bot.logger.error(`Error loading ${file}: ${e.stack}`);
+ }
+ }
+ resolve();
+ });
+ });
+ }
+
+ handle(message) {
+ if (message[0] === '!') {
+ let command = message.content.substr(1);
+ for (let name in this.commands) {
+ if (command.startsWith(name))
+ return this.commands[name].handle(message, command.replace(name, '').trim());
+ }
+ }
+ }
+}
+
+module.exports = GeneralCommands;
@@ -0,0 +1,20 @@
+var AbstractCommand = require('../../../../lib/base/AbstractCommand');
+
+class AwaitCommand extends AbstractCommand {
+ constructor() {
+ super();
+ }
+
+ get name() {
+ return 'await';
+ }
+
+ handle(message, args) {
+ this.sendMessage(message, 'Say the args in caps in this channel, time limit: 5 seconds');
+ this.awaitMessage(msg => msg.channel.id === message.channel.id && msg.content === args.toUpperCase(), msg => {
+ this.sendMessage(msg, 'Recieved');
+ }, 5000);
+ }
+}
+
+module.exports = AwaitCommand;
@@ -0,0 +1,17 @@
+var AbstractCommand = require('../../../../lib/base/AbstractCommand');
+
+class PingCommand extends AbstractCommand {
+ constructor() {
+ super();
+ }
+
+ get name() {
+ return 'ping';
+ }
+
+ handle(message) {
+ this.sendMessage(message, 'pong!');
+ }
+}
+
+module.exports = PingCommand;
@@ -0,0 +1,9 @@
+const Mirai = require('../../');
+var config = require('./config.json');
+
+var GeneralCommands = new (require('./commands/GeneralCommands'));
+
+var mirai = new Mirai(config);
+mirai.loadCommandPlugin(GeneralCommands)
+ .then(() => mirai.run())
+ .catch(console.error);
View
@@ -1 +1 @@
-module.exorts = require('./lib/Bot.js');
+module.exports = require('./lib/Bot.js');
@@ -4,6 +4,10 @@ class AbstractCommand {
throw new Error("Can't instantiate an abstract class!");
}
+ get name() {
+ throw new Error('name must be overwritten');
+ }
+
load(parent) {
this.client = parent.bot.client;
this.parent = parent;
@@ -16,7 +20,7 @@ class AbstractCommand {
return Promise.resolve();
}
- send(message, content, options = {}) {
+ sendMessage(message, content, options = {}) {
if (!message || !content)
return;
@@ -36,6 +40,10 @@ class AbstractCommand {
}).catch(error => this.parent.logger.warn(error)); // TODO
});
}
+
+ awaitMessage(trigger, action, timeout) {
+ this.parent.bot.chatHandler.awaitMessage(trigger, action, timeout);
+ }
}
module.exports = AbstractCommand;
@@ -9,23 +9,21 @@ class AbstractCommandPlugin {
}
get name() {
- throw new Error('Name must be overwritten');
+ throw new Error('name must be overwritten');
}
load(bot) {
this.bot = bot;
- this.logger = bot.logger;
return Promise.resolve(this);
}
destroy() {
- this.logger = undefined;
this.bot = undefined;
return Promise.resolve();
}
handle() {
- throw new Error('Handle must be overwritten');
+ throw new Error('handle must be overwritten');
}
}
View
@@ -15,6 +15,14 @@ try {
Promise = global.Promise;
}
+/**
+ * Manages the connection to Discord and interaction between plugins
+ * @extends EventEmitter
+ * @example <caption>Create a new instance of Mirai</caption>
+ * const Mirai = require('mirai-bot-discord');
+ * var config = require('./config.json');
+ * var mirai = new Mirai(config);
+ */
class Bot extends EventEmitter {
constructor(config) {
super();
@@ -25,55 +33,72 @@ class Bot extends EventEmitter {
this.logger = new Logger();
this.client = new Eris(config.token, config.erisOptions);
- this.chatHandler = new ChatHandler(this.client, config.chatHandler);
+ this.chatHandler = new ChatHandler(this, config.chatHandler);
+ this.blacklistedGuilds = config.blacklistedGuilds || [];
+ this.blacklistedUsers = config.blacklistedUsers || [];
this.commandPlugins = [];
this.eventPlugins = [];
this.middleware = [];
this.client.on('ready', () => {
this.chatHandler.run();
- this.logger.info(); // TODO
+ this.logger.info('ready'); // TODO
this.emit('ready');
});
this.client.on('error', (error, shard) => {
- this.logger.error(); // TODO
+ this.logger.error('error', error); // TODO
this.emit('error', error, shard)
});
this.client.on('disconnected', () => {
- this.logger.error(); // TODO
+ this.logger.error('disconnected'); // TODO
this.emit('disconnected');
});
this.client.on('shardReady', shard => {
- this.logger.info(); // TODO
+ this.logger.info('shard', shard, 'ready'); // TODO
this.emit('shardReady', shard);
});
this.client.on('shardDisconnect', (error, shard) => {
- this.logger.warn(); // TODO
+ this.logger.warn('shard', shard, 'disconnected'); // TODO
this.emit('shardDisconnect', error, shard);
});
this.client.on('shardResume', shard => {
- this.logger.info(); // TODO
+ this.logger.info('shard', shard, 'resumed'); // TODO
this.emit('shardResume', shard);
});
+
+ if (this.blacklistedGuilds.length !== 0) {
+ this.client.on('guildCreate', guild => {
+ if (this.blacklistedGuilds.includes(guild.id)) {
+ guild.leave();
+ this.logger.info('left', guild.name); // TODO
+ }
+ });
+ }
}
+ /** Runs the bot, connecting to Discord */
run() {
this.client.connect().catch(error => {
this.emit('error', error);
});
}
+ /**
+ * @param {Function} plugin The plugin to load. Must have a load() method which is passed <code>this</code>.
+ * @example <caption>Loading a Command Plugin</caption>
+ * var funCommands = new FunCommandsPlugin();
+ * mirai.loadCommandPlugin(funCommands);
+ * @returns {Promise}
+ */
loadCommandPlugin(plugin) {
- if (typeof plugin === 'function')
- return plugin.load(this).then(() => {
- this.commandPlugins.push(plugin);
- });
- return Promise.reject('You must pass a class to loadCommandPlugin');
+ return plugin.load(this).then(() => {
+ this.commandPlugins.push(plugin);
+ });
}
reloadCommandPlugin(plugin) {
@@ -96,12 +121,14 @@ class Bot extends EventEmitter {
});
}
+ /**
+ * @param {Function} plugin The plugin to load. Must have a load() method which is passed <code>this</code>.
+ * @returns {Promise}
+ */
loadEventPlugin(plugin) {
- if (typeof plugin === 'function')
- return plugin.load(this).then(() => {
- this.eventPlugins.push(plugin);
- });
- return Promise.reject('You must pass a class to loadEventPlugin');
+ return plugin.load(this).then(() => {
+ this.eventPlugins.push(plugin);
+ });
}
reloadEventPlugin(plugin) {
@@ -125,11 +152,9 @@ class Bot extends EventEmitter {
}
loadMiddleware(middleware) {
- if (typeof middleware === 'function')
- return middleware.load(this).then(() => {
- this.middleware.push(middleware);
- });
- return Promise.reject('You must pass a class to loadMiddleware');
+ return middleware.load(this).then(() => {
+ this.middleware.push(middleware);
+ });
}
reloadMiddleware(middleware) {
Oops, something went wrong.

0 comments on commit ad873c0

Please sign in to comment.