为什么我不能禁止/踢打使用Discord.py的人?

时间:2020-09-23 18:14:17

标签: python discord.py

这是我的第一个代码

# 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)

这是我的第二个代码

# 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))

因此,我已经运行了第一个代码,然后尝试使用一个alt帐户进行测试,但是 代码不起作用.....

我尝试运行.ban,但是它什么也没做,我也已经检查过console / cmd,但是我没有看到任何错误...

Python版本:3.8.3, Discord.py 1.4.1

1 个答案:

答案 0 :(得分:1)

ctx紧随self之后,在您的代码中,您要在ctx参数之后传递member

@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}')

您在这行上也有错字:

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