close

[Solved] Discord.py ban command

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Discord.py ban command in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Discord.py ban command Error Occurs?

Today I get the following error Discord.py ban command in python.

How To Solve Discord.py ban command Error ?

  1. How To Solve Discord.py ban command Error ?

    To Solve Discord.py ban command Error My ban command i got for my bot is , obviously dont keep the comment out for the ban part, I just put that there when i didn't know how to lock it to roles

  2. Discord.py ban command

    To Solve Discord.py ban command Error My ban command i got for my bot is , obviously dont keep the comment out for the ban part, I just put that there when i didn't know how to lock it to roles

Solution 1

My ban command i got for my bot is , obviously dont keep the comment out for the ban part, I just put that there when i didn’t know how to lock it to roles

#bans a user with a reason
@client.command()
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    # await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")

Solution 2

Try this:

import discord #Imports the discord module.
from discord.ext import commands #Imports discord extensions.

#The below code verifies the "client".
client = commands.Bot(command_prefix='pb?')
#The below code stores the token.
token = "Your token"

'''
The below code displays if you have any errors publicly. This is useful if you don't want to display them in your output shell.
'''
@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('Please pass in all requirements :rolling_eyes:.')
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You dont have all the requirements :angry:")

#The below code bans player.
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason = None):
    await member.ban(reason = reason)

#The below code unbans player.
@client.command()
@commands.has_permissions(administrator = True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
            return

#The below code runs the bot.
client.run(token)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read