为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村225694 2022-01-05 20:21:57
我一直在用 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 的用户并确认它确实禁止了
查看完整描述

2 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

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必须作为关键字参数传递。


查看完整回答
反对 回复 2022-01-05
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

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


查看完整回答
反对 回复 2022-01-05
  • 2 回答
  • 0 关注
  • 205 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号