首页 > 解决方案 > Discord.py v3(重写)尝试将用户输入与预设元组列表匹配

问题描述

为我所在的一些 RP 服务器处理不和谐的字符表机器人。我正在添加一个区域以在收集字符信息后编辑字符信息。我有一个为信息收集的初始循环预定义的元组列表,但对于编辑功能,我试图让用户输入与这些预定义元组之一匹配。我不知道......我究竟需要转向哪里?就像,我可以看到我正在尝试做的事情的逻辑,但我在 python 方面的经验水平几乎为零。

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

@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)指我的信息收集循环,如有必要,我可以发布

因为我现在有代码,它的功能正常,这更像是一种寻求帮助,让它做我真正想要它做的事情,xx根据给定菜单,然后将该输入值与预定义的元组列表中的正确元组匹配。

标签: pythontuplesdiscorddiscord.pydiscord.py-rewrite

解决方案


您可以考虑使用 adictionary来存储您的用户响应xaskeys并将您的元组存储为values您希望在用户响应与字典键匹配后执行的操作。

如何在 Python 中使用字典是一个很好的起点


推荐阅读