Skip to content

Commit

Permalink
WIP: Convert modules.countdown.admin to use adminui.Menu (see #63).
Browse files Browse the repository at this point in the history
This includes a workaround for #65.
  • Loading branch information
nmlorg committed Jul 15, 2019
1 parent 37e5367 commit dec1779
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
88 changes: 41 additions & 47 deletions metabot/modules/countdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import time

from metabot.util import adminui
from metabot.util import humanize


Expand All @@ -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)
Expand All @@ -48,51 +50,43 @@ def format_delta(seconds):
if seconds:
message.append(humanize.plural(seconds, 'second', '<b>%s</b> %s'))
if message:
return ', '.join(message)
return ', '.join(message) + suffix
return '<b>NOW</b>'


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 <code>%s</code> to <code>%s</code>.', command,
modconf[command], timestamp)
else:
msg.add('/%s is now counting down to <code>%s</code>.', 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 <code>%s</code>).', 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 <code>%s</code>!", timestamp)
timestamp = None

if not command:
msg.action = 'Choose a command'
msg.add(
"Type the name of a command to add (like <code>days</code>\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 <code>%s</code>!", 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 <code>days</code>\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')
14 changes: 8 additions & 6 deletions metabot/modules/test_countdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name
[chat_id=1000 disable_web_page_preview=True parse_mode=HTML]
Bot Admin › modulestestbot › countdown: <b>Choose a command</b>
/countdowntest is now counting down to <code>1534906800</code>.
Set <code>countdowntest</code> to <code>1534906800</code>.
Type the name of a command to add (like <code>days</code>—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]
"""

assert conversation.message('/admin modulestestbot countdown countdowntest 1000') == """\
[chat_id=1000 disable_web_page_preview=True parse_mode=HTML]
Bot Admin › modulestestbot › countdown: <b>Choose a command</b>
Changed /countdowntest from <code>1534906800</code> to <code>1000</code>.
Changed <code>countdowntest</code> from <code>1534906800</code> to <code>1000</code>.
Type the name of a command to add (like <code>days</code>—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]
"""

Expand All @@ -104,14 +104,16 @@ 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]
"""

assert conversation.message('remove') == """\
[chat_id=1000 disable_web_page_preview=True parse_mode=HTML]
Bot Admin › modulestestbot › countdown: <b>Choose a command</b>
Removed /countdowntest (which was counting down to <code>1000</code>).
Unset <code>countdowntest</code> (was <code>1000</code>).
Type the name of a command to add (like <code>days</code>—don't include a slash at the beginning!), or select an existing countdown to remove.
[Back | /admin modulestestbot]
Expand All @@ -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: <b>Choose a command</b>
/bogus is not currently counting down to anything.
<code>bogus</code> is already unset.
Type the name of a command to add (like <code>days</code>—don't include a slash at the beginning!), or select an existing countdown to remove.
[Back | /admin modulestestbot]
Expand Down

0 comments on commit dec1779

Please sign in to comment.