From dec1779a3b47463fbb8f33c5d284f15cdbbd265b Mon Sep 17 00:00:00 2001 From: Daniel Reed Date: Mon, 15 Jul 2019 15:41:06 -0700 Subject: [PATCH] WIP: Convert modules.countdown.admin to use adminui.Menu (see #63). This includes a workaround for #65. --- metabot/modules/countdown.py | 88 ++++++++++++++----------------- metabot/modules/test_countdown.py | 14 ++--- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/metabot/modules/countdown.py b/metabot/modules/countdown.py index f7dcfc1..f717a87 100644 --- a/metabot/modules/countdown.py +++ b/metabot/modules/countdown.py @@ -2,6 +2,7 @@ import time +from metabot.util import adminui from metabot.util import humanize @@ -22,16 +23,17 @@ def moddispatch(ctx, msg, modconf): # pylint: disable=missing-docstring def countdown(msg, timestamp): # pylint: disable=missing-docstring - now = time.time() - if now > timestamp: - msg.add(format_delta(now - timestamp) + ' ago') - else: - msg.add(format_delta(timestamp - now)) + msg.add(format_delta(timestamp - time.time())) def format_delta(seconds): """Format a number of seconds into "5 days, 1 hour, 13.4 seconds", etc.""" + if seconds < 0: + suffix = ' ago' + seconds = -seconds + else: + suffix = '' days, seconds = divmod(seconds, 60 * 60 * 24) hours, seconds = divmod(seconds, 60 * 60) minutes, seconds = divmod(seconds, 60) @@ -48,51 +50,43 @@ def format_delta(seconds): if seconds: message.append(humanize.plural(seconds, 'second', '%s %s')) if message: - return ', '.join(message) + return ', '.join(message) + suffix return 'NOW' -def admin(unused_ctx, msg, frame): +def admin(ctx, msg, frame): """Handle /admin BOTNAME countdown.""" - modconf = frame.value - command, _, timestamp = frame.text.partition(' ') - command = command.lower() - - if command and timestamp: - if timestamp.isdigit(): - timestamp = int(timestamp) - if command in modconf: - msg.add('Changed /%s from %s to %s.', command, - modconf[command], timestamp) - else: - msg.add('/%s is now counting down to %s.', command, timestamp) - modconf[command] = timestamp - command = timestamp = None - elif timestamp == 'remove': - if command not in modconf: - msg.add('/%s is not currently counting down to anything.', command) - else: - msg.add('Removed /%s (which was counting down to %s).', command, - modconf[command]) - modconf.pop(command) - command = timestamp = None + menu = adminui.Menu() + for command in sorted(frame.value): + menu.add(command) + newframe, handler = menu.select(ctx, msg, frame, create=True) + if handler: + if newframe.text.isdigit(): + adminui.set_log(msg, newframe, int(newframe.text)) + elif newframe.text.lower() in ('-', 'none', 'off', 'remove'): + adminui.set_log(msg, newframe, None) else: - msg.add("I'm not sure how to count down to %s!", timestamp) - timestamp = None - - if not command: - msg.action = 'Choose a command' - msg.add( - "Type the name of a command to add (like days\u2014don't include a slash " - 'at the beginning!), or select an existing countdown to remove.') - for command, timestamp in sorted(modconf.items()): - msg.button('/%s (%s)' % (command, timestamp), '%s remove' % command) - return - - msg.path(command) - msg.action = 'Type the time for /' + command - msg.add('This is a little technical (it will be made simpler in the future), but type the unix ' - 'timestamp to count down to.') - msg.add('(Go to https://www.epochconverter.com/, fill out the section "Human date to ' - 'Timestamp", then use the number listed next to "Epoch timestamp".)') + if newframe.text: + msg.add("I'm not sure how to count down to %s!", newframe.text) + + msg.path(newframe.field) + msg.action = 'Type the time for /' + newframe.field + msg.add('This is a little technical (it will be made simpler in the future), but type ' + 'the unix timestamp to count down to.') + msg.add('(Go to https://www.epochconverter.com/, fill out the section "Human date to ' + 'Timestamp", then use the number listed next to "Epoch timestamp".)') + if newframe.value: + msg.add('To remove /%s (which is counting to %s), type "off".', newframe.field, + newframe.value) + return + + msg.action = 'Choose a command' + msg.add( + "Type the name of a command to add (like days\u2014don't include a slash " + 'at the beginning!), or select an existing countdown to remove.') + # See https://github.com/nmlorg/metabot/issues/65. + menu = adminui.Menu() + for command in sorted(frame.value): + menu.add(command) + menu.display(ctx, msg, frame, 'command') diff --git a/metabot/modules/test_countdown.py b/metabot/modules/test_countdown.py index 047e9c3..f93a844 100644 --- a/metabot/modules/test_countdown.py +++ b/metabot/modules/test_countdown.py @@ -77,10 +77,10 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name [chat_id=1000 disable_web_page_preview=True parse_mode=HTML] Bot Admin › modulestestbot › countdown: Choose a command -/countdowntest is now counting down to 1534906800. +Set countdowntest to 1534906800. Type the name of a command to add (like days—don't include a slash at the beginning!), or select an existing countdown to remove. -[/countdowntest (1534906800) | /admin modulestestbot countdown countdowntest remove] +[countdowntest (1534906800) | /admin modulestestbot countdown countdowntest] [Back | /admin modulestestbot] """ @@ -88,10 +88,10 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name [chat_id=1000 disable_web_page_preview=True parse_mode=HTML] Bot Admin › modulestestbot › countdown: Choose a command -Changed /countdowntest from 1534906800 to 1000. +Changed countdowntest from 1534906800 to 1000. Type the name of a command to add (like days—don't include a slash at the beginning!), or select an existing countdown to remove. -[/countdowntest (1000) | /admin modulestestbot countdown countdowntest remove] +[countdowntest (1000) | /admin modulestestbot countdown countdowntest] [Back | /admin modulestestbot] """ @@ -104,6 +104,8 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name This is a little technical (it will be made simpler in the future), but type the unix timestamp to count down to. (Go to https://www.epochconverter.com/, fill out the section "Human date to Timestamp", then use the number listed next to "Epoch timestamp".) + +To remove /countdowntest (which is counting to 1000), type "off". [Back | /admin modulestestbot countdown] """ @@ -111,7 +113,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name [chat_id=1000 disable_web_page_preview=True parse_mode=HTML] Bot Admin › modulestestbot › countdown: Choose a command -Removed /countdowntest (which was counting down to 1000). +Unset countdowntest (was 1000). Type the name of a command to add (like days—don't include a slash at the beginning!), or select an existing countdown to remove. [Back | /admin modulestestbot] @@ -121,7 +123,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name [chat_id=1000 disable_web_page_preview=True parse_mode=HTML] Bot Admin › modulestestbot › countdown: Choose a command -/bogus is not currently counting down to anything. +bogus is already unset. Type the name of a command to add (like days—don't include a slash at the beginning!), or select an existing countdown to remove. [Back | /admin modulestestbot]