-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
skarab42
committed
Nov 30, 2020
1 parent
ad3a4db
commit d25d463
Showing
3 changed files
with
28 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const Command = require("../../../db/Models/Command"); | ||
|
||
module.exports = async function getCommandByName(name) { | ||
return await Command.findOne({ where: { name } }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
const getCommandNames = require("../api/getCommandNames"); | ||
const getCommandByName = require("../api/getCommandByName"); | ||
const pushActions = require("../pushActions"); | ||
const { _ } = require("../../i18next"); | ||
const ms = require("ms"); | ||
|
||
module.exports = async function onCommand({ command, nick, message }) { | ||
const commands = await getCommandNames(); | ||
const cooldowns = {}; | ||
|
||
if (!commands.includes(command.name)) { | ||
module.exports = async function onCommand({ channel, command, nick, message }) { | ||
const commandEntry = await getCommandByName(command.name); | ||
const now = Date.now(); | ||
|
||
if (!commandEntry) { | ||
throw new Error(`${_("twitch.command-not-found")} ${command.name}`); | ||
} | ||
|
||
const lastCall = cooldowns[command.name] || 0; | ||
const cooldown = commandEntry.cooldown * 1000; | ||
const elapsedTime = now - lastCall; | ||
|
||
if (elapsedTime < cooldown) { | ||
const rest = cooldown - elapsedTime; | ||
this.say(channel, `Cooldown for ${command.name} -> ~${ms(rest)}`); | ||
return; | ||
} | ||
|
||
cooldowns[command.name] = now; | ||
pushActions("onCommand", { user: nick, message, command }); | ||
}; |