【问题标题】:Counting game discord js计数游戏不和谐js
【发布时间】:2020-08-15 06:10:40
【问题描述】:

大家好。我创建了自己的机器人 我有很多很棒的东西,比如游戏等。 但是,我想做一个计算游戏。 我有一个叫“计数”的频道 我想设置我的机器人:

用户 1:456

用户 2:457

机器人:458

我的问题是,当没有其他人在计数时,如何让机器人计数?但只有一次。 (看例子^^)

如果可以,可以给我一个示例代码吗?谢谢!!

【问题讨论】:

    标签: bots discord discord.js counting


    【解决方案1】:

    试试这个:

    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')
    

    说明

    当用户(不是机器人)在计数通道中发送消息时,机器人会检查用户是否正确计数 (if (Number(content) === count + 1)。
    如果是,它会增加 count,如果存在超时则移除超时 (if (timeout) clearTimeout(timeout)),并安排机器人在 30 秒后计数 (setTimeout(() => channel.send(++count), 30000))。
    如果不是,机器人会发送一条消息,重置count,并清除超时(如果存在)。

    当机器人发送消息时,它不会触发任何消息。当机器人计数时,Number(content) === count 因为它已经增加了。

    【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2017-12-29
    • 2021-02-16
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2017-07-11
    • 2017-11-15
    • 2020-08-05
    相关资源
    最近更新 更多