如何在该代码上修复“ DiscordAPIError:未知频道”?

时间:2019-07-07 14:05:03

标签: javascript discord.js

我有此代码,工作正常。 它用于在超时后删除空的语音通道,也将在有人加入时清除该超时。

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)

我尝试了返回,过滤,捕获等操作:/

有什么想法吗?还有:)

2 个答案:

答案 0 :(得分:1)

每半秒,您的代码就会遍历每个既属于已定义类别又属于语音渠道的渠道。您将超时设置为5秒以删除频道。但是,下一次间隔计时时,由于通道仍然存在,因此它再次设置了相同的超时。当重复的超时尝试删除曾经存在的通道时,会抛出错误,因为第一个通道将其删除。

使用此代码,将实现一个队列,并且仅将队列中的 not 通道设置为删除。如果通道不为空,则将从队列中将其删除,并且超时不会将其删除。这样,在下一个滴答声中,通道将再次排队。如果通道确实为空,它将在5秒钟内删除,然后从队列中删除。如果通道不再存在,则代码不会尝试删除它,而是将其从队列中删除。最后,当不再有语音通道时,间隔将被清除。

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

答案 1 :(得分:0)

我改进了Slothiful的代码:

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

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

    const voiceChildren = parent.children.filter(channel => channel.type === 'voice');
    if (voiceChildren.size === 0) {
      return;
    }

    voiceChildren.filter(channel => !queue.includes(channel.id)).forEach(channel => {
      if (channel.members.size !== 0) {
      return;
     }
      queue.push(channel.id);

        var timer = setTimeout(() => {
        if (channel.members.size !== 0) {
          clearTimeout(timer)          
          return queue.splice(queue.indexOf(channel.id), 1);
        }
        channel.delete()
          .then(() => queue.splice(queue.indexOf(channel.id), 1))
          .catch(console.error);
      }, 5000);
    });
  }, 500);
});

有一个错误,是queue而不是deleteQueue