Queer European MD passionate about IT
Browse Source

Added support for multilanguage command descriptions

Davte 5 years ago
parent
commit
3f4417b84b
4 changed files with 35 additions and 28 deletions
  1. 1 1
      davtelepot/__init__.py
  2. 18 15
      davtelepot/administration_tools.py
  3. 11 12
      davtelepot/authorization.py
  4. 5 0
      davtelepot/bot.py

+ 1 - 1
davtelepot/__init__.py

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

+ 18 - 15
davtelepot/administration_tools.py

@@ -488,6 +488,14 @@ async def _talk_button(bot, update, user_record, data):
 
 
 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': {
         'description': {
             'en': "Restart bots",
@@ -545,6 +553,10 @@ default_admin_messages = {
         }
     },
     'db_command': {
+        'description': {
+            'en': "Ask for bot database via Telegram",
+            'it': "Ricevi il database del bot via Telegram"
+        },
         'not_sqlite': {
             'en': "Only SQLite databases may be sent via Telegram, since they "
                   "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`."""
     if talk_messages is None:
         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,
-                 description="Choose a user and forward messages to each "
-                             "other.",
+                 description=admin_messages['talk_command']['description'],
                  authorization_level='admin')
     async def talk_command(update):
         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)
 
     @bot.command(command='/restart', aliases=[], show_in_keyboard=False,
-                 description=bot.get_message(
-                     'admin', 'restart_command', 'description',
-                     language=language, default_message=''
-                 ),
+                 description=admin_messages['restart_command']['description'],
                  authorization_level='admin')
     async def 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
 
-    stop_command_description = bot.get_message(
-        'admin', 'stop_command', 'description',
-        language=language
-    )
-
     @bot.command(command='/stop', aliases=[], show_in_keyboard=False,
-                 description=stop_command_description,
+                 description=admin_messages['stop_command']['description'],
                  authorization_level='admin')
     async def stop_command(bot, update, user_record):
         return await _stop_command(bot, update, user_record)
 
     @bot.button(prefix='stop:///', separator='|',
-                description=stop_command_description,
+                description=admin_messages['stop_command']['description'],
                 authorization_level='admin')
     async def 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,
-                 description="Ask for bot database via Telegram",
+                 description=admin_messages['db_command']['description'],
                  authorization_level='admin')
     async def send_bot_database(bot, update, user_record):
         return await _send_bot_database(bot, update, user_record)

+ 11 - 12
davtelepot/authorization.py

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

+ 5 - 0
davtelepot/bot.py

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