Search code examples
pythondiscord.pybots

Python Discord.py Ban/Kick Command


i want to make a simple ban, unban and kick command but i couldn't figure it out.

i tried this: Discord.py ban command but it doesn't work.

i don't have any error. and this is how i use on discord <!kick @dcname>

my code:

@client.command
@has_permissions(kick_members=True)
async def kick(message, member:discord.Member, reason=None):
    await member.kick(reason=reason)
    await message.send(f"{member} sunucudan atıldı.")

Solution

  • Kick:

    @client.command()
    @has_permissions(kick_members=True)
    async def kick(ctx, member: discord.Member, *, reason=None):
        await member.kick(reason=reason)
        await ctx.send(f"{member} kicked from the server.")
    
    • @has_permissions(kick_members=True) decorator ensures that the user calling the command has the permission to kick members.
    • The ctx parameter represents the context of the command, such as the channel it was invoked in.
    • The "*" is used with the reason parameter to allow the user to provide a reason for the action being taken. This allows the user to provide a string of any length as the reason, which is then passed as an argument to the function.
    • The await member.kick(reason=reason) kicks the member from the server.

    Ban:

    @client.command()
    @has_permissions(ban_members=True)
    async def ban(ctx, member: discord.Member, *, reason=None):
        await member.ban(reason=reason)
        await ctx.send(f"{member} banned from the server.")
    
    • @has_permissions(ban_members=True) decorator ensures that the user calling the command has the permission to ban members.
    • The ctx parameter represents the context of the command, such as the channel it was invoked in.
    • The "*" is used with the reason parameter to allow the user to provide a reason for the action being taken. This allows the user to provide a string of any length as the reason, which is then passed as an argument to the function.
    • The await member.ban(reason=reason) bans the member from the server.

    Unban:

    @client.command()
    @has_permissions(ban_members=True)
    async def unban(ctx, member: discord.User, *, reason=None):
        await ctx.guild.unban(member, reason=reason)
        await ctx.send(f"{member} unbanned.")
    
    • @has_permissions(ban_members=True) decorator work the same way as in the ban command.
    • The ctx parameter represents the context of the command, such as the channel it was invoked in.
    • The "*" is used with the member parameter to allow the user to provide the user ID or name, followed by an optional reason.
    • The await ctx.guild.unban(member, reason=reason) unbans the member from the server.

    Don't forget to translate messages to your satisfaction.