mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Add error_if_none parameter to call.get_author
This commit is contained in:
parent
8d232fdc6b
commit
4735d145b6
5 changed files with 18 additions and 11 deletions
|
@ -4,7 +4,7 @@ import typing
|
||||||
import logging as _logging
|
import logging as _logging
|
||||||
import sys
|
import sys
|
||||||
from ..commands import NullCommand
|
from ..commands import NullCommand
|
||||||
from ..utils import asyncify, Call, Command
|
from ..utils import asyncify, Call, Command, UnregisteredError
|
||||||
from ..network import RoyalnetLink, Message
|
from ..network import RoyalnetLink, Message
|
||||||
from ..database import Alchemy, relationshiplinkchain
|
from ..database import Alchemy, relationshiplinkchain
|
||||||
|
|
||||||
|
@ -71,10 +71,12 @@ class TelegramBot:
|
||||||
response = await self.network.request(message, destination)
|
response = await self.network.request(message, destination)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
async def get_author(call):
|
async def get_author(call, error_if_none=False):
|
||||||
update: telegram.Update = call.kwargs["update"]
|
update: telegram.Update = call.kwargs["update"]
|
||||||
user: telegram.User = update.effective_user
|
user: telegram.User = update.effective_user
|
||||||
if user is None:
|
if user is None:
|
||||||
|
if error_if_none:
|
||||||
|
raise UnregisteredError("Author is not registered!")
|
||||||
return None
|
return None
|
||||||
query = call.session.query(self.master_table)
|
query = call.session.query(self.master_table)
|
||||||
for link in self.identity_chain:
|
for link in self.identity_chain:
|
||||||
|
|
|
@ -45,10 +45,7 @@ class DiarioCommand(Command):
|
||||||
@classmethod
|
@classmethod
|
||||||
async def common(cls, call: Call):
|
async def common(cls, call: Call):
|
||||||
# Find the creator of the quotes
|
# Find the creator of the quotes
|
||||||
creator = await call.get_author()
|
creator = await call.get_author(error_if_none=True)
|
||||||
if creator is None:
|
|
||||||
await call.reply("⚠️ Devi essere registrato a Royalnet per usare questo comando!")
|
|
||||||
return
|
|
||||||
# Recreate the full sentence
|
# Recreate the full sentence
|
||||||
raw_text = " ".join(call.args)
|
raw_text = " ".join(call.args)
|
||||||
# Pass the sentence through the diario regex
|
# Pass the sentence through the diario regex
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import traceback
|
import traceback
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
from ..utils import Command, CommandArgs, Call, InvalidInputError, UnsupportedError
|
from ..utils import Command, CommandArgs, Call, InvalidInputError, UnsupportedError, UnregisteredError
|
||||||
|
|
||||||
|
|
||||||
class ErrorHandlerCommand(Command):
|
class ErrorHandlerCommand(Command):
|
||||||
|
@ -24,6 +24,9 @@ class ErrorHandlerCommand(Command):
|
||||||
command = call.kwargs["previous_command"]
|
command = call.kwargs["previous_command"]
|
||||||
await call.reply(f"⚠️ Sintassi non valida.\nSintassi corretta: [c]/{command.command_name} {command.command_syntax}[/c]")
|
await call.reply(f"⚠️ Sintassi non valida.\nSintassi corretta: [c]/{command.command_name} {command.command_syntax}[/c]")
|
||||||
return
|
return
|
||||||
|
if e_type == UnregisteredError:
|
||||||
|
await call.reply("⚠️ Devi essere registrato a Royalnet per usare questo comando!")
|
||||||
|
return
|
||||||
await call.reply(f"❌ Eccezione non gestita durante l'esecuzione del comando:\n[b]{e_type.__name__}[/b]\n{e_value}")
|
await call.reply(f"❌ Eccezione non gestita durante l'esecuzione del comando:\n[b]{e_type.__name__}[/b]\n{e_value}")
|
||||||
formatted_tb: str = '\n'.join(traceback.format_tb(e_tb))
|
formatted_tb: str = '\n'.join(traceback.format_tb(e_tb))
|
||||||
call.logger.error(f"Unhandled exception - {e_type.__name__}: {e_value}\n{formatted_tb}")
|
call.logger.error(f"Unhandled exception - {e_type.__name__}: {e_value}\n{formatted_tb}")
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from .asyncify import asyncify
|
from .asyncify import asyncify
|
||||||
from .call import Call
|
from .call import Call, UnregisteredError
|
||||||
from .command import Command, CommandArgs, InvalidInputError, UnsupportedError, InvalidConfigError, ExternalError
|
from .command import Command, CommandArgs, InvalidInputError, UnsupportedError, InvalidConfigError, ExternalError
|
||||||
from .safeformat import safeformat
|
from .safeformat import safeformat
|
||||||
from .classdictjanitor import cdj
|
from .classdictjanitor import cdj
|
||||||
from .sleepuntil import sleep_until
|
from .sleepuntil import sleep_until
|
||||||
|
|
||||||
__all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError", "CommandArgs",
|
__all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError", "CommandArgs",
|
||||||
"cdj", "InvalidConfigError", "ExternalError", "sleep_until"]
|
"cdj", "InvalidConfigError", "ExternalError", "sleep_until", "UnregisteredError"]
|
||||||
|
|
|
@ -10,6 +10,10 @@ if typing.TYPE_CHECKING:
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
|
||||||
|
class UnregisteredError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Call:
|
class Call:
|
||||||
"""A command call. Still an abstract class, subbots should create a new call from this."""
|
"""A command call. Still an abstract class, subbots should create a new call from this."""
|
||||||
|
|
||||||
|
@ -27,9 +31,10 @@ class Call:
|
||||||
The data must be pickleable."""
|
The data must be pickleable."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def get_author(self):
|
async def get_author(self, error_if_none=False):
|
||||||
"""Try to find the universal identifier of the user that sent the message.
|
"""Try to find the universal identifier of the user that sent the message.
|
||||||
That probably means, the database row identifying the user."""
|
That probably means, the database row identifying the user.
|
||||||
|
Raise a UnregisteredError if error_if_none is set to True and no author is found."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
# These parameters / methods should be left alone
|
# These parameters / methods should be left alone
|
||||||
|
|
Loading…
Reference in a new issue