【问题标题】:Discord.JS, Numbering users that send a specific phrase to Bot DMsDiscord.JS,对向 Bot DM 发送特定短语的用户进行编号
【发布时间】:2021-06-18 00:47:22
【问题描述】:

我正在我的机器人中创建一个函数,当一个短语被发送到机器人时,它显示它是第一个发送该短语的用户,然后是第二个用户,它显示第二个,依此类推。到目前为止,我有 for 循环,因此它一次显示所有数字 1-3。我只是在创建为每个发送消息的用户显示一个数字的功能时遇到了一些困难。 For more clarification

感谢任何帮助,谢谢!

代码:


        const channel = bot.channels.cache.get('732757852615344139');
    
        channel.updateOverwrite(message.author,{
            VIEW_CHANNEL: true,
          })     
          for(let i = 1; i< 4; i++){
        let scavWelcome = new Discord.MessageEmbed()
          .setTitle('Good Work')
          .setDescription(`Welcome ${message.author}, you placed number ${i}`)
          channel.send(scavWelcome)
        }
      }

【问题讨论】:

  • 在全局范围内有一个数字,每次说这句话时增加数字,显示数字
  • 类似:if (message.content == 'Phrase'{ i++ } 或者类似的东西?
  • 是的,只要确保在消息事件之外定义i,否则变量将在每条消息上重置,我会尽快提供答案

标签: discord discord.js


【解决方案1】:

通过全局变量或提升到 bot 对象设置全局计数器变量和最大计数。增加每条消息的计数器。一旦计数器达到最大值,将计数器重置回 1。

// Under where you defined bot
bot.counter = 1;
bot.maxCount = 4;

bot.on('message', message => {
   // Your message event code...
   
   const channel = bot.channels.cache.get('732757852615344139');
    
   channel.updateOverwrite(message.author,{
       VIEW_CHANNEL: true
   })     

   if (message.content === 'Phrase Here') {
      if (bot.counter === bot.maxCount) bot.counter = 1;
      else bot.counter++;
   }
   
   let scavWelcome = new Discord.MessageEmbed()
      .setTitle('Good Work')
      .setDescription(`Welcome ${message.author}, you placed number ${bot.counter}`)
   channel.send(scavWelcome);
});

【讨论】:

  • 好像成功了,我总是在全球专柜上遇到麻烦,不知为何容易混淆物流哈哈,非常感谢!
【解决方案2】:

如果我正确理解了您的问题,您可以使用数组来存储用户并获取与其位置对应的元素的索引

client.on('message', message => {
const userArr = [];
if (message.content.toLowerCase() == 'a phrase') userArr.push(`${message.author.username`);
});

然后您可以通过执行以适当的顺序查看列表

for (let i = 0; i < userArr.length; i++) arrWithIndex.push(`${i + 1} ${userArr[i]}`) // i + 1 because index starts with 0, but we're counting from 1
message.channel.send(arrWithIndex.join('\n'));

如果我误解了您的问题,请您在 cmets 中解释一下,ty

【讨论】:

  • 我看到你想要的解决方案,这可能可行,其他人的回复似乎已经解决了问题,感谢回复!
猜你喜欢
  • 1970-01-01
  • 2021-10-26
  • 2021-02-24
  • 2021-12-24
  • 2021-12-29
  • 2021-11-09
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
相关资源
最近更新 更多