简体   繁体   中英

Retrieve argument from command and match it with an item in list

Basically, what I want to do is receive a message from a user, compare the argument with the item in a list then assign a role to the user using the argument. What I got is a bunch of if statements one leading to the other possible arguments:

@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

People have already told me use the proper @bot.command for commands using discord.py but I want to make this work first.

The problem here is I don't know how properly retrieve just the argument from the message and match it with the list.

What I thought would work, looked like this:

@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

That obviously doesn't work because I can only use strings in a if . What now?

People have already told me use the proper @bot.command for commands using discord.py but I want to make this work first.

Well if you're really adamant you can use discord.utils.get and loop through the list of your role names as strings

Maybe something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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