So I was pretty sure that this code worked correctly at one point, but recently it was brought to my attention that it sometimes works but not always (read: It will always purge my messages by n amount, but it might not purge another members).
Because I was teaching myself how to use multiple files on this project, the code is actually split between two files. I'm not sure if that has anything to do with the issue or not. Given the size of the two files, I've uploaded them both to pastebin.
The command code. The Function code.
It purges just fine when I try and purge n where n is any number within the acceptable range, but when I try and purge n by username I get an error message stating
DiscordAPIError: Invalid Form Body
limit: Value "" is not int.
(note that is two double quotes, the formatting is a little unclear)
I'm confused as to where it is getting a value that is not an int, as I'm running the amount through praseInt. I've snipped out the code segment that checks if a user is defined and posted it below as I believe it to be the problem:
if (user) { // If User is Provided
debug.run(`Filtering messages by ${user.username}`);
const filterBy = user ? user.id : client.user.id;
messages = messages.filter(m => m.author.id === filterBy).array().slice(0, amount);
}
Any assistance would be greatly appreciated. I'm more or less about to scrap the user feature if I can't figure it out.
Also I apologize for the formatting. I can never get the hang of this sites formatting.
Try getting rid of
.array().slice(0, amount)
.
The array of fetched messages doesn't need to be cut by the amount
variable if the message.channel.fetchMessages({ limit: amount }).then((messages) => {
line already limits it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With