从命令中检索参数并将其与列表中的项目匹配

时间:2019-01-26 17:30:21

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

基本上,我想做的是从用户那里收到一条消息,将参数与列表中的项目进行比较,然后使用参数为用户分配角色。我得到的是一堆if语句,这些语句导致其他可能的参数:

@bot.event
async def on_message(message):
    memberName = message.author
    roleParasoul = discord.utils.get(message.server.roles, name="Parasoul")
    roleValentine = discord.utils.get(message.server.roles, name="Valentine")
    roleBand = discord.utils.get(message.server.roles, name="Big Band")
    rolePeacock = discord.utils.get(message.server.roles, name="Peacock")
    roleRobo = discord.utils.get(message.server.roles, name="Robo Fortune")
    roleFortune = discord.utils.get(message.server.roles, name="Ms. Fortune")
    roleBeowulf = discord.utils.get(message.server.roles, name="Beowulf")
    roleCerebella = discord.utils.get(message.server.roles, name="Cerebella")
    roleFilia = discord.utils.get(message.server.roles, name="Filia")
    roleFukua = discord.utils.get(message.server.roles, name="Fukua")
    roleDouble = discord.utils.get(message.server.roles, name="Double")
    roleEliza = discord.utils.get(message.server.roles, name="Eliza")
    rolePainwheel = discord.utils.get(message.server.roles, name="Painwheel")
    roleFeelme = discord.utils.get(message.server.roles, name="Feelme")
    roleSkullgirls = discord.utils.get(message.server.roles, id="343122878595727360")

# check which command we wanted (and ignore any message that isn't a command)
    if message.content.startswith('!roles'):
        botCmd = LobbyBotCommand.ROLES
    else:
        return

    if botCmd == LobbyBotCommand.ROLES:
        tagReact = bot.add_reaction(message, ":SeemsGood:263784999038353420")
        if "Feelme" in message.content:
            await bot.add_roles(memberName, roleFeelme)
            await tagReact
            return
        elif "Skullgirls" in message.content:
            await bot.add_roles(memberName, roleSkullgirls)
            await tagReact
            return

人们已经告诉我对使用discord.py的命令使用正确的@bot.command,但是我想首先使这项工作生效。

这里的问题是我不知道如何正确地从消息中检索参数并将其与列表匹配。

我认为可行的看起来像这样:

@bot.event
async def on_message(message):
    roleCharacter = ("Parasoul", "Valentine", "Big Band", "Peacock", "Robo Fortune", "Ms. Fortune", "Beowulf", "Cerebella", "Filia", "Fukua", "Double", "Eliza", "Painwheel", "Feelme", "SG")
    if message.content.startswith('!roles'):
        if roleCharacter in message.content:
            await bot.add_roles(memberName, roleCharacter)
            return

这显然不起作用,因为我只能在if中使用字符串。现在怎么办?

1 个答案:

答案 0 :(得分:0)

  

人们已经告诉我对使用discord.py的命令使用正确的@ bot.command,但是我想首先进行这项工作。

好吧,如果您真的坚持的话,可以使用discord.utils.get并以字符串形式遍历角色名称列表

也许是

response.request.res.responseUrl