当新公会邀请机器人时,无法弄清楚如何从新公会中获取公会所有者 ID

时间:2021-07-15 19:08:02

标签: javascript discord.js

所以我试图让机器人从刚刚加入的公会中获取公会所有者 ID,以便它以后可以与我的数据库进行比较,看看他们是否有一个可以锁定或解锁某些命令的角色。但无论我怎么做,所有者 ID 都会以未定义状态返回,并且所有内容都显示“无法读取未定义的 X”。天气我尝试 .users .guild 等

/// adding a guild to the database upon invite
bot.on('guildCreate', async (guild) => {
    // Guild the user needs to have the role in
    let guildA = process.env.BOT_GUILD;
    console.log(guildA);

    // Role that the user needs
    let requiredRole = process.env.PAID_ROLE;
    console.log(requiredRole);

    // Member object of the user in guildA
    let member = guild.bot.users.fetch(guild.ownerID);
    console.log(member);

----------------------------

    i have also tried 

    // Member object of the user in guildA
    let member = bot.guild.ownerID;
    console.log(member);


    // Member object of the user in guildA
    let member = bot.guilds.cache.get(guild.ownerID).id;
    console.log(member);

    // Member object of the user in guildA
    let member = guilds.members.cache.get(guild.ownerID);
    console.log(member);

    etc etc etc

如何在这种情况下正确获取公会所有者 ID?在这一点上,我觉得 iv 尝试了所有对其他人有用的方法。

2 个答案:

答案 0 :(得分:2)

在 GuildCreate 事件中,如果您想获取公会所有者作为 user 对象,您有 2 个选择。 Guild 对象包含您已经发现的 ownerID 属性。还有 owner 属性,它是一个 GuildMember 对象。我建议检查此所有者属性是否存在,您可以通过访问 GuildMember#user 从中获取用户,这将返回该公会所有者的用户对象。

如果成员不存在,您将不得不获取用户。您可以通过在获取用户时等待请求来完成此操作。由于 fetch 方法返回一个承诺。

例如:

client.on('guildCreate', async (guild) => {
    const guildOwner = guild.owner ? guild.owner : await 
    guild.members.fetch(guild.ownerID);
    console.log(guildOwner); 
})

答案 1 :(得分:2)

不确定我是否理解正确,但可能您确实想将所有者作为成员获取。您可以使用 guild.ownerguild.members.fetch(ID)

bot.on('guildCreate', async (guild) => {
  // Guild the user needs to have the role in
  let guildA = process.env.BOT_GUILD;
  console.log(guildA);

  // Role that the user needs
  let requiredRole = process.env.PAID_ROLE;
  console.log(requiredRole);

  // fetch the guild owner as a member
  let member = await guild.members.fetch(guild.ownerID);

  console.log(member);
});