Home > other >  Cannot read properties of undefined (reading 'fetchBans')
Cannot read properties of undefined (reading 'fetchBans')

Time:09-23

Im trying to make a command with in my Discord bot to make unban a person. When I finnished the code I got an error. The error is Cannot read properties of undefined (reading 'fetchBans'). The code I use to make it is

message.guild.cache.fetchBans().then(async bans => {
            if (bans.size === 0) return message.channel.send("No one is banned on the server!")
            let bannedUser = bans.find(ban => ban.user.id == userID)
            if (!bannedUser) return message.channel.send("This user isn't banned!")
            await message.guild.members.unban(bannedUser.user, reason).catch(err => {
                return message.channel.send("Something went wrong!")
            }).then(() => {
                let embed = new MessageEmbed()
                    .setColor("GREEN")
                    .setTitle(`Member unbanned!`)
                    .setDescription("")
                    .setTimestamp()
                    .setFooter(this.client.user.username)

            embed.addField(`member = ${target.username}`, `reason = ${reason}`, false)


            message.channel.send({embeds: [embed]});

            if (row[0].logs_channel != null) this.client.channels.cache.get(row[0].logs_channel).send({embeds: [embed]})

            message.delete()
        })

The line where it goes wrong is this line:

 message.guild.cache.fetchBans().then(async bans => {

When I run this command it will give the error. I also tried to do something else. The code i tried else for that line is:

message.guild.fetchBans().then(async bans => {

This line also gave an error that says: TypeError: message.guild.fetchBans is not a function.

I don't know what i can do about this. I hope somebody can help me.

Node.js version: 16.9.1 discord.js version: 13.1.0

CodePudding user response:

According to the documentation there is no fetchBans on Guild. Maybe you're looking for message.guild.bans.fetch().

It's also worth noting that message.guild is nullable - if the message was a direct message (not sent in a guild) it will be null.

  • Related