Discord.py v3(重写)尝试使用户输入与预设元组列表匹配

时间:2018-12-24 21:32:04

标签: python tuples discord discord.py discord.py-rewrite

正在为我所处的某些RP服务器开发一个字符表bot,以解决不一致的问题。在收集字符信息之后,我正在添加一个区域来编辑字符信息。我有一个为信息收集的初始循环预定义的元组列表,但是对于编辑功能,我试图让用户输入以与这些预定义元组之一匹配。我不知道...我到底需要转向哪里?就像,我可以看到我要执行的操作的逻辑,但是我在python中的经验水平接近于nil。

在当前时间点,我只是运行一系列的if / then语句,但是我想让代码循环遍历我拥有的信息收集代码,直到用户告诉bot他们编辑完毕,现在每次要编辑该机器人时,都必须触发该机器人。

@commands.command()
async def edit(self, ctx):
def check(message):
    return message.author == ctx.message.author
member = ctx.author
n = await self.config.member(member).name()
await ctx.send("Accessing Character Data for {}. . .".format(n))
await asyncio.sleep(1)
await ctx.send("1: Name \n 2: Race \n 3: Gender \n 4: Age \n 5: World of Origin \n 6: Profession/Class/Occupation \n 7: Eyes \n 8: Hair \n 9: Height \n 10: Weight \n 11: Body Mods \n 12: Description \n 13: Primary Weapon \n 14: Secondary Weapon \n 15: Magic/Tech Skill 1 \n 16: Magic/Tech Skill 2 \n 17:Magic/Tech Skill 3 \n 18: Magic/Tech Skill 4 \n 19: Magic/Tech Skill 5 \n 20: Equipped Weapon(s) \n 21: Equipped Armor \n 22: Combat Role")
i = 0
while i == 0:
    await ctx.send("Which trait would you like to edit? Please only respond with the numerical value for the selected trait.")
    try:
        input = await self.bot.wait_for('message', check=check, timeout=60)
    except asyncio.TimeoutError:
        return
    input = input.content
    ii = 0
    while ii == 0:
        if input == "1":
            info_type = ("name", "Name")
            await self.add_char_info(ctx, info_type, member)
            await ctx.send("Would you like to edit another trait?")
            try:
                msg = await self.bot.wait_for('message', check=check, timeout=60)
            except asynico.TimeoutError:
                await ctx.send("You took too long to respond. Terminating.")
                i = 1
                ii = 1
                return
            msg = msg.content.lower()
            if msg == "yes":
                ii = 0
            elif msg == "no":
                i = 1
                ii = 1
                await ctx.send("Closing Character Editor")
            else:
                await ctx.send("Please enter yes or no.")

add_char_info(ctx, info_type, member)是指我的信息收集循环,如有必要,我可以发布该循环

正如我现在拥有的代码一样,它可以正常运行,这更多是在寻求帮助,以使它完成我实际想要做的事情,这是将用户回复x转换为{ {1}}是给定菜单中的1至22的数字,然后将该输入值与预定义的元组列表中的适当元组匹配。

1 个答案:

答案 0 :(得分:0)

您可以考虑使用dictionary将用户对x的响应存储为keys,并将元组存储为values,您希望对其执行一次操作用户响应与字典键匹配。

How to use dictionaries in Python是一个很好的起点