Skip to content

Commit

Permalink
Upgrade buttons management and add polls
Browse files Browse the repository at this point in the history
  • Loading branch information
AlberLC committed Jun 22, 2022
1 parent 6cd4dad commit f98c8eb
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ For both a normal bot and a user bot (bot using your "human" account) you will n
:target: https://www.python.org/downloads/
:alt: PyPI - Python Version

.. |multiBot_class_diagram| image:: https://user-images.githubusercontent.com/37489786/173702357-d53b3d52-987a-447b-9a75-e5520d08f56c.png
.. |multiBot_class_diagram| image:: https://user-images.githubusercontent.com/37489786/174965487-c83fb40a-65f9-4796-b661-2eb93883fe5d.png
:alt: multiBot_class_diagram

.. |my.telegram.org_app| image:: https://user-images.githubusercontent.com/37489786/149607226-36b0e3d6-6e21-4852-a08f-16ce52d3a7dc.png
Expand Down
45 changes: 37 additions & 8 deletions multibot/bots/discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ async def _accept_button_event(self, event: constants.DISCORD_EVENT | Message):
case Message():
event = event.original_event

await event.response.defer()
try:
await event.response.defer()
except AttributeError:
pass

def _add_handlers(self):
super()._add_handlers()
Expand Down Expand Up @@ -313,7 +316,7 @@ async def find_users_by_roles(self, roles: Iterable[int | str | Role], group_: i
return users

@return_if_first_empty(exclude_self_types='DiscordBot', globals_=globals())
async def get_chat(self, chat: int | str | User | Chat | Message = None) -> Chat | None:
async def get_chat(self, chat: int | str | User | Chat | Message) -> Chat | None:
match chat:
case int(chat_id):
pass
Expand All @@ -339,6 +342,21 @@ async def get_me(self, group_: int | str | Chat | Message = None) -> User | None
else:
return await self.get_user(user, group_)

@return_if_first_empty(exclude_self_types='DiscordBot', globals_=globals())
async def get_message(self, chat: int | str | User | Chat | Message, message: int | str | Message) -> Message | None:
match message:
case int(message_id):
pass
case str(message_id):
message_id = int(message_id)
case Message():
return message
case _:
raise TypeError('bad arguments')

chat = await self.get_chat(chat)
return await self._get_message(await chat.original_object.fetch_message(message_id))

@return_if_first_empty(exclude_self_types='DiscordBot', globals_=globals())
async def get_roles(self, group_: int | str | Chat | Message) -> list[Role]:
if not (discord_group := await self._get_discord_group(group_)):
Expand Down Expand Up @@ -416,14 +434,15 @@ async def remove_role(self, user: int | str | User, group_: int | str | Chat | M
@parse_arguments
async def send(
self,
text='',
text: str = None,
media: Media = None,
buttons: list[str | tuple[str, bool] | Button | list[str | tuple[str, bool] | Button]] | None = None,
chat: int | str | User | Chat | Message | None = None,
message: Message = None,
*,
buttons_key: Any = None,
reply_to: int | str | Message = None,
contents: dict = None,
silent: bool = False,
send_as_file: bool = None,
edit=False
Expand All @@ -442,6 +461,8 @@ async def send(

if edit:
kwargs = {}
if text is not None:
kwargs['content'] = text
if file:
kwargs['attachments'] = [file]
if buttons is not None:
Expand All @@ -450,9 +471,16 @@ async def send(
if buttons_key is not None:
message.buttons_info.key = buttons_key

message.original_object = await message.original_object.edit(content=text, **kwargs)
if content := getattr(media, 'content', None):
message.contents = [content]
message.original_object = await message.original_object.edit(**kwargs)
last_contents = message.contents
if media_content := getattr(media, 'content', None):
del last_contents['media']
message.contents = {'media': media_content}
if contents is None:
message.contents |= last_contents
else:
message.contents |= contents

message.update_last_edit()
message.save()

Expand All @@ -479,8 +507,9 @@ async def send(
raise e

bot_message.buttons_info = ButtonsInfo(buttons=buttons, key=buttons_key)
if content := getattr(media, 'content', None):
bot_message.contents = [content]
bot_message.contents = {'media': getattr(media, 'content', None)}
if contents is not None:
bot_message.contents |= contents

bot_message.save()

Expand Down
59 changes: 37 additions & 22 deletions multibot/bots/multi_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import telethon.events
import telethon.events.common
from flanautils import AmbiguityError, Media, NotFoundError, OrderedSet, RatioMatch, return_if_first_empty, shift_args_if_called

from multibot import constants
from multibot.exceptions import LimitError, SendError
from multibot.models import Ban, BotAction, Button, ButtonsGroup, Chat, Message, Mute, Platform, RegisteredButtonCallback, RegisteredCallback, Role, User
Expand Down Expand Up @@ -59,10 +60,11 @@ def admin(func_: Callable = None, /, is_=True, send_negative=False) -> Callable:
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
async def wrapper(self: MultiBot, message: Message):
message = message
if is_ == message.author.is_admin or message.chat.is_private:
return await func(self, message, *args, **kwargs)
return await func(self, message)
await self._accept_button_event(message)
if send_negative:
await self.send_negative(message)

Expand All @@ -73,7 +75,9 @@ async def wrapper(self: MultiBot, message: Message, *args, **kwargs):

def block(func: Callable) -> Callable:
@functools.wraps(func)
async def wrapper(*_args, **_kwargs):
@find_message
async def wrapper(self: MultiBot, message: Message):
await self._accept_button_event(message)
return

return wrapper
Expand All @@ -84,9 +88,10 @@ def bot_mentioned(func_: Callable = None, /, is_=True) -> Callable:
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
async def wrapper(self: MultiBot, message: Message):
if is_ == self.is_bot_mentioned(message):
return await func(self, message, *args, **kwargs)
return await func(self, message)
await self._accept_button_event(message)

return wrapper

Expand All @@ -98,9 +103,10 @@ def group(func_: Callable = None, /, is_=True) -> Callable:
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
async def wrapper(self: MultiBot, message: Message):
if is_ == message.chat.is_group:
return await func(self, message, *args, **kwargs)
return await func(self, message)
await self._accept_button_event(message)

return wrapper

Expand All @@ -110,9 +116,10 @@ async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
def ignore_self_message(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
async def wrapper(self: MultiBot, message: Message):
if message.author.id != self.id:
return await func(self, message, *args, **kwargs)
return await func(self, message)
await self._accept_button_event(message)

return wrapper

Expand All @@ -122,9 +129,10 @@ def inline(func_: Callable = None, /, is_=True) -> Callable:
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
async def wrapper(self: MultiBot, message: Message):
if message.is_inline is None or is_ == message.is_inline:
return await func(self, message, *args, **kwargs)
return await func(self, message)
await self._accept_button_event(message)

return wrapper

Expand All @@ -134,9 +142,10 @@ async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
def out_of_service(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *_args, **_kwargs):
async def wrapper(self: MultiBot, message: Message):
if self.is_bot_mentioned(message) or message.chat.is_private:
await self.send(random.choice(constants.OUT_OF_SERVICES_PHRASES), message)
await self._accept_button_event(message)

return wrapper

Expand Down Expand Up @@ -171,7 +180,7 @@ def parse_buttons(buttons_) -> list[list[Button]] | None:
return buttons_

self: MultiBot | None = None
text = ''
text: str | None = None
media: Media | None = None
buttons: list[str | tuple[str, bool] | Button | list[str | tuple[str, bool] | Button]] | None = None
chat: int | str | User | Chat | Message | None = None
Expand Down Expand Up @@ -199,8 +208,8 @@ def parse_buttons(buttons_) -> list[list[Button]] | None:
chat = await self.get_chat(kwargs.get('chat', chat))
if 'buttons' in kwargs:
buttons = parse_buttons(kwargs['buttons'])
reply_to = kwargs.get('reply_to', None)
edit = kwargs.get('edit', None)
reply_to = kwargs.get('reply_to')
edit = kwargs.get('edit')

if reply_to is not None:
if chat:
Expand Down Expand Up @@ -233,9 +242,10 @@ def reply(func_: Callable = None, /, is_=True) -> Callable:
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
@find_message
async def wrapper(self: MultiBot, message: Message, *args, **kwargs):
async def wrapper(self: MultiBot, message: Message):
if is_ == bool(message.replied_message):
return await func(self, message, *args, **kwargs)
return await func(self, message)
await self._accept_button_event(message)

return wrapper

Expand Down Expand Up @@ -439,7 +449,7 @@ def _parse_callbacks(
mached_keywords_groups = 0
total_ratio = 0
for keywords_group in registered_callback.keywords:
text_words += [original_text_word for original_text_word in original_text_words if original_text_word in keywords_group]
text_words += [original_text_word for original_text_word in original_text_words if flanautils.cartesian_product_string_matching(original_text_word, keywords_group, min_ratio=registered_callback.min_ratio)]
word_matches = flanautils.cartesian_product_string_matching(text_words, keywords_group, min_ratio=registered_callback.min_ratio)
ratio = sum((max(matches.values()) + 1) ** ratio_reward_exponent for matches in word_matches.values())
try:
Expand Down Expand Up @@ -561,9 +571,9 @@ async def _on_users(self, message: Message):
user_names = [f'<@{user.id}>' for user in await self.find_users_by_roles([], message)]
joined_user_names = ', '.join(user_names)
await self.send(
f"<b>{len(user_names)} usuario{'' if len(user_names) == 1 else 's'}:<b>\n"
f"<b>{len(user_names)} usuario{'' if len(user_names) == 1 else 's'}:</b>\n"
f"{joined_user_names}\n\n"
f"<b>Filtrar usuarios por roles:<b>",
f"<b>Filtrar usuarios por roles:</b>",
flanautils.chunks([f'❌ {role_name}' for role_name in role_names], 5),
message,
buttons_key=ButtonsGroup.USERS
Expand All @@ -585,9 +595,9 @@ async def _on_users_button_press(self, message: Message):
user_names = [f'<@{user.id}>' for user in await self.find_users_by_roles(selected_role_names, message)]
joined_user_names = ', '.join(user_names)
await self.edit(
f"<b>{len(user_names)} usuario{'' if len(user_names) == 1 else 's'}:<b>\n"
f"<b>{len(user_names)} usuario{'' if len(user_names) == 1 else 's'}:</b>\n"
f"{joined_user_names}\n\n"
f"<b>Filtrar usuarios por roles:<b>",
f"<b>Filtrar usuarios por roles:</b>",
message.buttons_info.buttons,
message
)
Expand Down Expand Up @@ -653,6 +663,10 @@ def get_group_name(self, group_: int | str | Chat | Message) -> str | None:
async def get_me(self, group_: int | str | Chat | Message = None) -> User | None:
pass

@return_if_first_empty(exclude_self_types='MultiBot', globals_=globals())
async def get_message(self, chat: int | str | User | Chat | Message, message: int | str | Message) -> Message | None:
pass

@return_if_first_empty(exclude_self_types='MultiBot', globals_=globals())
async def get_role(self, role: int | str | Role, group_: int | str | Chat | Message) -> Role | None:
if isinstance(role, Role):
Expand Down Expand Up @@ -769,6 +783,7 @@ async def send(
*,
buttons_key: Any = None,
reply_to: int | str | Message = None,
contents: dict = None,
silent: bool = False,
send_as_file: bool = None,
edit=False
Expand Down
Loading

0 comments on commit f98c8eb

Please sign in to comment.