From 3c8eb7bb703c10a554042019c146c2c72a019997 Mon Sep 17 00:00:00 2001 From: Minidodo Date: Wed, 1 Sep 2021 14:33:33 +0200 Subject: [PATCH] polls get sent to all members on logon dirty fix for the MA nanolines There's an error with the urllib, downgrading the dependency. --- .../accounting/services/account_service.py | 23 ++++++++++++++---- modules/standard/nano/nano_controller.py | 5 +++- modules/standard/poll/poll_controller.py | 24 +++++++++++-------- requirements.txt | 1 + 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/modules/core/accounting/services/account_service.py b/modules/core/accounting/services/account_service.py index 0603aab..9e0040f 100644 --- a/modules/core/accounting/services/account_service.py +++ b/modules/core/accounting/services/account_service.py @@ -305,10 +305,23 @@ class AccountService: return ["success", True] def get_orgs(self) -> list: - try: - return [x["org_id"] for x in self.db.query("SELECT * from orgs", [])] - except SqlException: - return [self.bot.public_channel_service.org_id] + # With the broader usage of the "simple_checks" (Check for ban, org membership, ....) + # We might need to cache the orgs matching the filters. + # As these dont change as frequently as the account [if at all], + # Saving them for longer should be safe. + if not hasattr(self, "orgs"): + try: + def del_orgs(_): + del self.orgs + self.orgs = [x["org_id"] for x in self.db.query("SELECT * from orgs", [])] + self.bot.job_scheduler.delayed_job(del_orgs, 60) + return self.orgs + # The table "orgs" does not exist, which means the bot is not running the onlinebot modules. + # Cache lifetime is infinity, as the bot usually wont leave its org while its online. + except SqlException: + self.orgs = [self.bot.public_channel_service.org_id] + return self.orgs + return self.orgs def create_users(self, users, disable=False) -> int: # Default preferences @@ -530,6 +543,8 @@ class AccountService: points = -points self.db.exec("UPDATE account set points = points-? where char_id=(select main from account where char_id=?)", [points, char_id]) + if account := self.acc_cache.get(char_id, None): + account.points -= points self.add_log(char_id, 'points', reason, leader, delta=-points) def add_rank(self, char_id, rank) -> int: diff --git a/modules/standard/nano/nano_controller.py b/modules/standard/nano/nano_controller.py index 77b9e3d..4536891 100644 --- a/modules/standard/nano/nano_controller.py +++ b/modules/standard/nano/nano_controller.py @@ -158,8 +158,11 @@ class NanoController: @command(command="nanolines", params=[Any("profession"), Any("nanoline")], access_level="member", description="Show nanos by nanoline profession and line name") - def nanolines_prof_strain(self, _, prof, strain): + def nanolines_prof_strain(self, _, prof: str, strain: str): profession = self.util.get_profession(prof) + if strain.startswith("Artist"): + profession = "Martial Artist" + strain = strain[7:] nanoline = self.db.query_single( "SELECT * FROM nanos n WHERE n.strain LIKE ? AND profession =? and type='Crystal' LIMIT 1", [strain, profession]) diff --git a/modules/standard/poll/poll_controller.py b/modules/standard/poll/poll_controller.py index 9c96635..03769dc 100644 --- a/modules/standard/poll/poll_controller.py +++ b/modules/standard/poll/poll_controller.py @@ -3,7 +3,7 @@ import time from core.chat_blob import ChatBlob from core.command_param_types import Const, Any, Int from core.decorators import instance, command, event -from modules.orgbot.org.org_roster_controller import OrgRosterController +from modules.core.accounting.services.account_service import AccountService @instance() @@ -16,7 +16,7 @@ class PollController: self.job_scheduler = registry.get_instance("job_scheduler") self.pork_service = registry.get_instance("pork_service") self.command_alias_service = registry.get_instance("command_alias_service") - self.account_service = registry.get_instance("account_service") + self.account_service: AccountService = registry.get_instance("account_service") def start(self): self.db.exec("CREATE TABLE IF NOT EXISTS poll (" @@ -155,16 +155,20 @@ class PollController: self.check_for_finished_polls() self.create_scheduled_jobs_for_polls() - @event(event_type=OrgRosterController.ORG_MEMBER_LOGON_EVENT, + @event(event_type="member_logon", description="Send active polls to org members logging on") - def org_member_logon_event(self, event_type, event_data): + def poll_on_logon(self, event_type, event_data): if self.bot.is_ready(): - data = self.db.query("SELECT * FROM poll WHERE is_finished != 1 AND " - "id NOT IN (SELECT poll_id FROM poll_vote WHERE char_id = ?) " - "ORDER BY finished_at, id", [event_data.account.char_id]) - if data: - row = data[0] - self.bot.send_private_message(event_data.char_id, self.show_poll_details_blob(row)) + # Only proceed if the "normal" checks are fine + if not self.account_service.simple_checks(event_data.account): + return + if event_data.account.news_spam == 1: + data = self.db.query("SELECT * FROM poll WHERE is_finished != 1 AND " + "id NOT IN (SELECT poll_id FROM poll_vote WHERE char_id = ?) " + "ORDER BY finished_at, id", [event_data.account.char_id]) + if data: + row = data[0] + self.bot.send_private_message(event_data.char_info.char_id, self.show_poll_details_blob(row)) def create_scheduled_jobs_for_polls(self): data = self.db.query("SELECT * FROM poll WHERE is_finished != 1") diff --git a/requirements.txt b/requirements.txt index d19833a..b01b756 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,4 @@ websock~=1.0.4 pip>=21.2.4 websockets~=9.1 torpy~=1.1.6 +urllib3==1.25.11