Giter VIP home page Giter VIP logo

Comments (3)

almostSouji avatar almostSouji commented on September 23, 2024 1

expiredTimestamp is defined based on expirationTime, which adds the cooldownAmount to the timestamp of the last use:

const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
// ...
const expiredTimestamp = Math.round(expirationTime / 1000);

from guide.

jdic avatar jdic commented on September 23, 2024

I know, this is the code that I have, what I wanted to say, is that in discord the time of the message that shows <t: :R> shows a timestamp from when the message was started, but the text implies that it should show a countdown of how much time is left to use the message, am I right or what the guide shows is wrong?

Continue to count the time elapsed, example of the bot response using the example in the guide:

Please wait, you are on a cooldown for ping. You can use it again in 16 seconds ago

My code:

const { Collection, Interaction } = require('discord.js')

module.exports =
{
  /**
   * Checks if the user is in timeout to re-execute a certain command, if yes it returns true, otherwise it returns false.
   * @param { Interaction } interaction
   * @param { object } command
   * @returns { Boolean }
   */
  hasCooldown: async (interaction, command) =>
  {
    if(!command.cooldown) return false
    const { cooldowns } = interaction.client

    if (!cooldowns.has(command.data.name)) cooldowns.set(command.data.name, new Collection())

    const now = Date.now()
    const timestamps = cooldowns.get(command.data.name)
    const cooldownAmmount = command.cooldown * 1000

    if (timestamps.has(interaction.user.id))
    {
      const expirationTime = timestamps.get(interaction.user.id) + cooldownAmmount

      if (now < expirationTime)
      {
        const expiredTimestamp = Math.round(expirationTime / 1000)
        return !!interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again in <t:${expiredTimestamp}:R>.`, ephemeral: true })
      }
    }

    timestamps.set(interaction.user.id, now)
    setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmmount)
    return false
  }
}

from guide.

jdic avatar jdic commented on September 23, 2024

It seems to me that the only solution is that once the countdown reaches 0, before the counter starts to increase its value, delete the interaction.

if (now < expirationTime)
{
  const expiredTimestamp = Math.round(expirationTime / 1000)
  interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again in <t:${expiredTimestamp}:R>`, ephemeral: true })
  setTimeout(() => { interaction.deleteReply() }, expirationTime - now)
  return true
}

from guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.