0
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: [] }
4

1 回答 1

0

消息可能在 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 的类型并不是那么好,所以需要类型断言as GuildTextBasedChannel来让 TypeScript 知道message.channel如果 ifmessage.guild不是的话,它必须是一个公会基于文本的通道null


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

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

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

于 2021-10-03T23:28:15.187 回答