I made a purge/clear command for my discord bot, but when I do /purge amount: (amount), it works fine, but the bot replies with 🗑️ (amount) messages were deleted from undefined.
, which should be 🗑️ (amount) messages were deleted from #moderator-only.
. Is this because I don't have the correct intents or have I done something wrong? I have currently only the SERVER MEMBERS
intent.
Code:
const { CommandInteraction, MessageEmbed } = require('discord.js');
module.exports = {
name: "purge",
description: "Deletes a specific amount of messages from a target.",
permissions: "MANAGE_MESSAGES",
options: [
{
name: "amount",
description: "Select the amount of messages to remove from the target.",
type: "NUMBER",
required: true
},
{
name: "target",
description: "Select the target to remove messages from.",
type: "USER",
required: false
}
],
/**
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
const { channel, options } = interaction;
const Amount = options.getNumber("amount");
const Target = options.getMember("target");
const Messages = await channel.messages.fetch();
const Response = new MessageEmbed()
.setColor('#171717');
if(Target) {
let i = 0;
const filtered = [];
(await Messages).filter((m) => {
if(m.author.id === Target.id && Amount > i) {
filtered.push(m);
i++;
}
})
await channel.bulkDelete(filtered, true).then(messages => {
Response.setDescription(`🗑️ **${messages.size}** messages were deleted from ${Target}.`);
interaction.reply({embeds: [Response]});
})
} else {
await channel.bulkDelete(Amount, true).then(messages =>{
Response.setDescription(`🗑️ **${messages.size}** messages were deleted from ${channel.mention}.`);
interaction.reply({embeds: [Response]});
})
}
}
}
channel.mention
is not a thing in discord.js..setDescription(`🗑️ **${messages.size}** messages were deleted from ${channel}.`);
OR
.setDescription(`🗑️ **${messages.size}** messages were deleted from <#${channel.id}>.`);