Skip to content

Commit

Permalink
Command.sendMessage resolves with message
Browse files Browse the repository at this point in the history
  • Loading branch information
brussell98 committed Dec 19, 2016
1 parent 9adc676 commit 1b8c4b0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
4 changes: 3 additions & 1 deletion examples/test/commands/general/Ping.js
Expand Up @@ -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`);
});
}
}

Expand Down
24 changes: 12 additions & 12 deletions lib/Base/AbstractCommand.js
Expand Up @@ -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();
}

/**
Expand All @@ -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;
Expand All @@ -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); });
}

Expand All @@ -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); });
}

Expand Down
50 changes: 50 additions & 0 deletions lib/Base/AbstractEventPlugin.js
@@ -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;
2 changes: 1 addition & 1 deletion package.json
@@ -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",
Expand Down

0 comments on commit 1b8c4b0

Please sign in to comment.