I'm using Typescript 4.0.2, the error is with the Typescript typings from what I can see. The error is:
error TS2339: Property 'bulkDelete' does not exist on type 'TextChannel | DMChannel | NewsChannel'. Property 'bulkDelete' does not exist on type 'DMChannel'.
client.on("message", async(msg: discord.Message) => {
if (msg.content == "!delete") {
let messages = await msg.channel.messages.fetch({ limit: 100 });
msg.channel.bulkDelete(messages);
}
})
Further details:
Relevant client options:
You never check if channel is a DM channel. You cannot bulkDelete there, so this is expected behavior.
I check if channel is a DM channel on my normal code, forgot to include it here it seems
Ok so seems like the problem is with the way I filter DM channels, somehow. I keep my commands in separate files and export a function from them. I then run the functions in message event. I also have a filter in message event that looks like this:
if (msg.channel.type == "dm") return;
if (msg.author.bot) return;
if (!msg.content.startsWith(config.prefix)) return;
I'm not sure why it's doing that, I'm guessing it's because i keep the commands in a separate file it can't detect that.
I fixed the problem by also adding the filter into the command.
@owoNUB any chance on a code example? New to discord.js and stuck on the same issue.
i literally went into the index.d.ts and slapped it there myself. probably not a good practice owo
if (message.channel.type === "dm") return;
anyways I think they meant this, didn't work for me, though
The correct was to handle this in TypeScript is to cast the channel to the correct type, eg TextChannel or NewsChannel on which bulkDelete does exist.
@monbrey that’s a hack. The types should be updated to ensure when we do the check listed above it resolves correct. Type guarding and such should work.
@monbrey that’s a hack. The types should be updated to ensure when we do the check listed above it resolves correct. Type guarding and such should work.
The entire typing file is "a hack" given that the code is written in JavaScript. We've tried before to resolve the issue of Channel typeguards but it causes issues in edge-cases.
When the library is eventually rewritten in TypeScript as the primary language, the issue will (should) be resolved.
Most helpful comment
The correct was to handle this in TypeScript is to cast the channel to the correct type, eg
TextChannelorNewsChannelon whichbulkDeletedoes exist.