Ban Command Not Working

I'm trying to create a ban command for my bot that primarily works via the user's ID number, but I keep getting an error when I try testing it. The code I am using is here: https://pastebin.com/p5BPttKn The error I get is: TypeError: userid.ban is not a function Is it even possible to do this, or have I missed something (blatantly obvious, given my track record).
Pastebin
Ban Command - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
16 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Jaworek
Jaworek3y ago
string.ban is not a function use guild.bans.create(id) ^ also
GeodesicDragon
GeodesicDragonOP3y ago
And how would I attach the reason to that? I tried guild.bans.create(id) and got an error: ReferenceError: guild is not defined
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
GeodesicDragon
GeodesicDragonOP3y ago
Tweaked a couple of things; the ban is now being applied, but the message explaining why the user was banned isn't being sent to the channel I want. TypeError: Cannot read properties of undefined (reading 'cache') Code has been updated on Pastebin.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
GeodesicDragon
GeodesicDragonOP3y ago
Okay, that's working now. All that remains is to attach a reason to the ban (because I hate seeing 'no reason provided' in my ban list.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
GeodesicDragon
GeodesicDragonOP3y ago
So interaction.guild.bans.create(userid){reason: banreason}?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
GeodesicDragon
GeodesicDragonOP3y ago
All right, final question (I hope). How do I get the username from the ID number? I tried interaction.guild.members.cache.get(userid) but that returns 'undefined.'
Jarco
Jarco3y ago
The member won't always be in the cache try fetching it instead if you just need the username I'd be easiest to just fetch the "user" not the "member"
d.js docs
d.js docs3y ago
method User#fetch() Fetches this user.
Jarco
Jarco3y ago
nvm this one i picked the wrong one you want this one instead
Jarco
Jarco3y ago
Discord.js
Discord.js is a powerful node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
GeodesicDragon
GeodesicDragonOP3y ago
const interactionUser = await interaction.guild.members.fetch(userid)
let userName = interactionUser.username
const interactionUser = await interaction.guild.members.fetch(userid)
let userName = interactionUser.username
Still returning 'undefined.' Never mind, I got it working. Just had to replace interactionUser.username with interactionUser.user.username. Everything appears to be working as I'd hoped. Thanks for the assistance. 👍
Want results from more Discord servers?
Add your server