From b8e368b94e313ff364a73cfb97ba86091cb25f35 Mon Sep 17 00:00:00 2001 From: int3l Date: Sun, 24 Nov 2019 01:17:47 +0200 Subject: [PATCH] Nicer pep number matching and extraction Covers the following cases: !pep 1234 test !pep 1234test !pep1234 test !pep1245test !pep test !pep --- bot.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/bot.py b/bot.py index 2527d8f..6e77729 100644 --- a/bot.py +++ b/bot.py @@ -295,18 +295,12 @@ def cmd_uptime(match: Match[str]) -> Response: return UptimeResponse() -@handle_message('!pep') +@handle_message('!pep[ ]?(?P\d{1,4})') def cmd_pep(match: Match[str]) -> Response: - *_, msg = match.groups() - *_, rest = msg.partition(' ') - try: - pep = str(int(rest)).zfill(4) - except ValueError: - return MessageResponse(match, 'Please make sure you gave me a number!') - else: - return MessageResponse( - match, f'https://www.python.org/dev/peps/pep-{pep}/', - ) + *_, number = match.groups() + return MessageResponse( + match, f'https://www.python.org/dev/peps/pep-{number.zfill(4)}/', + ) COMMAND_RE = re.compile(r'!\w+')