简体   繁体   English

如何获得加入Discord.js的公会ID

[英]How to get the ID of a guild on join in Discord.js

I need the bot to get the guild ID on join for an api.我需要机器人获取加入 api 的公会 ID。 At the moment I've tried this, but I get an error:目前我已经尝试过了,但出现错误:

Cannot read property 'id' of undefined

My code我的代码

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

Your issue is that you try to get guild.id and guild.memberCount from client .您的问题是您尝试从client获取guild.idguild.memberCount

guildCreate callback parameter returns the Guild joined, so modify as following: 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); 
    }
  );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM