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

Fix for commands that return a virtual "never". #76

Merged
merged 2 commits into from
Oct 30, 2017
Merged
Changes from all commits
Commits
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
43 changes: 23 additions & 20 deletions src/monitors/commandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = class extends Monitor {
if (!validCommand) return;
const timer = new Stopwatch();
if (this.client.config.typing) msg.channel.startTyping();

this.client.inhibitors.run(msg, validCommand)
.then(() => this.runCommand(this.makeProxy(msg, new CommandMessage(msg, validCommand, prefix, prefixLength)), timer))
.catch((response) => {
Expand Down Expand Up @@ -53,27 +54,29 @@ module.exports = class extends Monitor {
});
}

runCommand(msg, timer) {
msg.validateArgs()
.then(async (params) => {
await msg.cmd.run(msg, params)
.then(mes => {
this.client.emit('commandRun', msg, msg.cmd, params, mes);
return this.client.finalizers.run(msg, mes, timer);
})
.catch(error => this.client.emit('commandError', msg, msg.cmd, msg.params, error));
if (this.client.config.typing) msg.channel.stopTyping();
async runCommand(msg, timer) {
try {
await msg.validateArgs();
} catch (error) {
if (this.client.config.typing) msg.channel.stopTyping();
if (error.code === 1 && this.client.config.cmdPrompt) {
return this.awaitMessage(msg, timer, error.message)
.catch(err => this.client.emit('commandError', msg, msg.cmd, msg.params, err));
}
return this.client.emit('commandError', msg, msg.cmd, msg.params, error);
}

const commandRun = msg.cmd.run(msg, msg.params);

if (this.client.config.typing) msg.channel.stopTyping();
timer.stop();

return commandRun
.then(mes => {
this.client.finalizers.run(msg, mes, timer);
this.client.emit('commandRun', msg, msg.cmd, msg.params, mes);
})
.catch((error) => {
if (this.client.config.typing) msg.channel.stopTyping();
if (error.code === 1 && this.client.config.cmdPrompt) {
return this.awaitMessage(msg, timer, error.message)
.catch(err => {
if (err) this.client.emit('commandError', msg, msg.cmd, msg.params, err);
});
}
return this.client.emit('commandError', msg, msg.cmd, msg.params, error);
});
.catch(error => this.client.emit('commandError', msg, msg.cmd, msg.params, error));
}

async awaitMessage(msg, timer, error) {
Expand Down