discord.py:如何返回命令?

时间:2020-06-06 13:29:15

标签: python discord.py

我有一个命令,并且该命令中的bot等待消息。在这种情况下,消息内容为“否”或“是”。但是,如果用户编写了其他内容,则漫游器会发送一条消息,表明该消息不起作用,并且用户必须再次尝试。因此,在这种情况下,机器人应再次检查用户是否发送“否”或“是”。但是我该怎么办呢?

我的代码:

@client.command()
async def setup(ctx, choice = None):
    if choice == 'welcome':
        def check4(messagetitletags):
            return messagetitletags.channel.id == ctx.message.channel.id and messagetitletags.author == ctx.message.author and not messagetitletags.author == ctx.message.author.bot

        messagetitletagscheck = await client.wait_for('message', check=check4, timeout=None)

        if messagetitletagscheck.content in ['Yes', 'y', 'Y', 'yes']:
            with open(r'./welcome.json', 'r') as f:
                welcomemessage = json.load(f)
            if f'{ctx.guild.id}' in welcomemessage.keys():
                try:
                    welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
                    welcomemessage[f'{ctx.guild.id}'][f"TitleTags"] = f"Yes"
                except KeyError:
                    welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
                    welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = f"Yes"
            else:
                welcomemessage[f"{ctx.guild.id}"] = {}
                welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = [f"Yes"]
            with open(r'./welcome.json', 'w+')as f:
                json.dump(welcomemessage, f, sort_keys=True, indent=4)

        elif messagetitletagscheck.content in ['No', 'n', 'N', 'no']:
            with open(r'./welcome.json', 'r') as f:
                welcomemessage = json.load(f)
            if f'{ctx.guild.id}' in welcomemessage.keys():
                try:
                    welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
                    welcomemessage[f'{ctx.guild.id}'][f"TitleTags"] = f"No"
                except KeyError:
                    welcomemessage[f"{ctx.guild.id}"][f'TitleTags'] = {}
                    welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = f"No"
            else:
                welcomemessage[f"{ctx.guild.id}"] = {}
                welcomemessage[f"{ctx.guild.id}"][f"TitleTags"] = [f"No"]
            with open(r'./welcome.json', 'w+')as f:
                json.dump(welcomemessage, f, sort_keys=True, indent=4)

        else:
            await ctx.send('<:error:713187214586282054> Invalid input. Please try again.')
            return check4

1 个答案:

答案 0 :(得分:1)

您可以创建一个while循环,直到结果成功为止。

例如,如果您要等待有效的是/否答案,则可以在正确的输出中使该语句为假。

示例:

def your_code():
    if output == "right":
        # some code
        return True
    elif output == "wrong":
        # some code
        return True
    else:
        # some code
        return False

answer_is_valid = False

while not answer_is_valid: # Loops till answer_is_valid is True
    answer_is_valid = your_code() # This puts the return value in answer_is_valid.
                                  #If the return value was False it will loop again.

如果用户不断返回错误输入,我不建议它永远循环。因此,我会在函数中进行另一次检查,以在x尝试后停止该函数。 看起来可能像这样:

def your_code():
    if output == "right":
        # some code
        return True
    elif output == "wrong":
        # some code
        return True
    else:
        # some code
        return False

answer_is_valid = False
incorrect_tries = 0
max_incorrect_tries = 5

# the loops exits if the answer is valid. Or max incorrect tries has been reached    
while not answer_is_valid and incorrect_tries <= max_incorrect_tries:
    answer_is_valid = your_code() # This repeats the function until the function returns True
    incorrect_tries += 1

您可以使用函数替换your_code()函数,如果输出正确/不正确,请确保返回True / False。

要实现它:

@client.command()
async def setup(ctx, choice = None):
    if choice == "welcome":
         # some code

应转换为:

def welcome():
    # some code that returns True when the input was valid, False if not

@client.command()
async def setup(ctx, choice = None):
    if choice == "welcome":
        # the new code with while loop explained earlier using your welcome function