【问题标题】:Is there a way to get invites from ALL the guilds my bot is in?有没有办法从我的机器人所在的所有公会中获得邀请?
【发布时间】:2020-10-16 13:16:31
【问题描述】:

关于如何从你不参与但我的机器人参与的不和谐中获得邀请的任何想法?

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

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

    这种方法更安全,(由于 async/await 可能会慢一些):

    (这需要在异步函数中)

    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)
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 此方法不安全,因为您可能会获得一个您无权访问的频道
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2019-11-14
      • 2021-09-05
      • 2021-01-12
      • 2022-07-01
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多