From 068411ce1e4f42770278dba1541fca553b000bab Mon Sep 17 00:00:00 2001 From: Kamal Curi Date: Mon, 3 Apr 2023 15:00:46 -0300 Subject: [PATCH] ADD: Persistent quote stack. --- README.md | 1 + motd | 2 +- settings/config.py | 3 +++ utils/commands.py | 11 ++++++++--- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0bf5a3d..f16bc53 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ You can also run neeble under [docker](https://www.docker.com/) with [docker-com - Make sure you are running a SQL server instance - Create a database named `neeble` - Set up your environment variables with `environment/template` (You may copy the template into a new file) +- Make a file `/opt/neeble/id.stack` (or define your own with `NEEBLE_STACK_FILE`), it must contain an empty list (`[]`) - Load environment variables with `source`: `$ source environment/template` diff --git a/motd b/motd index 9b27261..d0b9e0e 100644 --- a/motd +++ b/motd @@ -5,7 +5,7 @@ █ ▄ █ ▄▄▄█ ▄▄▄█ ▄ ██ █▄▄▄█ ▄▄▄█ █ █ █ █ █▄▄▄█ █▄▄▄█ █▄█ █ █ █▄▄▄ █▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█ -Neeble version: 2023.03.30 +Neeble version: 2023.04.03 Bot source can be found by users with the "Neeble Dev" role. Thank you! diff --git a/settings/config.py b/settings/config.py index 5ea20ff..9eb6d5d 100644 --- a/settings/config.py +++ b/settings/config.py @@ -31,6 +31,9 @@ SQLACHEMY = create_engine( # Define the log level LOGLEVEL = os.environ.get('LOGLEVEL', 'info').upper() +# Define the path for the quote ID stack file +QUOTE_STACK = os.environ.get('NEEBLE_STACK_FILE', '/opt/neeble/id.list') + # Logging custom config. LOGGING_CONFIG = { 'version': 1, diff --git a/utils/commands.py b/utils/commands.py index aaa536a..b9592cd 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -4,12 +4,13 @@ Bot commands. import logging import os +import json from random import choice from discord import Embed, Intents from discord.ext import commands -from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS +from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS, QUOTE_STACK from utils.database import (count_quotes, count_quotes_user, get_by_id, get_quote_contains, get_quotes, remove_quote, set_quote) @@ -21,8 +22,8 @@ from utils.weather import displayweather, getweatherdata client = commands.Bot(command_prefix='--', intents=Intents.all()) logger = logging.getLogger(__name__) -quote_id_stack = [] - +with open(QUOTE_STACK, mode='r') as f: + quote_id_stack = json.load(f) @client.command(aliases=['q']) async def quote(bot: object, *quote: str) -> str: @@ -74,6 +75,10 @@ async def random_quote(bot: object) -> str: if stack_len >= stack_limit: quote_id_stack.pop(0) + + # Writes to persistent stackfile + with open(QUOTE_STACK, mode='w') as f: + json.dump(quote_id_stack, f) try: # To image links.