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

2024-04-29 21:39:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一个代码

# 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,但它没有做任何事情,我也已经检查了控制台/cmd,但我没有看到任何错误

Python版本:3.8.3, Discord.py 1.4.1


Tags: importselfclientasyncdefextensionawaitcommand
1条回答
网友
1楼 · 发布于 2024-04-29 21:39:08

ctx位于self之后,在代码中,您在member参数之后传递ctx

@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

相关问题 更多 >