Added support for multilanguage command descriptions

This commit is contained in:
Davte 2019-07-21 20:47:54 +02:00
parent b1c7ee4299
commit 3f4417b84b
4 changed files with 35 additions and 28 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.1.27" __version__ = "2.1.28"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -488,6 +488,14 @@ async def _talk_button(bot, update, user_record, data):
default_admin_messages = { default_admin_messages = {
'talk_command': {
'description': {
'en': "Choose a user and forward messages to each other",
'it': "Scegli un utente e il bot farà da tramite inoltrando a "
"ognuno i messaggi dell'altro finché non terminerai la "
"sessione"
}
},
'restart_command': { 'restart_command': {
'description': { 'description': {
'en': "Restart bots", 'en': "Restart bots",
@ -545,6 +553,10 @@ default_admin_messages = {
} }
}, },
'db_command': { 'db_command': {
'description': {
'en': "Ask for bot database via Telegram",
'it': "Ricevi il database del bot via Telegram"
},
'not_sqlite': { 'not_sqlite': {
'en': "Only SQLite databases may be sent via Telegram, since they " 'en': "Only SQLite databases may be sent via Telegram, since they "
"are single-file databases.\n" "are single-file databases.\n"
@ -693,7 +705,7 @@ async def _send_bot_database(bot, update, user_record):
) )
def init(bot, talk_messages=None, admin_messages=None, language='en'): def init(bot, talk_messages=None, admin_messages=None):
"""Assign parsers, commands, buttons and queries to given `bot`.""" """Assign parsers, commands, buttons and queries to given `bot`."""
if talk_messages is None: if talk_messages is None:
talk_messages = default_talk_messages talk_messages = default_talk_messages
@ -739,8 +751,7 @@ def init(bot, talk_messages=None, admin_messages=None, language='en'):
) )
@bot.command(command='/talk', aliases=[], show_in_keyboard=False, @bot.command(command='/talk', aliases=[], show_in_keyboard=False,
description="Choose a user and forward messages to each " description=admin_messages['talk_command']['description'],
"other.",
authorization_level='admin') authorization_level='admin')
async def talk_command(update): async def talk_command(update):
return await _talk_command(update, bot) return await _talk_command(update, bot)
@ -750,10 +761,7 @@ def init(bot, talk_messages=None, admin_messages=None, language='en'):
return await _talk_button(bot, update, user_record, data) return await _talk_button(bot, update, user_record, data)
@bot.command(command='/restart', aliases=[], show_in_keyboard=False, @bot.command(command='/restart', aliases=[], show_in_keyboard=False,
description=bot.get_message( description=admin_messages['restart_command']['description'],
'admin', 'restart_command', 'description',
language=language, default_message=''
),
authorization_level='admin') authorization_level='admin')
async def restart_command(bot, update, user_record): async def restart_command(bot, update, user_record):
return await _restart_command(bot, update, user_record) return await _restart_command(bot, update, user_record)
@ -787,25 +795,20 @@ def init(bot, talk_messages=None, admin_messages=None, language='en'):
) )
return return
stop_command_description = bot.get_message(
'admin', 'stop_command', 'description',
language=language
)
@bot.command(command='/stop', aliases=[], show_in_keyboard=False, @bot.command(command='/stop', aliases=[], show_in_keyboard=False,
description=stop_command_description, description=admin_messages['stop_command']['description'],
authorization_level='admin') authorization_level='admin')
async def stop_command(bot, update, user_record): async def stop_command(bot, update, user_record):
return await _stop_command(bot, update, user_record) return await _stop_command(bot, update, user_record)
@bot.button(prefix='stop:///', separator='|', @bot.button(prefix='stop:///', separator='|',
description=stop_command_description, description=admin_messages['stop_command']['description'],
authorization_level='admin') authorization_level='admin')
async def stop_button(bot, update, user_record, data): async def stop_button(bot, update, user_record, data):
return await _stop_button(bot, update, user_record, data) return await _stop_button(bot, update, user_record, data)
@bot.command(command='/db', aliases=[], show_in_keyboard=False, @bot.command(command='/db', aliases=[], show_in_keyboard=False,
description="Ask for bot database via Telegram", description=admin_messages['db_command']['description'],
authorization_level='admin') authorization_level='admin')
async def send_bot_database(bot, update, user_record): async def send_bot_database(bot, update, user_record):
return await _send_bot_database(bot, update, user_record) return await _send_bot_database(bot, update, user_record)

View File

@ -235,7 +235,7 @@ def get_authorization_function(bot):
return is_authorized return is_authorized
AUTHORIZATION_MESSAGES = { deafult_authorization_messages = {
'auth_command': { 'auth_command': {
'description': { 'description': {
'en': "Edit user permissions. To select a user, reply to " 'en': "Edit user permissions. To select a user, reply to "
@ -488,7 +488,7 @@ async def _ban_command(bot, update, user_record):
return return
def init(bot, roles=None, language='en'): def init(bot, roles=None, authorization_messages=None):
"""Set bot roles and assign role-related commands. """Set bot roles and assign role-related commands.
Pass an OrderedDict of `roles` to get them set. Pass an OrderedDict of `roles` to get them set.
@ -515,21 +515,21 @@ def init(bot, roles=None, language='en'):
bot.set_authorization_function( bot.set_authorization_function(
get_authorization_function(bot) get_authorization_function(bot)
) )
bot.messages['authorization'] = AUTHORIZATION_MESSAGES if authorization_messages is None:
authorization_messages = deafult_authorization_messages
bot.messages['authorization'] = authorization_messages
@bot.command(command='/auth', aliases=[], show_in_keyboard=False, @bot.command(command='/auth', aliases=[], show_in_keyboard=False,
description=bot.get_message( description=(
'authorization', 'auth_command', 'description', authorization_messages['auth_command']['description']
language=language
), ),
authorization_level='moderator') authorization_level='moderator')
async def authorization_command(bot, update, user_record): async def authorization_command(bot, update, user_record):
return await _authorization_command(bot, update, user_record) return await _authorization_command(bot, update, user_record)
@bot.button('auth:///', @bot.button('auth:///',
description=bot.get_message( description=(
'authorization', 'auth_button', 'description', authorization_messages['auth_button']['description']
language=language
), ),
separator='|', separator='|',
authorization_level='moderator') authorization_level='moderator')
@ -537,9 +537,8 @@ def init(bot, roles=None, language='en'):
return await _authorization_button(bot, update, user_record, data) return await _authorization_button(bot, update, user_record, data)
@bot.command('/ban', aliases=[], show_in_keyboard=False, @bot.command('/ban', aliases=[], show_in_keyboard=False,
description=bot.get_message( description=(
'authorization', 'ban_command', 'description', authorization_messages['ban_command']['description']
language=language
), ),
authorization_level='admin') authorization_level='admin')
async def ban_command(bot, update, user_record): async def ban_command(bot, update, user_record):

View File

@ -173,6 +173,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
self.individual_text_message_handlers = dict() self.individual_text_message_handlers = dict()
self.commands = OrderedDict() self.commands = OrderedDict()
self.command_aliases = OrderedDict() self.command_aliases = OrderedDict()
self.messages['commands'] = dict()
self._unknown_command_message = None self._unknown_command_message = None
self.text_message_parsers = OrderedDict() self.text_message_parsers = OrderedDict()
# Handle location messages # Handle location messages
@ -1559,6 +1560,10 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
description=description, description=description,
authorization_level=authorization_level authorization_level=authorization_level
) )
if type(description) is dict:
self.messages['commands'][command] = dict(
description=description
)
if aliases: if aliases:
for alias in aliases: for alias in aliases:
self.command_aliases[alias] = decorated_command_handler self.command_aliases[alias] = decorated_command_handler