【问题标题】:Send private message to a list of people向人员列表发送私人消息
【发布时间】:2020-12-05 18:39:37
【问题描述】:

我的机器人将向被接受的人员列表发送一条私人消息。 我想输入一个命令,向.json 文件中的每个人发送消息。 我试过循环,但不能让它工作。

我认为这是 json 文件:

{
    "1": "Name#0001",
    "2": "Guy#0001",
    "3": "Person#0001"
}

这是我的 index.js 或 main.js:

var ytLinks = JSON.parse(fs.readFileSync('./ytvids.json', 'utf8'));

if (args[1] === 'send') {
  const userValues = Object.values(acceptedUsers);
  var userList = '';
  var i;
  for (i = 0; i < userValues.length; i++) {
    userList += userValues[i];
  }
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    something.json:

    {
        "1": "Name#0001",
        "2": "Guy#0001",
        "3": "Person#0001"
    }
    

    index.js:

    const obj = require('./something.json') // require object from file
    
    // execute function on each entry (user tag)
    Object.values(obj).forEach((tag) => {
    
        // find each user via tag and send DM 
        client.users.cache.find(user => user.tag === tag).send('This is a DM') 
    });
    


    请注意,过多的大规模 DMing 可能会导致 Discord 的服务条款出现问题

    【讨论】:

    • 从性能的角度来看,Array.forEach 比 for...of 和 A​​rray.map 慢得多,因此您应该将其切换到 for...of 循环。
    • 如果我错了,请纠正我,但Array#forEach()绝对不会比Array#map()
    • 此外,虽然for...of 被认为更快,但只有在迭代数百万个元素时才会有所不同。否则,没关系。
    猜你喜欢
    • 2012-09-21
    • 2017-06-04
    • 2020-06-16
    • 1970-01-01
    • 2019-10-09
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    相关资源
    最近更新 更多