有没有办法从我的机器人所在的所有公会中获得邀请?

时间:2020-06-26 00:26:54

标签: node.js discord discord.js

关于如何从您不在但我的机器人已进入的不和谐中获得邀请的任何想法?

2 个答案:

答案 0 :(得分:1)

我想您想被邀请加入您的机器人所在的每个公会

    client.guilds.cache.forEach(guild => {
        guild.channels.cache.filter(x => x.type != "category").random().createInvite()
          .then(inv => console.log(`${guild.name} | ${inv.url}`));
      });

答案 1 :(得分:0)

Rez的回答是常见的,但不是安全的,您获得的随机频道可能是您无权创建邀请的频道。

这种方法比较安全(自异步/等待以来可能会慢一些):

(这需要在异步函数中)

const invites = [];

//cant use async inside of forEach 
//https://www.coreycleary.me/why-does-async-await-in-a-foreach-not-actually-await/
for (const [guildID, guild] of client.guilds.cache) {
    //if no invite was able to be created or fetched default to string
    let invite = "No invite";

    //fetch already made invites first
    const fetch = await guild.fetchInvites().catch(() => undefined);

    //if fetch worked and there is atleast one invite
    if (fetch && fetch.size) {
        invite = fetch.first().url;
        invites.push({ name: guild.name, invite });
        continue;
    }

    for (const [channelID, channel] of guild.channels.cache) {
        //only execute if we don't already have invite and if the channel is not  a category
        if (!invite && channel.createInvite) {
            const attempt = await channel.createInvite().catch(() => undefined);

            if (attempt) {
                invite = attempt.url;
            }
        }
    }

    invites.push({ name: guild.name, invite });
}

console.log(invites)