猿问

禁止命令 discord.py 的问题(重写分支)

我一直在用 discord.py(重写分支)编写一个机器人程序,我想添加一个禁止命令。机器人仍然没有禁止该成员,它只是显示一个错误:


@client.command(aliases=['Ban'])

async def ban(ctx,member: discord.Member, days: int = 1):

    if "548841535223889923" in (ctx.author.roles):

        await client.ban(member, days)

        await ctx.send("Banned".format(ctx.author))

    else:

        await ctx.send("You don't have permission to use this command.".format(ctx.author))

        await ctx.send(ctx.author.roles)      

它会禁止被 ping 的用户并确认它确实禁止了


慕村225694
浏览 206回答 2
2回答

Qyouu

Member.roles是一个Role对象列表,而不是字符串。您可以使用discord.utils.getid(作为 int)来搜索该列表。from discord.utils import get@client.command(aliases=['Ban'])async def ban(ctx, member: discord.Member, days: int = 1):    if get(ctx.author.roles, id=548841535223889923):        await member.ban(delete_message_days=days)        await ctx.send("Banned {}".format(ctx.author))    else:        await ctx.send("{}, you don't have permission to use this command.".format(ctx.author))        await ctx.send(ctx.author.roles)也不再有Client.ban协程,附加参数Member.ban必须作为关键字参数传递。

慕工程0101907

async def ban(ctx, member: discord.Member=None, *, reason=None):  if reason:    await member.send(f'You got banned from {ctx.guild.name} by {ctx.author}, reason: ```{reason}```')        await member.ban()        await ctx.send(f'{member.mention} got banned by {ctx.author.mention} with reason: ```{reason}```')  if reason is None:     await ctx.send("please specify a reason")
随时随地看视频慕课网APP

相关分类

Python
我要回答