Permalink
Browse files

Command.sendMessage resolves with message

  • Loading branch information...
1 parent 9adc676 commit 1b8c4b0dc9669314b4f6ddd2503ae34cdc269fea @brussell98 committed Dec 19, 2016
Showing with 66 additions and 14 deletions.
  1. +3 −1 examples/test/commands/general/Ping.js
  2. +12 −12 lib/Base/AbstractCommand.js
  3. +50 −0 lib/Base/AbstractEventPlugin.js
  4. +1 −1 package.json
@@ -12,7 +12,9 @@ class PingCommand extends AbstractCommand {
handle(message) {
if (this.userOnCooldown(message.author.id, 3000))
return this.sendMessage(message, 'You can only use this command every 3 seconds.', { deleteAfter: 3000, deleteTrigger: true });
- this.sendMessage(message, 'pong!');
+ this.sendMessage(message, 'pong in ??ms').then(msg => {
+ msg.edit(`pong in ${(msg.timestamp - message.timestamp).toLocaleString()}ms`);
+ });
}
}
@@ -36,21 +36,17 @@ class AbstractCommand {
* @returns {Promise} Resolves with `this`
*/
load(parent) {
- return new Promise(resolve => {
- this.parent = parent;
- resolve(this);
- });
+ this.parent = parent;
+ return Promise.resolve(this);
}
/**
* Destroys/unloads the command
* @returns {Promise} Resolves with no value
*/
destroy() {
- return new Promise(resolve => {
- this.parent = undefined;
- resolve();
- });
+ this.parent = undefined;
+ return Promise.resolve();
}
/**
@@ -63,19 +59,20 @@ class AbstractCommand {
* @param {Boolean} [options.DM=false] Send in a Direct Message
* @param {Object} [options.file] The file to be sent with the message. Format as shown in the Eris docs
* @param {Boolean} [options.paginate] Split the message at 2000 charcter intervals. Used to send log messages that would normally fail
+ * @returns {Promise<Message>} The sent message, or nothing if pagination is true
* @see {@link http://eris.tachibana.erendale.abal.moe/Eris/docs/Client#function-createMessage|Eris.Client#createMessage}
*/
sendMessage(message, content, options = {}) {
if (!message || !content)
- return;
+ return Promise.resolve();
if (options.deleteTrigger && message.channel.guild && message.channel.permissionsOf(this.parent.bot.user.id).has('manageMessages'))
message.delete().catch(error => { this.parent.logger.warn('Error deleting trigger:', error); });
if (typeof content !== 'object')
content = { content };
- (options.DM ? message.author.getDMChannel() : Promise.resolve(message.channel)).then(channel => {
+ return (options.DM ? message.author.getDMChannel() : Promise.resolve(message.channel)).then(channel => {
if (!options.paginate)
return this._sendMessage(channel, content, options);
let i = 0;
@@ -86,6 +83,7 @@ class AbstractCommand {
i += 2000;
options.file = undefined;
}
+ return Promise.resolve();
}).catch(error => { this.parent.logger.warn('Error getting DM channel:', error); });
}
@@ -94,14 +92,16 @@ class AbstractCommand {
* @param {Channel} channel The channel to create the message in
* @param {String|Object} content The content to be passed to Eris.Client#createMessage
* @param {Object} [options] Options for sending the message
+ * @returns {Promise<Message>} The sent message
*/
_sendMessage(channel, content, options) {
- channel.createMessage(content, options.file).then(msg => {
+ return channel.createMessage(content, options.file).then(message => {
if (options.deleteAfter > 0) {
setTimeout(() => {
- msg.delete().catch(error => { this.parent.logger.warn('Error deleting message (deleteAfter): ', error); });
+ message.delete().catch(error => { this.parent.logger.warn('Error deleting message (deleteAfter): ', error); });
}, options.deleteAfter);
}
+ return message;
}).catch(error => { this.parent.logger.warn('Error creating message:', error); });
}
@@ -0,0 +1,50 @@
+/**
+ * Handles events emitted from eris
+ * @abstract
+ */
+class AbstractEventPlugin {
+ /**
+ * Creates a new AbstractEventPlugin
+ * @abstract
+ */
+ constructor() {
+ if (this.constructor === AbstractEventPlugin)
+ throw new Error("Can't instantiate an abstract class!");
+ }
+
+ /**
+ * The name of the plugin
+ * @type {String}
+ * @abstract
+ */
+ get name() {
+ throw new Error('name must be overwritten');
+ }
+
+ /**
+ * Loads the plugin
+ * @param {Bot} bot The main client
+ * @returns {Promise} Resolves with `this`
+ */
+ load(bot) {
+ return new Promise(resolve => {
+ this.logger = bot.logger;
+ this.bot = bot;
+ resolve(this);
+ });
+ }
+
+ /**
+ * Destroys/unloads the plugin
+ * @returns {Promise} Resolves with no value
+ */
+ destroy() {
+ return new Promise(resolve => {
+ this.logger = undefined;
+ this.bot = undefined;
+ resolve();
+ });
+ }
+}
+
+module.exports = AbstractEventPlugin;
View
@@ -1,6 +1,6 @@
{
"name": "mirai-bot-core",
- "version": "4.0.0-beta",
+ "version": "4.0.0-beta2",
"description": "A Discord bot core allowing easy plugging in of modules",
"author": "brussell98",
"main": "index.js",

0 comments on commit 1b8c4b0

Please sign in to comment.