Skip to content

Commit

Permalink
Move ctx and msg into Frame (see #63).
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlorg committed Aug 10, 2019
1 parent a9911df commit 725c362
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 66 deletions.
12 changes: 6 additions & 6 deletions metabot/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def moddispatch(ctx, msg, modconf): # pylint: disable=missing-docstring
def default(ctx, msg): # pylint: disable=missing-docstring
ctx.private = True
msg.path('/admin', 'Bot Admin')
frame = adminui.Frame(ctx.bot.multibot.conf, 'bots', None, ctx.text)
frame = adminui.Frame(ctx, msg, ctx.bot.multibot.conf, 'bots', None, ctx.text)
menu = adminui.Menu()
for username, botconf in sorted(frame.value.items()):
if ctx.user['id'] in botconf['issue37']['admin']['admins']:
Expand All @@ -41,7 +41,7 @@ def default(ctx, msg): # pylint: disable=missing-docstring
'\n'
'<pre>/admin %s admin add %s</pre>', ctx.bot.username, ctx.bot.username, ctx.user['id'])

frame, handler = menu.select(ctx, msg, frame)
frame, handler = menu.select(frame)
if not handler:
msg.add('This is a metabot! Check out https://github.com/nmlorg/metabot/issues to keep '
'track of bugs and features.')
Expand All @@ -50,7 +50,7 @@ def default(ctx, msg): # pylint: disable=missing-docstring
except IOError:
pass
msg.add('To configure your bot, choose its username:')
return menu.display(ctx, msg, frame, 'bot')
return menu.display(frame, what='bot')

msg.path(frame.field)
ctx.targetbotuser = frame.field
Expand All @@ -66,7 +66,7 @@ def default(ctx, msg): # pylint: disable=missing-docstring
"Hi! There aren't any configurable modules installed. Contact a metabot admin to "
'install one.')

menu.handle(ctx, msg, frame, 'module')
menu.handle(frame, what='module')


def bootstrap(ctx, msg, modconf):
Expand All @@ -77,10 +77,10 @@ def bootstrap(ctx, msg, modconf):
msg.add('Added %s to the admin list.', ctx.user['id'])


def admin(ctx, msg, frame): # pylint: disable=too-many-branches
def admin(frame): # pylint: disable=too-many-branches
"""Handle /admin BOTNAME admin (configure the admin module itself)."""

modconf = frame.value
ctx, msg, modconf = frame.ctx, frame.msg, frame.value
if 'admins' not in modconf: # pragma: no cover
modconf['admins'] = []

Expand Down
11 changes: 6 additions & 5 deletions metabot/modules/countdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ def format_delta(seconds):
return '<b>NOW</b>'


def admin(ctx, msg, frame):
def admin(frame):
"""Handle /admin BOTNAME countdown."""

msg = frame.msg
menu = adminui.Menu()
for command in sorted(frame.value):
menu.add(command)
newframe, handler = menu.select(ctx, msg, frame, create=True)
newframe, handler = menu.select(frame, create=True)
if handler:
if newframe.text.isdigit():
adminui.set_log(msg, newframe, int(newframe.text))
adminui.set_log(newframe, int(newframe.text))
elif newframe.text.lower() in ('-', 'none', 'off', 'remove'):
adminui.set_log(msg, newframe, None)
adminui.set_log(newframe, None)
else:
if newframe.text:
msg.add("I'm not sure how to count down to <code>%s</code>!", newframe.text)
Expand All @@ -88,4 +89,4 @@ def admin(ctx, msg, frame):
menu = adminui.Menu()
for command in sorted(frame.value):
menu.add(command)
menu.display(ctx, msg, frame, 'command')
menu.display(frame, what='command')
9 changes: 5 additions & 4 deletions metabot/modules/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ def echo(ctx, msg, data): # pylint: disable=missing-docstring
msg.button('More (%i/%i)' % (page, len(lines)), '/%s %i' % (ctx.command, page + 1))


def admin(ctx, msg, frame):
def admin(frame):
"""Handle /admin BOTNAME echo."""

msg = frame.msg
menu = adminui.Menu()
for command, data in sorted(frame.value.items()):
menu.add(command, desc=data.get('text', '').replace('\n', ' '))
frame, handler = menu.select(ctx, msg, frame, create=True)
frame, handler = menu.select(frame, create=True)
if not handler:
msg.action = 'Choose a command'
msg.add(
"Type the name of a command to add (like <code>rules</code>\u2014don't include a slash "
'at the beginning!), or select an existing echo.')
return menu.display(ctx, msg, frame, 'command')
return menu.display(frame, what='command')

