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();
}
});
I'm trying to bulk delete, but the problem is that message.channel.bulkDelete(fetched); The .channel part is saying its a TextBasedChannel and not TextChannel. And I asked someone about this earlier and they said that I'm using a DMChannel when I should be using a TextChannel. I understand that they are different classes, but I'm not sure how I'm using DMChannel and not TextChannel in my code. I'm not sure how to fix this and if someone had a link to something that tells me the difference, I'd appreciate it. Just having a hard time understanding DMChannel since I'm using the bot in a server and not in the Direct Messages. I'm just confused
EDIT:
I was able to clear the chat as intended, but now I get a DiscordAPIError.
Can I just catch the error?
Here's the error message:
EDIT 2: This is what is after the above Error message
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: [] }
The message could be in a DM, and you can't bulk delete messages in a DM channel. Check if the message is in a guild first:
Unfortunately, the typings for Discord.js aren't that great, so the type assertion
as GuildTextBasedChannel
is needed to let TypeScript know thatmessage.channel
must be a guild text-based channel if ifmessage.guild
is notnull
.If you want, you can define a helper function that would eliminate the need for this type assertion:
Alternatively, you could use something like this:
The
true
argument inbulkDelete(fetched, true)
means that Discord.js will automatically filter out messages that are over 14 days old. Discord doesn't allow you to bulk delete messages over 14 days old, which is what your error message is telling you.