如何在Discord.js中获取加入行会的ID

时间:2020-05-18 15:00:06

标签: javascript discord.js

我需要机器人在加入api时获得行会ID。目前,我已尝试过此操作,但出现错误:

Cannot read property 'id' of undefined

我的代码

client.on("guildCreate", (guild) => {
  request(
    {
      url:
        "https://urlgoeshere" +
        client.guild.id +
        "/" +
        key +
        "/" +
        client.guild.memberCount,
    },
    function (error, httpResponse, body) {
      console.error("error:", error); 
      console.log("body:", body); 
    }
  );
});

1 个答案:

答案 0 :(得分:3)

您的问题是您尝试从guild.id获取guild.memberCountclient

guildCreate回调参数返回加入的Guild,因此请进行以下修改:

client.on("guildCreate", (guild) => {
  request(
    {
      url:
        "https://urlgoeshere" +
        guild.id +
        "/" +
        key +
        "/" +
        guild.memberCount,
    },
    function (error, httpResponse, body) {
      console.error("error:", error); 
      console.log("body:", body); 
    }
  );
});