msg.path(frame.field)

Expand All @@ -55,4 +56,4 @@ def admin(ctx, msg, frame):
'The message, sticker, or image to send in response to /%s.' % frame.field),
('paginate', adminui.bool, 'For multiline messages, display just one line at a time?'),
('private', adminui.bool, 'Send the message in group chats, or just in private?'),
).handle(ctx, msg, frame)
).handle(frame)
2 changes: 1 addition & 1 deletion metabot/modules/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,4 @@ def settings(ctx, msg, modconf):
adminui.Menu(
('calendars', adminui.calendars, 'Which calendars do you want to see?'),
('timezone', adminui.timezone, 'What time zone are you in?'),
).handle(ctx, msg, adminui.Frame(modconf['users'], user_id, None, text))
).handle(adminui.Frame(ctx, msg, modconf['users'], user_id, None, text))
4 changes: 2 additions & 2 deletions metabot/modules/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def inline(ctx, modconf):
ctx.reply_inline(results, cache_time=60)


def admin(ctx, msg, frame):
def admin(frame):
"""Handle /admin BOTNAME groups."""

modconf = frame.value
ctx, msg, modconf = frame.ctx, frame.msg, frame.value
text = frame.text
if text.startswith('remove '):
text = text[len('remove '):]
Expand Down
4 changes: 2 additions & 2 deletions metabot/modules/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def default(ctx, msg, modconf): # pylint: disable=missing-docstring
msg.add('%s', line)


def admin(ctx, msg, frame): # pylint: disable=too-many-branches
def admin(frame): # pylint: disable=too-many-branches
"""Handle /admin BOTNAME help."""

modconf = frame.value
ctx, msg, modconf = frame.ctx, frame.msg, frame.value
action, _, modname = frame.text.partition(' ')

hidden = set(modconf.get('hidden', '').split())
Expand Down
13 changes: 6 additions & 7 deletions metabot/modules/moderator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def moddispatch(ctx, msg, modconf): # pylint: disable=missing-docstring
ctx.private = True
msg.path('/mod', 'Group Admin')
ctx.targetbotconf = ctx.bot.config
return admin(ctx,
msg,
adminui.Frame(ctx.bot.config['issue37'], 'moderator', 'Group Admin', ctx.text),
return admin(adminui.Frame(ctx, msg, ctx.bot.config, 'moderator', 'Group Admin', ctx.text),
botadmin=False)

return False
Expand Down Expand Up @@ -108,9 +106,10 @@ def join(ctx, msg, modconf):
msg.add(greeting)


def admin(ctx, msg, frame, botadmin=True): # pylint: disable=too-many-arguments
def admin(frame, botadmin=True): # pylint: disable=too-many-arguments
"""Handle /admin BOTNAME moderator."""

ctx, msg = frame.ctx, frame.msg
menu = adminui.Menu()
for group_id in sorted(frame.value):
groupdata = ctx.bot.multibot.conf['groups'][int(group_id)]
Expand All @@ -127,9 +126,9 @@ def admin(ctx, msg, frame, botadmin=True): # pylint: disable=too-many-arguments
"Hi! You aren't an admin in any groups I'm in. If you should be, ask a current admin "
"to promote you from the group's members list.")

frame, handler = menu.select(ctx, msg, frame)
frame, handler = menu.select(frame)
if not handler:
return menu.display(ctx, msg, frame, 'group')
return menu.display(frame, what='group')

msg.path(frame.field)

Expand All @@ -141,4 +140,4 @@ def admin(ctx, msg, frame, botadmin=True): # pylint: disable=too-many-arguments
('maxeventscount', adminui.integer, 'How many events should be listed in /events?'),
('maxeventsdays', adminui.integer, 'How many days into the future should /events look?'),
('timezone', adminui.timezone, 'What time zone should be used in /events?'),
).handle(ctx, msg, frame)
).handle(frame)
4 changes: 2 additions & 2 deletions metabot/modules/telegram.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Manage the bot's Telegram state."""


def admin(ctx, msg, frame):
def admin(frame):
"""Handle /admin BOTNAME telegram (manage the bot's Telegram state)."""

modconf = frame.value
ctx, msg, modconf = frame.ctx, frame.msg, frame.value
username = ctx.targetbotuser
msg.action = 'Choose an action'

Expand Down
Loading

0 comments on commit 725c362

Please sign in to comment.