diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index e9f8519..7063e37 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -26,10 +26,15 @@ def create_app(test_config: None = None) -> Flask: app.config.from_pyfile('redeems.py', silent=True) # set up polls if they're enabled - if app.config['POLLS']: + if app.config['POLLS_ENABLED']: app.config.from_object('tlapbot.defaults.default_polls') app.config.from_pyfile('polls.py', silent=True) + for poll in app.config['POLLS']: + if ' ' in poll: + app.logger.warning(f"Poll '{poll}' has spaces in its name.") + app.logger.warning("Poll with spaces are impossible to redeem.") + # Make logging work for gunicorn-ran instances of tlapbot. if app.config['GUNICORN']: gunicorn_logger = logging.getLogger('gunicorn.error') diff --git a/tlapbot/db.py b/tlapbot/db.py index d90eca8..3335af8 100644 --- a/tlapbot/db.py +++ b/tlapbot/db.py @@ -92,12 +92,24 @@ def refresh_counters() -> bool: return insert_counters(db) +def populate_poll_options(db: sqlite3.Connection, poll: str) -> bool: + for option in current_app.config['POLLS'][poll]['options']: + db.execute( + "INSERT INTO polls(poll_name, points, option) VALUES(?, ?, ?)", + (poll, 0, option) + ) + db.commit() + return True + + def refresh_polls() -> bool: db = get_db() - try: db.execute("DELETE FROM polls") db.commit() + for poll in current_app.config['POLLS']: + populate_poll_options(db, poll) + db.commit() except sqlite3.Error as e: print("Error occurred deleting old counters:", e.args[0]) return False @@ -114,12 +126,7 @@ def reset_poll(poll: str) -> bool: "DELETE FROM polls WHERE poll_name = ?", (poll,) ) - for option in current_app.config['POLLS'][poll]: - db.execute( - "INSERT INTO polls(poll_name, points, option) VALUES(?, ?, ?)", - (poll, 0, option) - ) - db.commit() + populate_poll_options(db, poll) return True except sqlite3.Error as e: current_app.logger.error(f"Error occurred resetting poll: {e.args[0]}") diff --git a/tlapbot/defaults/default_config.py b/tlapbot/defaults/default_config.py index 72103b6..f28e1bc 100644 --- a/tlapbot/defaults/default_config.py +++ b/tlapbot/defaults/default_config.py @@ -8,4 +8,4 @@ LIST_REDEEMS=False ACTIVE_CATEGORIES=[] GUNICORN=False PREFIX='!' -POLLS=True \ No newline at end of file +POLLS_ENABLED=True \ No newline at end of file diff --git a/tlapbot/poll_handler.py b/tlapbot/poll_handler.py index 77f4ff1..d91e04e 100644 --- a/tlapbot/poll_handler.py +++ b/tlapbot/poll_handler.py @@ -25,6 +25,7 @@ def handle_poll_vote(message: str, user_id: str) -> None: if not user_points: send_chat(f"Can't vote in {poll_name} poll, failed to read users' points.") return + poll_points = int(poll_points) if user_points < poll_points: send_chat(f"Can't vote in {poll_name} poll, you're voting with more points than you have.") if (vote_in_poll(db, poll_name, option, poll_points) and diff --git a/tlapbot/redeems/polls.py b/tlapbot/redeems/polls.py index 3734776..7af29d4 100644 --- a/tlapbot/redeems/polls.py +++ b/tlapbot/redeems/polls.py @@ -28,11 +28,11 @@ def vote_in_poll(db: Connection, poll_name: str, option: str, points: int) -> bo if poll_option_exists(db, poll_name, option): try: cursor = db.execute( - "UPDATE polls SET progress = progress + ? WHERE poll_name = ? and option = ?", + "UPDATE polls SET points = points + ? WHERE poll_name = ? and option = ?", (points, poll_name, option) ) db.commit() return True except Error as e: - current_app.logger.error(f"Error occurred updating milestone: {e.args[0]}") + current_app.logger.error(f"Error occurred updating poll: {e.args[0]}") return False