Mori
Mori

Reputation: 23

Why can't I ban/kick someone using Discord.py?

This is my first code

# IMPORT

import discord
from discord.ext import commands
import os

# INIT

client = commands.Bot(command_prefix = '.')

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

@client.command()
async def reload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')

        
client.run('My Token)

This is my second code

# IMPORT

import discord
from discord.ext import commands

# INIT

class Admin(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_member_join(self, member):
        print(f'{member} has joined a server')

    @commands.Cog.listener()
    async def on_member_remove(self, member):
        print(f'{member} has left a server')

    @commands.command(aliases=['cls'])
    @commands.has_permissions(kick_members=True)
    async def clear(self, ctx, amount=20):
        await ctx.channel.purge(limit=amount)
        await ctx.send("Refreshing channel")   

    @commands.command()
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, member : discord.Member, *, reason=None):
        await member.kick(reason=reason)
        await ctx.send(f'Banned {member.mention}')

    @commands.command()
    @commands.has_permissions()
    async def ban(self, ctx, member : discord.Member, *, reason=None):
        await member.ban(reason=reason)
        await ctx.send(f'Banned {member.mention}')

def setup(client):
    client.add_cog(Admin(client))

So, I already run the first code, and then I tried to use one of my alt account to test, but somehow the code doesn't work.....

I tried running .ban, but it didn't do anything, I also already check the console/cmd, but I didn't see any error...

Python ver: 3.8.3, Discord.py 1.4.1

Upvotes: 0

Views: 523

Answers (1)

Jawad
Jawad

Reputation: 2041

ctx comes after self, in your code you're passing ctx after your member parameter.

@commands.command()
@commands.has_permissions(ban_members=True)
async def ban(self, ctx, member: discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f'Banned {member.mention}')

And you also have a typo on this line:

await onmember.kick(reason=reason) #  Should just be member.kick

Upvotes: 1

Related Questions