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