【问题标题】:Discord.js how can I create an invite for every guild my bot is in?Discord.js 如何为我的机器人所在的每个公会创建邀请?
【发布时间】:2020-12-11 16:05:20
【问题描述】:

我正在尝试发出一个命令,在那里我可以获得该机器人当前所在的每个公会邀请。 当前代码:

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

【问题讨论】:

  • 请注意,Discord 只允许机器人在被允许的情况下创建公会邀请,即。公会管理员执行命令来创建新邀请。未经公会版主/管理员许可,不得创建邀请

标签: javascript node.js discord discord.js


【解决方案1】:

试试这个:

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

例如,这是在运行命令后得到的:


【讨论】:

  • TypeError: client.guilds.createInvite 不是函数
  • 我用的是V12,client.gulds.createInvite不是函数
  • createInvite()GuildChannel 的方法,而不是Guild
猜你喜欢
  • 2019-11-14
  • 2021-09-05
  • 2021-11-02
  • 2019-08-28
  • 1970-01-01
  • 2021-04-25
  • 2020-10-16
  • 2021-10-14
  • 2018-07-28
相关资源
最近更新 更多