【问题标题】:Bulk Deleting discord.js not working because of wrong channel由于错误的频道,批量删除 discord.js 无法正常工作
【发布时间】:2021-11-24 10:55:29
【问题描述】:
import DiscordJS, { TextChannel, Intents, Message, Channel } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

//sets prefix to be used when running bot commands
const prefix = '~';

//This lets the discord bot know what your intentions are using this bot. Hence the guilds, guilds messages and message reactions
const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
        Intents.FLAGS.DIRECT_MESSAGES
    ]
})


//Deleting messages in bulk
client.on("message", (message) => {
    if (message.content.toLowerCase().startsWith(prefix + "clearchat")) {
        async function clear() {
            message.delete();
            var fetched = await message.channel.messages.fetch({limit: 99})
            message.channel.bulkDelete(fetched);
        }
        clear();
    }
});

我正在尝试批量删除,但问题是 message.channel.bulkDelete(fetched); .channel 部分说它是 TextBasedChannel 而不是 TextChannel。我之前问过某人,他们说我在使用 DMChannel,而我应该使用 TextChannel。我知道它们是不同的类,但我不确定我是如何在我的代码中使用 DMChannel 而不是 TextChannel 的。我不知道如何解决这个问题,如果有人有一个链接可以告诉我不同​​之处,我会很感激的。因为我在服务器而不是直接消息中使用机器人,所以很难理解 DMChannel。我只是困惑

编辑: 我能够按预期清除聊天,但现在我收到 DiscordAPIError。 我可以抓住错误吗? 这是错误消息:

编辑 2:这是上述错误消息之后的内容

 DiscordAPIError: You can only bulk delete messages that are under 14 days old.
    at RequestHandler.execute (C:\Users\theod\Desktop\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\theod\Desktop\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
    at async TextChannel.bulkDelete (C:\Users\theod\Desktop\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:312:7) {
  method: 'post',
  path: '/channels/872986149294047234/messages/bulk-delete',
  code: 50034,
  httpStatus: 400,
  requestData: { json: { messages: [Array] }, files: [] }

【问题讨论】:

  • 它会给你什么错误?你能证明吗?

标签: typescript discord.js bulk-delete


【解决方案1】:

消息可能位于 DM 中,您无法批量删除 DM 频道中的消息。首先检查消息是否在公会中:

import type { NewsChannel, TextChannel, ThreadChannel } from 'discord.js';

type GuildTextBasedChannel = TextChannel | NewsChannel | ThreadChannel

if (message.content.toLowerCase().startsWith(prefix + "clearchat")) {
    if (message.guild) {
        async function clear() {
            message.delete();
            var fetched = await message.channel.messages.fetch({limit: 99})
            (message.channel as GuildTextBasedChannel).bulkDelete(fetched, true);
        }
        clear();
    }
}

不幸的是,Discord.js 的类型不是很好,所以如果 message.guild 不是 @,则需要类型断言 as GuildTextBasedChannel 让 TypeScript 知道 message.channel 必须是基于公会文本的频道987654326@.


如果你愿意,你可以定义一个辅助函数来消除对这种类型断言的需要:

import type { Message } from 'discord.js';

function inGuild(message: Message): message is Message & { readonly channel: GuildTextBasedChannel } {
    return message.guild !== null;
}

if (inGuild(message)) {
  message.channel.bulkDelete(99, true); // no type assertion needed
}

或者,您可以使用以下内容:

import type * as Discord from 'discord.js';

type Message = Discord.Message &
  (
    | {
        readonly channel: GuildTextBasedChannel;
        readonly guild: Discord.Guild;
      }
    | {
        readonly channel:
          | Discord.DMChannel
          // if you're using partials
          | Discord.PartialDMChannel;
        readonly guild: null;
      }
  );

client.on("messageCreate", (_message) => {
    const message = _message as Message;
    // rest of code...
    // type assertion not required
});

bulkDelete(fetched, true) 中的 true 参数表示 Discord.js 将自动过滤掉超过 14 天的消息。 Discord 不允许您批量删除超过 14 天的消息,这是您的错误消息告诉您的。

【讨论】:

  • 是的,我解决了这个问题!谢谢,但是现在清除聊天后会弹出一个错误。我贴了上面错误信息的图片。
  • 您问题中的屏幕截图显示了弃用警告,因为message event got renamed to mesageCreate。您能否使用与DiscordAPIError 相关的完整错误来编辑您的问题(throw new DiscordAPIError 行下应该有更多信息)?请将错误作为文本复制并粘贴到您的问题中;不要使用图片。
  • 我只是把其他部分放在上面。
  • @TheoChow 抱歉回复晚了!我已经更新了我的答案。
猜你喜欢
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 2021-07-31
  • 2020-03-03
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多