|
|
|
@@ -2,6 +2,7 @@ __all__ = ['FlanaBot']
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import asyncio
|
|
|
|
import datetime
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
import math
|
|
|
|
import random
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import time as time_module
|
|
|
|
import time as time_module
|
|
|
|
@@ -167,12 +168,12 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
last_2s_messages = Message.find({
|
|
|
|
last_2s_messages = Message.find({
|
|
|
|
'platform': self.platform.value,
|
|
|
|
'platform': self.platform.value,
|
|
|
|
'author': message.author.object_id,
|
|
|
|
'author': message.author.object_id,
|
|
|
|
'last_update': {'$gte': datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(seconds=2)}
|
|
|
|
'date': {'$gte': datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(seconds=2)}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
last_7s_messages = Message.find({
|
|
|
|
last_7s_messages = Message.find({
|
|
|
|
'platform': self.platform.value,
|
|
|
|
'platform': self.platform.value,
|
|
|
|
'author': message.author.object_id,
|
|
|
|
'author': message.author.object_id,
|
|
|
|
'last_update': {'$gte': datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(seconds=7)}
|
|
|
|
'date': {'$gte': datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(seconds=7)}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if len(last_2s_messages) >= constants.FLOOD_2s_LIMIT or len(last_7s_messages) >= constants.FLOOD_7s_LIMIT:
|
|
|
|
if len(last_2s_messages) >= constants.FLOOD_2s_LIMIT or len(last_7s_messages) >= constants.FLOOD_7s_LIMIT:
|
|
|
|
@@ -269,7 +270,7 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
return_exceptions=True
|
|
|
|
return_exceptions=True
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if not message.is_inline and (self.is_bot_mentioned(message) or not message.chat.is_group):
|
|
|
|
if not message.is_inline and (self.is_bot_mentioned(message) or message.chat.is_private):
|
|
|
|
while not results.done():
|
|
|
|
while not results.done():
|
|
|
|
if constants.SCRAPING_MESSAGE_WAITING_TIME <= time_module.perf_counter() - start_time:
|
|
|
|
if constants.SCRAPING_MESSAGE_WAITING_TIME <= time_module.perf_counter() - start_time:
|
|
|
|
bot_state_message = await self.send(random.choice(constants.SCRAPING_PHRASES), message)
|
|
|
|
bot_state_message = await self.send(random.choice(constants.SCRAPING_PHRASES), message)
|
|
|
|
@@ -296,26 +297,53 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
# HANDLERS #
|
|
|
|
# HANDLERS #
|
|
|
|
# ---------------------------------------------- #
|
|
|
|
# ---------------------------------------------- #
|
|
|
|
async def _on_bye(self, message: Message):
|
|
|
|
async def _on_bye(self, message: Message):
|
|
|
|
if not message.chat.is_group or self.is_bot_mentioned(message):
|
|
|
|
if message.chat.is_private or self.is_bot_mentioned(message):
|
|
|
|
await self.send_bye(message)
|
|
|
|
await self.send_bye(message)
|
|
|
|
|
|
|
|
|
|
|
|
@bot_mentioned
|
|
|
|
|
|
|
|
async def _on_choose(self, message: Message):
|
|
|
|
async def _on_choose(self, message: Message):
|
|
|
|
discarded_words = {*constants.KEYWORDS['choose'], *constants.KEYWORDS['random'], self.name, f'<@{self.id}>'}
|
|
|
|
if message.chat.is_group and not self.is_bot_mentioned(message):
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
discarded_words = {
|
|
|
|
|
|
|
|
*constants.KEYWORDS['choose'],
|
|
|
|
|
|
|
|
*constants.KEYWORDS['random'],
|
|
|
|
|
|
|
|
self.name.lower(), f'<@{self.id}>',
|
|
|
|
|
|
|
|
*flanautils.CommonWords.get('conjunctions'),
|
|
|
|
|
|
|
|
'entre', 'between'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if final_words := [word for word in message.text.split() if not flanautils.cartesian_product_string_matching(word.lower(), discarded_words, min_ratio=multibot_constants.PARSE_CALLBACKS_MIN_RATIO_DEFAULT)]:
|
|
|
|
if final_words := [word for word in message.text.split() if not flanautils.cartesian_product_string_matching(word.lower(), discarded_words, min_ratio=multibot_constants.PARSE_CALLBACKS_MIN_RATIO_DEFAULT)]:
|
|
|
|
|
|
|
|
for i in range(1, len(final_words) - 1):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
n1 = flanautils.cast_number(final_words[i - 1])
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
n1 = flanautils.words_to_numbers(final_words[i - 1], ignore_no_numbers=False)
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
n2 = flanautils.cast_number(final_words[i + 1])
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
n2 = flanautils.words_to_numbers(final_words[i + 1], ignore_no_numbers=False)
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
if final_words[i] in ('al', 'el', 'to'):
|
|
|
|
|
|
|
|
await self.send(random.randint(math.ceil(n1), math.floor(n2)), message)
|
|
|
|
|
|
|
|
return
|
|
|
|
await self.send(random.choice(final_words), message)
|
|
|
|
await self.send(random.choice(final_words), message)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
await self.send(random.choice(('¿Que elija el qué?', '¿Y las opciones?', '?', '🤔')), message)
|
|
|
|
await self.send(random.choice(('¿Que elija el qué?', '¿Y las opciones?', '?', '🤔')), message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _on_config_button_press(self, message: Message):
|
|
|
|
async def _on_config_button_press(self, message: Message):
|
|
|
|
await self._accept_button_event(message)
|
|
|
|
await self.accept_button_event(message)
|
|
|
|
|
|
|
|
|
|
|
|
if not message.buttons_info.presser_user.is_admin:
|
|
|
|
if message.buttons_info.presser_user.is_admin is False:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
config = message.buttons_info.pressed_text.split()[1]
|
|
|
|
config = message.buttons_info.pressed_text.split()[1]
|
|
|
|
message.chat.config[config] = not message.chat.config[config]
|
|
|
|
message.chat.config[config] = not message.chat.config[config]
|
|
|
|
pressed_button = message.buttons_info[message.buttons_info.pressed_text]
|
|
|
|
pressed_button = message.buttons_info.pressed_button
|
|
|
|
pressed_button.is_checked = not pressed_button.is_checked
|
|
|
|
pressed_button.is_checked = not pressed_button.is_checked
|
|
|
|
pressed_button.text = f"{'✔' if pressed_button.is_checked else '❌'} {config}"
|
|
|
|
pressed_button.text = f"{'✔' if pressed_button.is_checked else '❌'} {config}"
|
|
|
|
|
|
|
|
|
|
|
|
@@ -381,15 +409,17 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
async def _on_delete_original_config_show(self, message: Message):
|
|
|
|
async def _on_delete_original_config_show(self, message: Message):
|
|
|
|
await self._show_config('auto_delete_original', message)
|
|
|
|
await self._show_config('auto_delete_original', message)
|
|
|
|
|
|
|
|
|
|
|
|
@bot_mentioned
|
|
|
|
|
|
|
|
async def _on_dice(self, message: Message):
|
|
|
|
async def _on_dice(self, message: Message):
|
|
|
|
|
|
|
|
if message.chat.is_group and not self.is_bot_mentioned(message):
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if top_number := flanautils.sum_numbers_in_text(message.text):
|
|
|
|
if top_number := flanautils.sum_numbers_in_text(message.text):
|
|
|
|
await self.send(random.randint(1, top_number), message)
|
|
|
|
await self.send(random.randint(1, math.floor(top_number)), message)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
await self.send(random.choice(('¿De cuántas caras?', '¿Y el número?', '?', '🤔')), message)
|
|
|
|
await self.send(random.choice(('¿De cuántas caras?', '¿Y el número?', '?', '🤔')), message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _on_hello(self, message: Message):
|
|
|
|
async def _on_hello(self, message: Message):
|
|
|
|
if not message.chat.is_group or self.is_bot_mentioned(message):
|
|
|
|
if message.chat.is_private or self.is_bot_mentioned(message):
|
|
|
|
await self.send_hello(message)
|
|
|
|
await self.send_hello(message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _on_new_message_default(self, message: Message):
|
|
|
|
async def _on_new_message_default(self, message: Message):
|
|
|
|
@@ -436,8 +466,10 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
def _distribute_poll_buttons(self, texts: Sequence[str]) -> list[list[str]]:
|
|
|
|
def _distribute_poll_buttons(self, texts: Sequence[str]) -> list[list[str]]:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@bot_mentioned
|
|
|
|
|
|
|
|
async def _on_poll(self, message: Message):
|
|
|
|
async def _on_poll(self, message: Message):
|
|
|
|
|
|
|
|
if message.chat.is_group and not self.is_bot_mentioned(message):
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
discarded_words = {*constants.KEYWORDS['poll'], self.name.lower(), f'<@{self.id}>'}
|
|
|
|
discarded_words = {*constants.KEYWORDS['poll'], self.name.lower(), f'<@{self.id}>'}
|
|
|
|
if final_options := [option.title() for option in message.text.split() if not flanautils.cartesian_product_string_matching(option.lower(), discarded_words, min_ratio=multibot_constants.PARSE_CALLBACKS_MIN_RATIO_DEFAULT)]:
|
|
|
|
if final_options := [option.title() for option in message.text.split() if not flanautils.cartesian_product_string_matching(option.lower(), discarded_words, min_ratio=multibot_constants.PARSE_CALLBACKS_MIN_RATIO_DEFAULT)]:
|
|
|
|
await self.send('Encuesta en curso...', self._distribute_poll_buttons(final_options), message, buttons_key=ButtonsGroup.POLL, contents={'poll': {'is_active': True, 'votes': {option: [] for option in final_options}}})
|
|
|
|
await self.send('Encuesta en curso...', self._distribute_poll_buttons(final_options), message, buttons_key=ButtonsGroup.POLL, contents={'poll': {'is_active': True, 'votes': {option: [] for option in final_options}}})
|
|
|
|
@@ -445,7 +477,7 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
await self.send(random.choice(('¿Y las opciones?', '?', '🤔')), message)
|
|
|
|
await self.send(random.choice(('¿Y las opciones?', '?', '🤔')), message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _on_poll_button_press(self, message: Message):
|
|
|
|
async def _on_poll_button_press(self, message: Message):
|
|
|
|
await self._accept_button_event(message)
|
|
|
|
await self.accept_button_event(message)
|
|
|
|
if not message.contents['poll']['is_active']:
|
|
|
|
if not message.contents['poll']['is_active']:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
@@ -580,16 +612,15 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
if song_infos:
|
|
|
|
if song_infos:
|
|
|
|
for song_info in song_infos:
|
|
|
|
for song_info in song_infos:
|
|
|
|
await self.send_song_info(song_info, message)
|
|
|
|
await self.send_song_info(song_info, message)
|
|
|
|
elif self.is_bot_mentioned(message) or not message.chat.is_group:
|
|
|
|
elif self.is_bot_mentioned(message) or message.chat.is_private:
|
|
|
|
await self._manage_exceptions(SendError('No hay información musical en ese mensaje.'), message)
|
|
|
|
await self._manage_exceptions(SendError('No hay información musical en ese mensaje.'), message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _on_stop_poll(self, message: Message):
|
|
|
|
async def _on_stop_poll(self, message: Message):
|
|
|
|
if poll_message := message.replied_message:
|
|
|
|
if poll_message := message.replied_message:
|
|
|
|
if poll_message.contents.get('poll') is None:
|
|
|
|
if poll_message.contents.get('poll') is None:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
elif (
|
|
|
|
elif (
|
|
|
|
self.is_bot_mentioned(message)
|
|
|
|
(message.chat.is_private or self.is_bot_mentioned(message))
|
|
|
|
and
|
|
|
|
and
|
|
|
|
flanautils.cartesian_product_string_matching(message.text, constants.KEYWORDS['poll'], min_ratio=multibot_constants.PARSE_CALLBACKS_MIN_RATIO_DEFAULT)
|
|
|
|
flanautils.cartesian_product_string_matching(message.text, constants.KEYWORDS['poll'], min_ratio=multibot_constants.PARSE_CALLBACKS_MIN_RATIO_DEFAULT)
|
|
|
|
and
|
|
|
|
and
|
|
|
|
@@ -631,7 +662,7 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
await self.unpunish(user, message, message)
|
|
|
|
await self.unpunish(user, message, message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _on_weather_button_press(self, message: Message):
|
|
|
|
async def _on_weather_button_press(self, message: Message):
|
|
|
|
await self._accept_button_event(message)
|
|
|
|
await self.accept_button_event(message)
|
|
|
|
|
|
|
|
|
|
|
|
match message.buttons_info.pressed_text:
|
|
|
|
match message.buttons_info.pressed_text:
|
|
|
|
case WeatherEmoji.ZOOM_IN.value:
|
|
|
|
case WeatherEmoji.ZOOM_IN.value:
|
|
|
|
@@ -694,7 +725,7 @@ class FlanaBot(MultiBot, ABC):
|
|
|
|
- flanautils.cartesian_product_string_matching(original_text_words, multibot_constants.KEYWORDS['date'], min_ratio=0.85).keys()
|
|
|
|
- flanautils.cartesian_product_string_matching(original_text_words, multibot_constants.KEYWORDS['date'], min_ratio=0.85).keys()
|
|
|
|
- flanautils.cartesian_product_string_matching(original_text_words, multibot_constants.KEYWORDS['thanks'], min_ratio=0.85).keys()
|
|
|
|
- flanautils.cartesian_product_string_matching(original_text_words, multibot_constants.KEYWORDS['thanks'], min_ratio=0.85).keys()
|
|
|
|
- possible_mentioned_ids
|
|
|
|
- possible_mentioned_ids
|
|
|
|
- flanautils.CommonWords.all_words
|
|
|
|
- flanautils.CommonWords.get()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if not place_words:
|
|
|
|
if not place_words:
|
|
|
|
if not message.is_inline:
|
|
|
|
if not message.is_inline:
|
|
|
|
|