簡體   English   中英

Discord.js 如何為我的機器人所在的每個公會創建邀請?

[英]Discord.js how can I create an invite for every guild my bot is in?

我正在嘗試發出命令,在那里我可以獲得該機器人當前所在的每個公會邀請。當前代碼:

client.on('message', async (message) => {
 if (message.content.startsWith(prefix + 'invite')) {
  let invite = client.guilds
   .createInvite({
    maxAge: 0, // 0 = infinite expiration
    maxUses: 0, // 0 = infinite uses
   })
   .catch(console.error);
  message.channel.send(invite);
 }
});

錯誤:

DiscordAPIError: Cannot send an empty message

嘗試這個:

 var invites = []; // starting array
    message.client.guilds.cache.forEach(async (guild) => { // iterate loop on each guild bot is in

      // get the first channel that appears from that discord, because
      // `.createInvite()` is a method for a channel, not a guild.
      const channel = guild.channels.cache 
        .filter((channel) => channel.type === 'text')
        .first();
      if (!channel || guild.member(client.user).hasPermission('CREATE_INSTANT_INVITE') return;
      await channel
        .createInvite({ maxAge: 0, maxUses: 0 })
        .then(async (invite) => {
          invites.push(`${guild.name} - ${invite.url}`); // push invite link and guild name to array
        })
        .catch((error) => console.log(error));
      console.log(invites);
    });

例如,這是運行命令后得到的:

不和諧的例子


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM