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。我早些时候问过别人这件事,他们说我应该用TextChannel的时候,却在用DMChannel。我知道它们是不同的类,但我不确定如何在代码中使用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: [] }发布于 2021-10-03 23:28:15
消息可能在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不是null,那么message.guild必须是一个基于行会文本的通道。
如果需要,您可以定义一个帮助器函数来消除对此类型断言的需要:
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天的邮件。不一致不允许您批量删除超过14天的邮件,这是您的错误消息告诉您的。
https://stackoverflow.com/questions/69428808
复制相似问题