【问题标题】:I can't understand why my message doesn't send to every user that decided to be notified. It only sends to one我不明白为什么我的消息没有发送给每个决定收到通知的用户。它只发送给一个
【发布时间】:2021-11-24 02:42:08
【问题描述】:

我不明白为什么我的消息没有发送给所有决定接收通知的用户。它只发送给一个。如果有任何关于 quick.db 的文件可以修复它,当然让我知道。


这是 notify.js 代码


module.exports = {
  name: 'notify',
  description: 'Notify people when a new update is released',
  category: "Notified",
  execute(client, message, args, Discord) {
    if(message.member.user.id === '601364286731714591'){
      const db = require('quick.db')
      let notification = db.get(`notifiedusers_${message.author.id}`);
      if(notification === null) {
        return message.channel.send('Nobody is notified for any updates')
      }
      const content = args.join(" ")
      if(!content) return message.channel.send('What would be the content of the Update Be ?')
      
      const newEmbed = new Discord.MessageEmbed()
        .setColor('#b5b5b5')
        .setTitle('Update')
        .setDescription(`${content}`)
      message.channel.send('Notifying everybody that desiced to be notified')
      try{
      const chx = client.users.cache.get(notification)
      chx.send({embeds: [newEmbed]})
      }finally{
        message.channel.send('Everybody was notified')
      }
    }else{
      return
    }
  }
}

【问题讨论】:

    标签: javascript database discord discord.js


    【解决方案1】:
          const chx = client.users.cache.get(notification)
          chx.send({embeds: [newEmbed]})
    

    在此函数中,您只能使用 1 个 id 来获取该人的用户对象,如果您有一个可以使用 db.push 存储的人员 id 数组,则可以获取每个人的 id 并将其发送给每个人

          for (const id of <array_of_id>){
             const chx = client.users.cache.get(id)
             chx.send({embeds: [newEmbed]})
          }
    

    另外,对 discord 的 API 发出太多请求会得到ratelimited,所以请给每个用户时间send

          for(const id of <array_of_id>){
             const chx = client.users.cache.get(id)
             chx.send({embeds: [newEmbed]})
             // Wait 3000 milliseconds (3 minutes)
             await new Promise(r => setTimeout(r, 3000));
          }
    

    【讨论】:

    • 欢迎溢出,我不太明白 id 数组是什么意思,有什么更好更简单的解释方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2019-08-20
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多