尝试获取我的机器人所在的行会邀请时出错

时间:2020-06-02 23:50:08

标签: discord discord.js

某种程度上,它仅在我创建新频道时才起作用,尽管考虑到将我的机器人添加到服务器中的人不会专门为此创建新频道,这确实令人不快。

错误:

{ DiscordAPIError: Unknown Channel
    at RequestHandler.execute (C:\Users\Multiverse\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  name: 'DiscordAPIError',
  message: 'Unknown Channel',
  method: 'post',
  path: '/channels/702061566601986049/invites',
  code: 10003,
  httpStatus: 404 }

我正在使用的代码:

client.on("message", msg => {
  if (msg.content === "!guildinv") {
client.guilds.cache.forEach(guild => {
  guild.channels.cache.first().createInvite()
    .then(inv => console.log(`${guild.name} | ${inv.url}`)).catch(console.error);

1 个答案:

答案 0 :(得分:0)

这可能是因为第一个频道是类别。

const textChannel = guild.channels.cache.find(c => c.type === "text");
if(textChannel) textChannel.createInvite()
//if there is no textChannel found it could mean there is no *cached* text channel
//so you should prob make a fallback to fetch for a text channel
//then again I think all channels are cached so I'm not sure

另外一种选择是guild.fetchInvites();

https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=fetchInvites