Order commands in order of authorization level and then in alphabetical order

This commit is contained in:
Davte 2020-02-20 18:16:59 +01:00
parent b4ad650372
commit 6120568170
4 changed files with 84 additions and 46 deletions

View File

@ -14,7 +14,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.4.9" __version__ = "2.4.10"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -946,14 +946,12 @@ def init(telegram_bot, talk_messages=None, admin_messages=None):
async def maintenance_command(bot, update, user_record): async def maintenance_command(bot, update, user_record):
return await _maintenance_command(bot, update, user_record) return await _maintenance_command(bot, update, user_record)
@telegram_bot.command( @telegram_bot.command(command='/version',
command='/version', aliases=[],
aliases=[], reply_keyboard_button=admin_messages['version_command']['reply_keyboard_button'],
reply_keyboard_button=admin_messages['version_command']['reply_keyboard_button'], show_in_keyboard=False,
show_in_keyboard=False, description=admin_messages['version_command']['description'],
description=admin_messages['version_command']['description'], help_section=admin_messages['version_command']['help_section'],
help_section=admin_messages['version_command']['help_section'], authorization_level='admin',)
authorization_level='admin',
)
async def version_command(bot, update, user_record): async def version_command(bot, update, user_record):
return await _version_command(bot=bot, update=update, user_record=user_record) return await _version_command(bot=bot, update=update, user_record=user_record)

View File

