Search code examples
botsdiscorddiscord.jscounting

Counting game discord js


Hewo everyone. I created my own bot I have a lot of great things like games etc. However, I want to do a couting game. I have a channel called "Counting" And i want to setup my bot do like:

User 1: 456

User 2: 457

Bot: 458

My question is, How can I make the bot count when no one else is counting? But just once. (Look the example^^)

If you can, can you give me an example code please? Thanks!!


Solution

  • Try this:

    const {Client} = require('discord.js')
    
    const client = new Client()
    
    // Stores the current count.
    let count = 0
    // Stores the timeout used to make the bot count if nobody else counts for a set period of
    // time.
    let timeout
    
    // Discord.js v12:
    // client.on('message', ({channel, content, member}) => {
    // Discord.js v13:
    client.on('messageCreate', ({channel, content, member}) => {
      // Only do this for the counting channel of course
      // If you want to simply make this work for all channels called 'counting', you
      // could use this line:
      // if (client.channels.cache.filter(c => c.name === 'counting').has(channel.id))
      if (channel.id === 'counting channel id') {
        // You can ignore all bot messages like this
        if (member.user.bot) return
        // If the message is the current count + 1...
        if (Number(content) === count + 1) {
          // ...increase the count
          count++
          // Remove any existing timeout to count
          if (timeout) clearTimeout(timeout)
          // Add a new timeout
          timeout = setTimeout(
            // This will make the bot count and log all errors
            () => channel.send(++count).catch(console.error),
            // after 30 seconds
            30000
          )
        // If the message wasn't sent by the bot...
        } else if (member.id !== client.user.id) {
          // ...send a message because the person stuffed up the counting (and log all errors)
          channel.send(`${member} messed up!`).catch(console.error)
          // Reset the count
          count = 0
          // Reset any existing timeout because the bot has counted so it doesn't need to
          // count again
          if (timeout) clearTimeout(timeout)
        }
      }
    })
    
    client.login('your token')
    

    Explanation

    When a user (that isn't the bot) sends a message in the counting channel, the bot checks if the user is counting correctly (if (Number(content) === count + 1)).
    If they are, it increments count, removes the timeout if it exists (if (timeout) clearTimeout(timeout)), and schedules the bot to count after 30 seconds (setTimeout(() => channel.send(++count), 30000)).
    If they aren't, the bot sends a message, resets count, and clears the timeout (if it exists).

    When the bot sends a message, it won't trigger any of that. When the bot counts, Number(content) === count because it was already incremented.