简体   繁体   English

discord.py中的bot命令重新写入,如何基于角色包含AND角色不包含的条件分支?

[英]bot command in discord.py REWRITE, how to conditional branch based on role contains AND role doesnt contain?

I want to add some if statements to the " ready " command, something like: "if user has any of roles A,B,C" then do stuff. 我想在“ ready ”命令中添加一些if语句,例如:“如果用户具有角色A,B,C中的任何一个”,则执行操作。 Else if user has any of roles D,E,F then do other stuff. 否则,如果用户具有D,E,F角色中的任何一个,则执行其他操作。 I think if someone can help me resolve the stack trace error below then it will likely solve the Question below the code 我认为,如果有人可以帮助我解决以下的堆栈跟踪错误,则可能会解决代码下方的问题

import logging
import time
import discord
import asyncio
import time
from discord.ext import commands
from discord.ext.commands import MissingRequiredArgument

prefix = "!"
bot = commands.Bot(command_prefix=prefix, case_insensitive=True)

token = open("token.txt", "r").read()

class MemberRoles(commands.MemberConverter):
        async def convert(self, ctx, argument):
            member = await super().convert(ctx, argument)
            return [role.name for role in member.roles[1:]] # Remove everyone role!

@bot.command()
#i prefer not using @commands.has_any_role decorator because i want my 1 "ready" command to branch based on the role(s) the user has
#I am trying to make my bot command able to do multiple role checks
#I am trying to adapt this example "async def roles(ctx, *, member: MemberRoles):" from https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html


async def ready(ctx, *, message: str, member: MemberRoles):
    '''
    !ready must include text after the command and can only be used if you are assigned ANY of these roles: Admin, Newbie
    '''

    if message is None:
        return
    else:
        try:
          #stuff
        except Exception as e:
            print(str(e))

#This part, about detecting which roles a member has does not work, see the question below the code for more information
    await ctx.send('I see the following roles: ' + ', '.join(member))


@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.channel.send("Cannot ready without a message.  Type !ready <your message> and try again.")
    else:
        raise error

bot.run(token)

I think if someone can help me resolve this stack trace error then i can address the "question" below. 我认为,如果有人可以帮助我解决此堆栈跟踪错误,那么我可以解决以下“问题”。 The stack trace error complains at the " raise error " point. 堆栈跟踪错误在“ raise error ”点抱怨。 The error i see is "discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: ready() missing 1 required keyword-only argument: 'member'" 我看到的错误是“ discord.ext.commands.errors.CommandInvokeError:命令引发了异常:TypeError:ready()缺少1个仅关键字必需参数:'member'”

Question: Assuming that the "MemberRoles" class is a good way to do this, how do I use it within my "ready" command to achieve the A,B,C and D,E,F IF Else branching that I require? 问题:假设“ MemberRoles”类是执行此操作的好方法,如果我需要其他分支,如何在“ ready”命令中使用它来实现A,B,C和D,E,F?

Thanks for the help! 谢谢您的帮助!

You can only have one keyword only argument in a command, because uses keyword only arguments to collect the end of a message . 在命令中只能有一个仅关键字参数,因为使用仅关键字参数来收集消息的结尾 You actually just want the authors roles, so you don't need to use a converter at all: 实际上,您只需要作者角色,因此根本不需要使用转换器:

@bot.command
async def ready(ctx, *, message: str):
    author_roles = ctx.author.roles
    ...

As to checking roles, you can do something like 至于检查角色,您可以执行以下操作

roles_one = {"A", "B", "B"}
roles_two = {"D", "E", "F"}
if any(role.name in roles_one for role in author_roles):
    ...
elif not any(role.name in roles_two for role in author_roles):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM