【问题标题】:Why don't nested commands work for discord.py?为什么嵌套命令不适用于 discord.py?
【发布时间】:2021-01-31 19:59:03
【问题描述】:

我正在尝试为不和谐制作一个投票机器人,以使该人静音。但是,当我运行 Yes 和 No 函数时,出现 Command not found 错误。到目前为止,这就是我所拥有的:

@bot.command(name='votekick')
@commands.has_role('Player')
async def votekick(ctx, user: discord.Member):
    await ctx.send('A votekick against {} has started. Type .yes to vote to mute this person and .no if you don\'t want this person to be muted.'.format(user.name))

    server = ctx.message.guild
    role_id = 805035493573394452

    for member in server.members:
        if role_id in member.roles:
            member.add_roles(get(member.guild.roles, name='able'))

    arole = get(member.guild.roles, name='able')
    yes=0
    no=0
    while len(arole.members) > 0:
        @bot.command(name='yes')
        @commands.has_role('able')
        async def voteyes(ctx):
            author=ctx.message.author
            message=ctx.message
            await bot.delete_message(message)
            await ctx.send('Player {} has voted.'.format(author.display_name))
            author.remove_roles(get(author.guild.roles, name='able'))
            global yes
            yes=yes+1

        @bot.command(name='no')
        @commands.has_role('able')
        async def voteno(ctx):
            author = ctx.message.author
            message = ctx.message
            await bot.delete_message(message)
            await ctx.send('Player {} has voted.'.format(author.display_name))
            author.remove_roles(get(author.guild.roles, name='able'))
            global no
            no = no+1
    if yes > no:
        return None
    else:

        await user.edit(mute=True)

谁能告诉我有什么问题?提前致谢!

【问题讨论】:

  • 这是一个合理的假设,您可以将一个命令放在另一个命令中,以便一个接一个地发生,但不幸的是,它并不能完全按照这种方式工作。相反,您需要跟踪谁被提名被踢到一个“超过”votekick 函数调用的变量中,这样votenovoteyes(不会嵌套)就会知道谁去踢。不幸的是,这确实有点复杂,特别是如果多个人被提名同时被踢。

标签: python-3.x discord.py


【解决方案1】:

这里有些地方不对:

  1. 嵌套命令不能这样工作
  2. 您需要在每次投票后获得角色才能获得新成员,因为旧的成员将保持不变,不会自动更新
  3. 您可能想在此处使用wait_for,因为它比执行多个命令更容易
  4. 角色 ID 不会出现在 member.roles 中,因为它返回的是角色对象列表,而不是角色 ID
  5. Bot.delete_message() 不是一个东西,它的 Message.delete()
  6. 您想检查是否是 > 否然后静音此人,您正在做相反的事情

以下是修改后的代码:

@bot.command(name='votekick')
@commands.has_role('Player')
async def votekick(ctx, user: discord.Member):
    await ctx.send('A votekick against {} has started. Type .yes to vote to mute this person and .no if you don\'t want this person to be muted.'.format(user.name))
    server = ctx.message.guild
    role_id = 805035493573394452
    role = get(member.guild.roles, name='able')
    for i in server.members:
        if role_id in [z.id for z in i.roles]:
            await i.add_roles(role)
    
    role = get(member.guild.roles, name='able')
    yes, no = 0, 0
    while len(role.members) > 0:
        msg = await bot.wait_for("message", check=lambda m: m.guild.id == ctx.guild.id)
        if msg.content == ".yes":
            await msg.delete()
            role = get(member.guild.roles, name='able')
            await msg.author.remove_roles(role)
            await ctx.send(f'Player {msg.author.display_name} has voted.')
            yes += 1
        elif msg.content == ".no":
            await msg.delete()
            role = get(member.guild.roles, name='able')
            await msg.author.remove_roles(role)
            await ctx.send(f'Player {msg.author.display_name} has voted.')
            no += 1
    if yes > no:
        return await user.edit(mute=True)
    return

【讨论】:

  • 谢谢!但是,await ctx.send(f'{Player {msg.author.display_name} has voted.') 中有一个额外的大括号。永远不要少,非常感谢!
  • 很遗憾,它没有用,但仍然非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-08-11
  • 2020-05-07
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 2016-05-04
  • 2021-02-28
相关资源
最近更新 更多