发送私人消息到人员列表

时间:2020-08-16 12:49:19

标签: javascript node.js discord discord.js

我的机器人将向接收到的人列表发送私人消息。 我想输入一个向.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];
  }
}

1 个答案:

答案 0 :(得分:4)

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


请注意,大量DM会导致Discord服务条款出现问题