Skip to content

Commit

Permalink
ridotto intervallo di ricerca delle proposte per controllo integrità
Browse files Browse the repository at this point in the history
  • Loading branch information
cathartyc committed Sep 17, 2023
1 parent 85dc34b commit 2e01f5b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions utils/proposals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Modulo di gestione delle proposte"""

from __future__ import annotations
from datetime import date, timedelta
from datetime import datetime, date, timedelta
import json
from typing import ClassVar, Dict, Optional, TypedDict

Expand Down Expand Up @@ -89,8 +89,10 @@ class Proposals():
Attributes
-------------
_instance: `Proposals` attributo di classe, contiene
l'istanza del wrapper
_instance: `Proposals` attributo di classe, contiene l'istanza
del wrapper
last_timestamp: `Date` data dell'ultima proposta, usata per il
controllo di integrità
Classmethods
-------------
Expand All @@ -110,6 +112,7 @@ class Proposals():

def __init__(self) -> None:
self.proposals: Dict[int, Proposal]
self.timestamp: date
raise RuntimeError(
'Usa Proposals.get_instance() per ottenere l\'istanza')

Expand Down Expand Up @@ -147,6 +150,10 @@ def load(cls) -> None:
) for i, p in raw_proposals.items()}
cls._instance = cls.__new__(cls)
cls._instance.proposals = proposals
try:
cls._instance.timestamp = date.fromisoformat(sorted(proposals.values(), key=lambda x: x.timestamp).pop().timestamp)
except IndexError:
cls._instance.timestamp = date.today()

def get_proposal(self, messsage_id: int) -> Optional[Proposal]:
"""Cerca una proposta dato l'id del messaggio ad essa correlato.
Expand Down Expand Up @@ -181,6 +188,8 @@ def add_proposal(self, message: discord.Message) -> None:
content=message.content
)
self.proposals[message.id] = proposal
if message.created_at.date() > self.timestamp:
self.timestamp = message.created_at.date()
self._save()

async def remove_proposal(self, message_id: int) -> None:
Expand Down Expand Up @@ -288,7 +297,7 @@ async def check_proposals_integrity(self) -> None:
le proposte nel canale.
"""
existing_proposals = set()
async for message in Config.get_config().poll_channel.history():
async for message in Config.get_config().poll_channel.history(after=datetime.combine(self.timestamp, datetime.min.time())):
if message.author.bot:
continue
existing_proposals.add(message.id)
Expand All @@ -299,6 +308,7 @@ async def check_proposals_integrity(self) -> None:
for wrong_react in message.reactions:
to_remove[wrong_react] = set()
async for member in wrong_react.users():
assert isinstance(member, discord.Member)
if not (wrong_react.emoji in ('🔴', '🟢') and
(Config.get_config().orator_role in member.roles or
any(role in Config.get_config().moderation_roles for role in member.roles))):
Expand Down

0 comments on commit 2e01f5b

Please sign in to comment.