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

时间:2019-04-10 12:23:53

标签: python python-3.x discord discord.py

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

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

它将禁止被查验的用户并确认它确实禁止了

2 个答案:

答案 0 :(得分:0)

Member.rolesRole对象的列表,而不是字符串。您可以使用discord.utils.get使用ID(作为整数)在该列表中进行搜索。

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

答案 1 :(得分:0)

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