Couldn't resolve the user id to unban

I made a ban and unban commands for my bot. Even though my ban command works without any problems, I am having problems with my unban command. Even if the person is banned, when I use /unban, it says "This user is not banned" and it does not unban the person. Also, an error message appears on the console. Note: My bot has BanMembers and Administrator perm
No description
No description
No description
No description
14 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
duck
duck9mo ago
before addressing the actual error, is there a reason you're not using a user option?
kerem
keremOP9mo ago
the person is not on the server, so we cannot find that person on userOption. Am I wrong?
duck
duck9mo ago
it won't have them in the autocomplete, but it can still accept a user that's not in the server which is to say, discord will still validate the id for you
kerem
keremOP9mo ago
lemme try If I get an error again i will write back
kerem
keremOP9mo ago
There isn't any error
No description
No description
duck
duck9mo ago
that error message discord is giving seems to indicate id is an integer option please ensure that you've deployed commands since changing them
kerem
keremOP9mo ago
i already deployed commands
duck
duck9mo ago
since changing the option to a user option?
kerem
keremOP9mo ago
Oh wait I was able to write the ID but the application did not respond no error message I wrote the command again and now it says "the user is not banned" and it did not unban no error msg again
duck
duck9mo ago
then to address that, that'd be because <CommandInteractionOptionResolver>.getUser() returns a User object, and <User>.id is a string a string will never be equal to an entire User object you can entirely skip fetching bans and finding an existing ban since attempting to unban them is already an api call that will tell you whether they're banned (by rejecting if they're not banned)
kerem
keremOP9mo ago
honestly it wouldn't be a lie if i said i was confused. can you show me what i need to change?
const banList = await interaction.guild.bans.fetch();
const bannedUser = banList.find((user) => user.user.id === userId);

if (!bannedUser) {
return interaction.reply("This user is not banned.");
}
await interaction.guild.members.unban(userId);
const embed = new EmbedBuilder()
.setColor("Aqua")
.setTitle("User Unbanned")
.setDescription(`Successfully unbanned <@${userId}>`);
await interaction.reply({ embeds: [embed] });
const banList = await interaction.guild.bans.fetch();
const bannedUser = banList.find((user) => user.user.id === userId);

if (!bannedUser) {
return interaction.reply("This user is not banned.");
}
await interaction.guild.members.unban(userId);
const embed = new EmbedBuilder()
.setColor("Aqua")
.setTitle("User Unbanned")
.setDescription(`Successfully unbanned <@${userId}>`);
await interaction.reply({ embeds: [embed] });
duck
duck9mo ago
you can entirely skip fetching bans and finding an existing ban
technically you don't need to change the name of the variable userId, but it is a misleading name
kerem
keremOP9mo ago
thanks for help i fixed have a great day
Want results from more Discord servers?
Add your server