how to make message when command is in cooldown?

hey, so my default defaultCooldownDuration inside my Client is 4000ms(4 seconds), but when i run the command multiple times it won't respond because it's in cooldown, i just want to send a single message saying the bot is in cooldown, how an i do that?
Solution:
Implement messageCommandDenied / chatInputCommandDenied / contextMenuCommandDenied https://www.sapphirejs.dev/docs/Guide/preconditions/creating-your-own-preconditions
Sapphire Framework
Creating your own preconditions | Sapphire
Just as we did in Creating Commands, we will start by creating a preconditions subdirectory in
Jump to solution
14 Replies
Solution
Favna
Favna•12mo ago
Implement messageCommandDenied / chatInputCommandDenied / contextMenuCommandDenied https://www.sapphirejs.dev/docs/Guide/preconditions/creating-your-own-preconditions
Sapphire Framework
Creating your own preconditions | Sapphire
Just as we did in Creating Commands, we will start by creating a preconditions subdirectory in
Psyonix_
Psyonix_OP•12mo ago
all are already implemented events or preconditions?
Favna
Favna•12mo ago
then your preconditions are already handled. I'm not sure what you want to know then. Those are listeners
Psyonix_
Psyonix_OP•12mo ago
yes messageCommandDenied
import type { MessageCommandDeniedPayload, Events } from '@sapphire/framework';
import { Listener, UserError } from '@sapphire/framework';

export class UserEvent extends Listener<typeof Events.MessageCommandDenied> {
public async run(
{ context, message: content }: UserError,
{ message }: MessageCommandDeniedPayload
) {
// `context: { silent: true }` should make UserError silent:
// Use cases for this are for example permissions error when running the `eval` command.
if (Reflect.get(Object(context), 'silent')) return;

return message.channel.send({
content,
allowedMentions: { users: [message.author.id], roles: [] },
});
}
}
import type { MessageCommandDeniedPayload, Events } from '@sapphire/framework';
import { Listener, UserError } from '@sapphire/framework';

export class UserEvent extends Listener<typeof Events.MessageCommandDenied> {
public async run(
{ context, message: content }: UserError,
{ message }: MessageCommandDeniedPayload
) {
// `context: { silent: true }` should make UserError silent:
// Use cases for this are for example permissions error when running the `eval` command.
if (Reflect.get(Object(context), 'silent')) return;

return message.channel.send({
content,
allowedMentions: { users: [message.author.id], roles: [] },
});
}
}
i want to send a message in the channel when a command is in cooldown
Psyonix_
Psyonix_OP•12mo ago
i see, but the bot won't send that message
Favna
Favna•12mo ago
Does your listener trigger at all? Like for other preconditions
Psyonix_
Psyonix_OP•12mo ago
my messageCommandSuccess works just fine does not seem like messageCommandDenied works oh now it does seem to work but the timer goes back is there also a way to just send that message once and that's it?
Favna
Favna•12mo ago
The timer counts down because it uses a Discord formatted timer... there is practically no reason to send a static unchanging time because at least this way people can constantly tell how long remains without using the command over and over again to see it But if you really don't want it then you identify the precondition through error.identifier (currently not destructured in your listener) then use payload.context.remaining to get the amount of milliseconds the cooldown remains then send a custom message instead of the default one provided by Sapphire
Psyonix_
Psyonix_OP•12mo ago
i see, thanks for that now, is there also a way to just send that message once and that's it?
Favna
Favna•12mo ago
what do you mean
Psyonix_
Psyonix_OP•12mo ago
like if you keep executing the command it would send the ratelimiter message multiple times and that could cause the bot to be ratelimited by discord like just send it once and then ignore the rest of the executions while ratelimited
Favna
Favna•12mo ago
By default no there is not because the precondition will always trigger which will always trigger the listener. You could write some mechanic in that uses identifier and message author to keep a cache of whose been informed already. Basic JavaScript stuff there. Or you could switch to chat input commands then you can make it send as an ephemeral reply to the user instead so it won't spam the chat 🤷
Psyonix_
Psyonix_OP•12mo ago
oh i see
Want results from more Discord servers?
Add your server