Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从命令中检索参数并将其与列表中的项匹配_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

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

Python 从命令中检索参数并将其与列表中的项匹配,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,基本上,我要做的是接收来自用户的消息,将参数与列表中的项目进行比较,然后使用参数为用户分配角色。我得到的是一组if语句,其中一个导致其他可能的参数: @bot.event 异步def on_消息(消息): memberName=message.author roleParasoul=discord.utils.get(message.server.roles,name=“Parasoul”) roleValentine=discord.utils.get(message.server.roles

基本上,我要做的是接收来自用户的消息,将参数与列表中的项目进行比较,然后使用参数为用户分配角色。我得到的是一组
if
语句,其中一个导致其他可能的参数:

@bot.event
异步def on_消息(消息):
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=“孔雀”)
roleRobo=discord.utils.get(message.server.roles,name=“Robo Fortune”)
roleFortune=discord.utils.get(message.server.roles,name=“Ms.Fortune”)
rolebowulf=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”)
#检查我们想要的命令(并忽略任何非命令的消息)
如果message.content.startswith(“!roles”):
botCmd=LobbyBotCommand.ROLES
其他:
返回
如果botCmd==LobbyBotCommand.ROLES:
tagReact=bot.add_reaction(消息“:SeemsGood:263784999038353420”)
如果message.content中有“Feelme”:
等待bot.add_角色(memberName,roleFeelme)
等待反应
返回
message.content中的elif“Skullgirls”:
等待机器人。添加_角色(成员名称、角色库女孩)
等待反应
返回
人们已经告诉我,对于使用discord.py的命令,请使用适当的
@bot.command
,但我想先让它起作用

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

我认为有效的方法是这样的:

@bot.event
异步def on_消息(消息):
roleCharacter=(“帕拉苏尔”、“瓦伦丁”、“大乐队”、“孔雀”、“机器人财富”、“财富女士”、“贝奥武夫”、“小脑”、“菲利亚”、“福卡”、“双人”、“伊丽莎”、“痛苦之轮”、“菲尔梅”、“SG”)
如果message.content.startswith(“!roles”):
如果message.content中有角色字符:
等待bot.add_角色(memberName、roleCharacter)
返回
这显然不起作用,因为我只能在
if
中使用字符串。现在怎么办

人们已经告诉我,对于使用discord.py的命令,请使用适当的@bot.command,但我想先让它起作用

好吧,如果你真的很坚定,你可以使用和循环你的角色名列表作为字符串

也许像

@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

我强烈建议您尽可能缩短代码,以重现问题。特别是在未来。拥有每一个
elif
并不是传达这一点的必要条件。一个简单的例子可以帮助我们,“显然不行,因为我只能在if中使用字符串”:这里没有什么明显的东西。您想不使用字符串吗?这种价值从何而来?
if
可以处理任何产生
bool
值的东西。我来试试。