helpers.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:kitsuchan-2 作者: n303p4 项目源码 文件源码
def input_number(ctx: commands.Context,
                       message: str="Please enter a number within 10 seconds.",
                       *, timeout: int=10, min_value: int=None, max_value: int=None):
    """Input number helper. Ask a confirmation message with a timeout of 10 seconds.

    ctx - The context in which the question is being asked.
    message - Optional messsage that the question should ask.
    timeout - Timeout, in seconds, before automatically failing.
    min_value - Minimum accepted value for the input.
    max_value - Maximum accepted value for the input.
    """
    await ctx.send(message)

    def check(message):
        """A checking function dynamically to verify input."""
        if message.author != ctx.message.author or not message.clean_content.isdecimal():
            return False

        number = int(message.clean_content)

        if (min_value and number < min_value) or (max_value and number > max_value):
            return False

        return True

    try:
        message = await ctx.bot.wait_for("message", timeout=timeout, check=check)

    except asyncio.TimeoutError:
        raise commands.UserInputError("Timed out waiting.")

    return int(message.clean_content)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号