Skip to content

Commit

Permalink
Update bot construction
Browse files Browse the repository at this point in the history
  • Loading branch information
AlberLC committed Jan 14, 2022
1 parent 1ec5f93 commit 696c3fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
15 changes: 9 additions & 6 deletions multibot/bots/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ def __init__(self, api_id: int | str, api_hash: int | str, phone: int | str = No
self.phone = phone
self.bot_session = bot_session
self.user_session = user_session
if bot_session:

if self.bot_session:
bot_client = TelegramClient(StringSession(bot_session), self.api_id, self.api_hash)
else:
bot_client = TelegramClient('bot_session', self.api_id, self.api_hash)
if user_session:

if self.user_session:
self.user_client = TelegramClient(StringSession(user_session), self.api_id, self.api_hash)
elif input('Do you want to add an user-bot too? (you will need a phone number later) [y/n]: ').strip().lower() in ('y', 'yes', 's', 'si', 'sí', '1', 'true', 'ok', 'vale'):
elif self.phone:
self.user_client = TelegramClient('user_session', self.api_id, self.api_hash)
else:
self.user_client = None

super().__init__(bot_token=bot_token,
bot_client=bot_client)

Expand Down Expand Up @@ -353,7 +356,7 @@ async def send(
bot_message.save()
try:
original_message = await self.bot_client.send_message(message.chat.original_object, text, **kwargs)
except telethon.errors.rpcerrorlist.UserIsBlockedError:
except (telethon.errors.rpcerrorlist.PeerIdInvalidError, telethon.errors.rpcerrorlist.UserIsBlockedError):
return
await self._create_bot_message_from_telegram_bot_message(original_message, message, contents=[getattr(media, 'content', None)])
elif media.type_ is MediaType.IMAGE:
Expand All @@ -363,15 +366,15 @@ async def send(
elif edit:
try:
await message.original_object.edit(text, **kwargs)
except telethon.errors.rpcerrorlist.MessageNotModifiedError:
except (telethon.errors.rpcerrorlist.PeerIdInvalidError, telethon.errors.rpcerrorlist.UserIsBlockedError):
pass
message.contents = [getattr(media, 'content', None)]
message.save()
return message
else:
try:
original_message = await self.bot_client.send_message(message.chat.original_object, text, **kwargs)
except telethon.errors.rpcerrorlist.UserIsBlockedError:
except (telethon.errors.rpcerrorlist.PeerIdInvalidError, telethon.errors.rpcerrorlist.UserIsBlockedError):
return
return await self._create_bot_message_from_telegram_bot_message(original_message, message, contents=[getattr(media, 'content', None)])

Expand Down
4 changes: 2 additions & 2 deletions multibot/bots/twitch_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
# ------------------------------------------- DISCORD_BOT ------------------------------------------- #
# --------------------------------------------------------------------------------------------------- #
class TwitchBot(MultiBot[twitchio.Client]):
def __init__(self, bot_token: str, initial_channels: Iterable[str] = None, loop: asyncio.AbstractEventLoop = None, owner_name: str = None):
def __init__(self, bot_token: str, initial_channels: Iterable[str] = None, owner_name: str = None):
super().__init__(bot_token=bot_token,
bot_client=twitchio.Client(token=bot_token, initial_channels=initial_channels, loop=loop))
bot_client=twitchio.Client(token=bot_token, initial_channels=initial_channels))
self.owner_name = owner_name

# ----------------------------------------------------------- #
Expand Down
6 changes: 3 additions & 3 deletions multibot/models/registered_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def __init__(
):
self.callback = callback
match keywords:
case str(phrase):
self.keywords = (tuple(phrase.split()),)
case str(keyword):
self.keywords = (tuple(keyword.strip().split()),)
case [*_, [*_]]:
self.keywords = tuple((keywords_group,) if isinstance(keywords_group, str) else keywords_group for keywords_group in keywords)
case [*_, str()]:
self.keywords = (keywords,)
self.keywords = tuple(keyword.strip().split() for keyword in keywords)
case _:
self.keywords = tuple(keywords)
self.min_ratio = min_ratio
Expand Down

0 comments on commit 696c3fc

Please sign in to comment.