Refactor Message.data system
This commit is contained in:
@@ -279,7 +279,7 @@ class Connect4Bot(MultiBot, ABC):
|
||||
return False
|
||||
|
||||
try:
|
||||
message.buttons_info.data['is_active'] = False
|
||||
message.data['is_active'] = False
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
@@ -472,12 +472,14 @@ class Connect4Bot(MultiBot, ABC):
|
||||
message=message,
|
||||
buttons=self.distribute_buttons([str(n) for n in range(1, constants.CONNECT_4_N_COLUMNS + 1)]),
|
||||
buttons_key=ButtonsGroup.CONNECT_4,
|
||||
buttons_data={
|
||||
'is_active': True,
|
||||
'board': board,
|
||||
'player_1': player_1.to_dict(),
|
||||
'player_2': player_2.to_dict(),
|
||||
'turn': 0
|
||||
data={
|
||||
'connect_4': {
|
||||
'is_active': True,
|
||||
'board': board,
|
||||
'player_1': player_1.to_dict(),
|
||||
'player_2': player_2.to_dict(),
|
||||
'turn': 0
|
||||
}
|
||||
}
|
||||
)
|
||||
await self.delete_message(message)
|
||||
@@ -485,12 +487,14 @@ class Connect4Bot(MultiBot, ABC):
|
||||
async def _on_connect_4_button_press(self, message: Message):
|
||||
await self.accept_button_event(message)
|
||||
|
||||
is_active = message.buttons_info.data['is_active']
|
||||
board = message.buttons_info.data['board']
|
||||
player_1 = Player.from_dict(message.buttons_info.data['player_1'])
|
||||
player_2 = Player.from_dict(message.buttons_info.data['player_2'])
|
||||
connect_4_data = message.data['connect_4']
|
||||
|
||||
if message.buttons_info.data['turn'] % 2 == 0:
|
||||
is_active = connect_4_data['is_active']
|
||||
board = connect_4_data['board']
|
||||
player_1 = Player.from_dict(connect_4_data['player_1'])
|
||||
player_2 = Player.from_dict(connect_4_data['player_2'])
|
||||
|
||||
if connect_4_data['turn'] % 2 == 0:
|
||||
current_player = player_1
|
||||
next_player = player_2
|
||||
else:
|
||||
@@ -501,11 +505,11 @@ class Connect4Bot(MultiBot, ABC):
|
||||
|
||||
if not is_active or current_player.id != presser_id or board[0][move_column] is not None:
|
||||
return
|
||||
message.buttons_info.data['is_active'] = False
|
||||
connect_4_data['is_active'] = False
|
||||
|
||||
i, j = self.insert_piece(move_column, current_player.number, board)
|
||||
message.buttons_info.data['turn'] += 1
|
||||
if await self._check_game_finished(i, j, player_1, player_2, message.buttons_info.data['turn'], board, message):
|
||||
connect_4_data['turn'] += 1
|
||||
if await self._check_game_finished(i, j, player_1, player_2, connect_4_data['turn'], board, message):
|
||||
return
|
||||
|
||||
await self.edit(
|
||||
@@ -519,20 +523,20 @@ class Connect4Bot(MultiBot, ABC):
|
||||
)
|
||||
|
||||
if player_2.id == self.id:
|
||||
message.buttons_info.data['turn'] += 1
|
||||
connect_4_data['turn'] += 1
|
||||
if await self._ai_turn(
|
||||
player_1,
|
||||
player_2,
|
||||
next_player,
|
||||
current_player,
|
||||
message.buttons_info.data['turn'],
|
||||
connect_4_data['turn'],
|
||||
constants.CONNECT_4_AI_DELAY_SECONDS,
|
||||
board,
|
||||
message
|
||||
):
|
||||
return
|
||||
|
||||
message.buttons_info.data['is_active'] = True
|
||||
connect_4_data['is_active'] = True
|
||||
|
||||
async def _on_connect_4_vs_itself(self, message: Message):
|
||||
if message.chat.is_group and not self.is_bot_mentioned(message):
|
||||
|
||||
@@ -274,13 +274,13 @@ class FlanaBot(Connect4Bot, PenaltyBot, PollBot, ScraperBot, WeatherBot, MultiBo
|
||||
self.distribute_buttons(options, vertically=True),
|
||||
message,
|
||||
buttons_key=ButtonsGroup.ROLES,
|
||||
buttons_data={'user_id': message.author.id}
|
||||
data={'user_id': message.author.id}
|
||||
)
|
||||
await self.delete_message(message)
|
||||
|
||||
async def _on_roles_button_press(self, message: Message):
|
||||
await self.accept_button_event(message)
|
||||
if message.buttons_info.presser_user.id != message.buttons_info.data['user_id']:
|
||||
if message.buttons_info.presser_user.id != message.data['user_id']:
|
||||
return
|
||||
|
||||
role = await self.find_role(message.buttons_info.pressed_text[1:].strip(), message)
|
||||
|
||||
@@ -70,19 +70,21 @@ class PollBot(MultiBot, ABC):
|
||||
return poll_message
|
||||
|
||||
async def _update_poll_buttons(self, message: Message):
|
||||
if message.buttons_info.data['is_multiple_answer']:
|
||||
total_votes = len({option_vote[0] for option_votes in message.buttons_info.data['votes'].values() if option_votes for option_vote in option_votes})
|
||||
poll_data = message.data['poll']
|
||||
|
||||
if poll_data['is_multiple_answer']:
|
||||
total_votes = len({option_vote[0] for option_votes in poll_data['votes'].values() if option_votes for option_vote in option_votes})
|
||||
else:
|
||||
total_votes = sum(len(option_votes) for option_votes in message.buttons_info.data['votes'].values())
|
||||
total_votes = sum(len(option_votes) for option_votes in poll_data['votes'].values())
|
||||
|
||||
if total_votes:
|
||||
buttons = []
|
||||
for option, option_votes in message.buttons_info.data['votes'].items():
|
||||
for option, option_votes in poll_data['votes'].items():
|
||||
ratio = f'{len(option_votes)}/{total_votes}'
|
||||
names = f"({', '.join(option_vote[1] for option_vote in option_votes)})" if option_votes else ''
|
||||
buttons.append(f'{option} ➜ {ratio} {names}')
|
||||
else:
|
||||
buttons = list(message.buttons_info.data['votes'].keys())
|
||||
buttons = list(poll_data['votes'].keys())
|
||||
|
||||
await self.edit(self.distribute_buttons(buttons, vertically=True), message)
|
||||
|
||||
@@ -131,13 +133,15 @@ class PollBot(MultiBot, ABC):
|
||||
if not (poll_message := self._get_poll_message(message)):
|
||||
return
|
||||
|
||||
poll_data = poll_message.data['poll']
|
||||
|
||||
if all_:
|
||||
for option_name, option_votes in poll_message.buttons_info.data['votes'].items():
|
||||
poll_message.buttons_info.data['votes'][option_name].clear()
|
||||
for option_name, option_votes in poll_data['votes'].items():
|
||||
poll_data['votes'][option_name].clear()
|
||||
else:
|
||||
for user in await self._find_users_to_punish(message):
|
||||
for option_name, option_votes in poll_message.buttons_info.data['votes'].items():
|
||||
poll_message.buttons_info.data['votes'][option_name] = [option_vote for option_vote in option_votes if option_vote[0] != user.id]
|
||||
for option_name, option_votes in poll_data['votes'].items():
|
||||
poll_data['votes'][option_name] = [option_vote for option_vote in option_votes if option_vote[0] != user.id]
|
||||
|
||||
await self.delete_message(message)
|
||||
await self._update_poll_buttons(poll_message)
|
||||
@@ -162,11 +166,13 @@ class PollBot(MultiBot, ABC):
|
||||
self.distribute_buttons(final_options, vertically=True),
|
||||
message,
|
||||
buttons_key=ButtonsGroup.POLL,
|
||||
buttons_data={
|
||||
'is_active': True,
|
||||
'is_multiple_answer': is_multiple_answer,
|
||||
'votes': {option: [] for option in final_options},
|
||||
'banned_users_tries': {}
|
||||
data={
|
||||
'poll': {
|
||||
'is_active': True,
|
||||
'is_multiple_answer': is_multiple_answer,
|
||||
'votes': {option: [] for option in final_options},
|
||||
'banned_users_tries': {}
|
||||
}
|
||||
}
|
||||
)
|
||||
else:
|
||||
@@ -176,39 +182,42 @@ class PollBot(MultiBot, ABC):
|
||||
|
||||
async def _on_poll_button_press(self, message: Message):
|
||||
await self.accept_button_event(message)
|
||||
if not message.buttons_info.data['is_active']:
|
||||
|
||||
poll_data = message.data['poll']
|
||||
|
||||
if not poll_data['is_active']:
|
||||
return
|
||||
|
||||
presser_id = message.buttons_info.presser_user.id
|
||||
presser_name = message.buttons_info.presser_user.name.split('#')[0]
|
||||
if (presser_id_str := str(presser_id)) in message.buttons_info.data['banned_users_tries']:
|
||||
message.buttons_info.data['banned_users_tries'][presser_id_str] += 1
|
||||
if message.buttons_info.data['banned_users_tries'][presser_id_str] == 3:
|
||||
if (presser_id_str := str(presser_id)) in poll_data['banned_users_tries']:
|
||||
poll_data['banned_users_tries'][presser_id_str] += 1
|
||||
if poll_data['banned_users_tries'][presser_id_str] == 3:
|
||||
await self.send(random.choice((
|
||||
f'Deja de dar por culo {presser_name} que no puedes votar aqui',
|
||||
f'No es pesao {presser_name}, que no tienes permitido votar aqui',
|
||||
f'Deja de pulsar botones que no puedes votar aqui {presser_name}',
|
||||
f'{presser_name} deja de intentar votar aqui que no puedes',
|
||||
f'Te han prohibido votar aquì {presser_name}.',
|
||||
f'Te han prohibido votar aquí {presser_name}.',
|
||||
f'No puedes votar aquí, {presser_name}.'
|
||||
)), reply_to=message)
|
||||
return
|
||||
|
||||
option_name = results[0] if (results := re.findall('(.*?) ➜.+', message.buttons_info.pressed_text)) else message.buttons_info.pressed_text
|
||||
selected_option_votes = message.buttons_info.data['votes'][option_name]
|
||||
selected_option_votes = poll_data['votes'][option_name]
|
||||
|
||||
if (presser_id, presser_name) in selected_option_votes:
|
||||
selected_option_votes.remove((presser_id, presser_name))
|
||||
if [presser_id, presser_name] in selected_option_votes:
|
||||
selected_option_votes.remove([presser_id, presser_name])
|
||||
else:
|
||||
if not message.buttons_info.data['is_multiple_answer']:
|
||||
for option_votes in message.buttons_info.data['votes'].values():
|
||||
if not poll_data['is_multiple_answer']:
|
||||
for option_votes in poll_data['votes'].values():
|
||||
try:
|
||||
option_votes.remove((presser_id, presser_name))
|
||||
option_votes.remove([presser_id, presser_name])
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
break
|
||||
selected_option_votes.append((presser_id, presser_name))
|
||||
selected_option_votes.append([presser_id, presser_name])
|
||||
|
||||
await self._update_poll_buttons(message)
|
||||
|
||||
@@ -221,7 +230,7 @@ class PollBot(MultiBot, ABC):
|
||||
|
||||
winners = []
|
||||
max_votes = 1
|
||||
for option, votes in poll_message.buttons_info.data['votes'].items():
|
||||
for option, votes in poll_message.data['poll']['votes'].items():
|
||||
if len(votes) > max_votes:
|
||||
winners = [option]
|
||||
max_votes = len(votes)
|
||||
@@ -237,7 +246,7 @@ class PollBot(MultiBot, ABC):
|
||||
case _:
|
||||
text = 'Encuesta finalizada.'
|
||||
|
||||
poll_message.buttons_info.data['is_active'] = False
|
||||
poll_message.data['poll']['is_active'] = False
|
||||
|
||||
await self.edit(text, poll_message)
|
||||
|
||||
@@ -249,8 +258,8 @@ class PollBot(MultiBot, ABC):
|
||||
await self.delete_message(message)
|
||||
|
||||
for user in await self._find_users_to_punish(message):
|
||||
if str(user.id) not in poll_message.buttons_info.data['banned_users_tries']:
|
||||
poll_message.buttons_info.data['banned_users_tries'][str(user.id)] = 0
|
||||
if str(user.id) not in poll_message.data['poll']['banned_users_tries']:
|
||||
poll_message.data['poll']['banned_users_tries'][str(user.id)] = 0
|
||||
|
||||
@admin(send_negative=True)
|
||||
async def _on_voting_unban(self, message: Message):
|
||||
@@ -261,7 +270,7 @@ class PollBot(MultiBot, ABC):
|
||||
|
||||
for user in await self._find_users_to_punish(message):
|
||||
try:
|
||||
del poll_message.buttons_info.data['banned_users_tries'][str(user.id)]
|
||||
del poll_message.data['poll']['banned_users_tries'][str(user.id)]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
|
||||
@@ -151,7 +151,7 @@ class WeatherBot(MultiBot, ABC):
|
||||
],
|
||||
message,
|
||||
buttons_key=ButtonsGroup.WEATHER,
|
||||
buttons_data={'weather_chart': weather_chart},
|
||||
data={'weather_chart': weather_chart},
|
||||
send_as_file=False
|
||||
)
|
||||
await self.send_inline_results(message)
|
||||
@@ -166,7 +166,7 @@ class WeatherBot(MultiBot, ABC):
|
||||
async def _on_weather_button_press(self, message: Message):
|
||||
await self.accept_button_event(message)
|
||||
|
||||
weather_chart = message.buttons_info.data['weather_chart']
|
||||
weather_chart = message.data['weather_chart']
|
||||
|
||||
match message.buttons_info.pressed_text:
|
||||
case WeatherEmoji.ZOOM_IN.value:
|
||||
|
||||
Reference in New Issue
Block a user