javascript - 如何修复该代码上的 "DiscordAPIError: Unknown Channel"?

标签 javascript discord.js

我有这段代码,运行良好。 用于超时后删除空语音 channel ,有人加入时也会清除超时。

client.once('ready', () => {
    var interval = setInterval(function() {
        const parent = client.channels.get("469457677144293376")
        parent.children.filter(cha => cha.type === "voice" && cha).forEach(cha => {
            if (cha.members.size === 0) {
                var timer = setTimeout(() => {
                    cha.delete()
                }, 5000);
            }
            var interval = setInterval(function() {
             if (cha.members.size !== 0){
               clearTimeout(timer)
             }
            }, 1 * 500);
        })
    }, 1 * 500);
});

这是我在控制台上遇到的错误:

(node:8025) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Channel
    at item.request.gen.end (/rbd/pnpm-volume/9841fb11-337a-4c3b-bf2e-08d60ed04d96/node_modules/.registry.npmjs.org/discord.js/11.5.1/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15)
    at then (/rbd/pnpm-volume/9841fb11-337a-4c3b-bf2e-08d60ed04d96/node_modules/.registry.npmjs.org/snekfetch/3.6.4/node_modules/snekfetch/src/index.js:215:21)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
(node:8025) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:8025) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:8025) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Channel
    at item.request.gen.end (/rbd/pnpm-volume/9841fb11-337a-4c3b-bf2e-08d60ed04d96/node_modules/.registry.npmjs.org/discord.js/11.5.1/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15)
    at then (/rbd/pnpm-volume/9841fb11-337a-4c3b-bf2e-08d60ed04d96/node_modules/.registry.npmjs.org/snekfetch/3.6.4/node_modules/snekfetch/src/index.js:215:21)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

我试过 return、filter、catch,什么都没有:/

有什么想法吗?谢谢 :)

最佳答案

每隔半秒,您的代码就会循环遍历既是已定义类别的子 channel 又是语音 channel 的每个 channel 。您将超时设置为 5 秒以删除 channel 。但是,下一次间隔滴答时,它会再次设置相同的超时,因为 channel 仍然存在。当重复的超时尝试删除曾经存在的 channel 时,将抛出您的错误,因为第一个超时将其删除。

通过这段代码,实现了一个队列,只有在队列中的 channel 才会被设置为删除。如果 channel 不为空,它将从队列中移除,超时不会删除它。这样,在下一个滴答声中, channel 将再次排队。如果 channel 确实为空,则在5秒后删除,然后从队列中移除。如果 channel 不再存在,代码将不会尝试删除它,而是将其从队列中移除。最后,当不再有语音 channel 存在时,该间隔将被清除。

client.once('ready', () => {
  const queue = [];

  const interval = setInterval(() => {
    const parent = client.channels.get('469457677144293376');
    if (!parent) {
      console.error('Parent channel missing.');
      return clearInterval(interval);
    }

    const voiceChildren = parent.children.filter(channel => channel.type === 'voice');
    if (voiceChildren.size === 0) {
      console.log('All voice channels deleted.');
      return clearInterval(interval);
    }

    voiceChildren.filter(channel => !queue.includes(channel.id)).forEach(channel => {
      queue.push(channel.id);

      setTimeout(() => {
        if (!client.channels.get(channel.id)) {
          console.log(`"${channel.name}" was already deleted; aborting.`);
          return queue.splice(queue.indexOf(channel.id), 1);
        }

        if (channel.members.size !== 0) {
          console.log(`"${channel.name}" isn't empty; trying again.`);
          return queue.splice(queue.indexOf(channel.id), 1);
        }

        channel.delete()
          .then(() => queue.splice(queue.indexOf(channel.id), 1))
          .catch(console.error);
      }, 5000);
    });
  }, 500);
});

关于javascript - 如何修复该代码上的 "DiscordAPIError: Unknown Channel"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56922846/

相关文章:

Javascript:在不使用警告框的情况下警告用户

javascript - .drawImage 函数为 Canvas 抛出 "TypeError: Image or Canvas expected"

discord - 如何在 Discord.js 中使用 channel 名称查找 channel ?

javascript - 在 <asp :ScriptReference> Path property value 上追加查询字符串

Javascript/JQuery 删除代码中的父 TR 问题

javascript - 如何使用 jQuery 将选定值设置为多个 Select 输入?

Javascript 多变量赋值

javascript - 无法同时运行 2 个 nodemon 检查程序

javascript - 如何从一系列youtube视频链接中获取一系列youtube视频名称?

javascript - 获取用户的公会昵称