@ -10,7 +10,7 @@ from davtelepot.utilities import (
from .messages import default_help_messages from .messages import default_help_messages
def get_command_description(bot, update, user_record): def get_commands_description(bot, update, user_record):
"""Get a string description of `bot` commands. """Get a string description of `bot` commands.
Show only commands available for `update` sender. Show only commands available for `update` sender.
@ -18,11 +18,26 @@ def get_command_description(bot, update, user_record):
user_role = bot.Role.get_user_role( user_role = bot.Role.get_user_role(
user_record=user_record user_record=user_record
) )
return "\n".join( commands = {}
[ for command, details in bot.commands.items():
"/{}: {}".format( if 'description' not in details or not details['description']:
command, continue
bot.get_message( if 'authorization_level' not in details:
continue
command_role = bot.Role.get_role_by_name(details['authorization_level'])
if command_role.code < user_role.code:
continue
if command_role.code not in commands:
commands[command_role.code] = []
commands[command_role.code].append(
"/{command}{authorization_level}: {description}".format(
command=command,
authorization_level=(
f" <i>[{command_role.plural}]</i>"
if command_role.code != bot.Role.default_role_code
else ""
),
description=bot.get_message(
'commands', command, 'description', 'commands', command, 'description',
user_record=user_record, update=update, user_record=user_record, update=update,
default_message=( default_message=(
@ -32,20 +47,47 @@ def get_command_description(bot, update, user_record):
) )
) )
) )
for command, details in sorted( )
bot.commands.items(), return "\n".join(
key=lambda x:x[0] [
) command
if 'description' in details and details['description'] for role, commands in sorted(
and user_role.code <= bot.Role.get_user_role( commands.items(),
user_role_id=details['authorization_level'] key=(lambda x: -x[0])
).code )
for command in sorted(
commands
)
] ]
) )
# [
# "/{command}{authorization_level}: {description}".format(
# command=command,
# authorization_level=(
# ""
# if 1
# else ""
# ),
# description=bot.get_message(
# 'commands', command, 'description',
# user_record=user_record, update=update,
# default_message=(
# details['description']
# if type(details['description']) is str
# else ''
# )
# )
# )
# if 'description' in details and details['description']
# and user_role.code <= bot.Role.get_user_role(
# user_role_id=details['authorization_level']
# ).code
# ]
# )
def _make_button(text=None, callback_data='', def _make_button(text=None, callback_data='',
prefix='help:///', delimiter='|', data=[]): prefix='help:///', delimiter='|', data=None):
return make_button(text=text, callback_data=callback_data, return make_button(text=text, callback_data=callback_data,
prefix=prefix, delimiter=delimiter, data=data) prefix=prefix, delimiter=delimiter, data=data)
@ -84,7 +126,7 @@ def get_help_buttons(bot, update, user_record):
) )
for name, section in bot.messages['help_sections'].items() for name, section in bot.messages['help_sections'].items()
if 'authorization_level' in section if 'authorization_level' in section
and user_role.code <= bot.Role.get_user_role( and user_role.code <= bot.Role.get_user_role(
user_role_id=section['authorization_level'] user_role_id=section['authorization_level']
).code ).code
] ]
@ -134,7 +176,7 @@ async def _help_button(bot, update, user_record, data):
'help', 'help_command', 'header', 'help', 'help_command', 'header',
update=update, user_record=user_record, update=update, user_record=user_record,
bot=bot, bot=bot,
commands=get_command_description(bot, update, user_record) commands=get_commands_description(bot, update, user_record)
) )
reply_markup = get_back_to_help_menu_keyboard( reply_markup = get_back_to_help_menu_keyboard(
bot=bot, update=update, user_record=user_record bot=bot, update=update, user_record=user_record
@ -147,14 +189,14 @@ async def _help_button(bot, update, user_record, data):
) )
reply_markup = get_help_buttons(bot, update, user_record) reply_markup = get_help_buttons(bot, update, user_record)
elif ( elif (
data[0] == 'section' data[0] == 'section'
and len(data) > 1 and len(data) > 1
and data[1] in bot.messages['help_sections'] and data[1] in bot.messages['help_sections']
): ):
section = bot.messages['help_sections'][data[1]] section = bot.messages['help_sections'][data[1]]
if bot.authorization_function( if bot.authorization_function(
update=update, update=update,
authorization_level=section['authorization_level'] authorization_level=section['authorization_level']
): ):
text = ( text = (
"<b>{label}</b>\n\n" "<b>{label}</b>\n\n"
@ -200,27 +242,26 @@ async def _start_command(bot, update, user_record):
return return
def init(bot, help_messages=None): def init(telegram_bot, help_messages=None):
"""Assign parsers, commands, buttons and queries to given `bot`.""" """Assign parsers, commands, buttons and queries to given `bot`."""
if help_messages is None: if help_messages is None:
help_messages = default_help_messages help_messages = default_help_messages
bot.messages['help'] = help_messages telegram_bot.messages['help'] = help_messages
@bot.command("/start", authorization_level='everybody') @telegram_bot.command("/start", authorization_level='everybody')
async def start_command(bot, update, user_record): async def start_command(bot, update, user_record):
return await _start_command(bot, update, user_record) return await _start_command(bot, update, user_record)
@bot.command(command='/help', aliases=['00help'], @telegram_bot.command(command='/help', aliases=['00help'],
reply_keyboard_button=help_messages['help_command'][ reply_keyboard_button=help_messages['help_command'][
'reply_keyboard_button'], 'reply_keyboard_button'],
show_in_keyboard=True, show_in_keyboard=True,
description=help_messages['help_command']['description'], description=help_messages['help_command']['description'],
authorization_level='everybody') authorization_level='everybody')
async def help_command(bot, update, user_record): async def help_command(bot, update, user_record):
result = await _help_command(bot, update, user_record) return await _help_command(bot, update, user_record)
return result
@bot.button(prefix='help:///', separator='|', @telegram_bot.button(prefix='help:///', separator='|',
authorization_level='everybody') authorization_level='everybody')
async def help_button(bot, update, user_record, data): async def help_button(bot, update, user_record, data):
return await _help_button(bot, update, user_record, data) return await _help_button(bot, update, user_record, data)

View File

@ -280,7 +280,6 @@ def init(telegram_bot: davtelepot.bot.Bot, suggestion_messages=default_suggestio
types.datetime types.datetime
) )
@telegram_bot.command(command=suggestion_messages['suggestions_command']['command'], @telegram_bot.command(command=suggestion_messages['suggestions_command']['command'],
aliases=suggestion_messages['suggestions_command']['aliases'], aliases=suggestion_messages['suggestions_command']['aliases'],
reply_keyboard_button=( reply_keyboard_button=(