diff options
author | 2020-10-14 18:25:50 +0530 | |
---|---|---|
committer | 2020-10-14 18:25:50 +0530 | |
commit | 7f7e377aab20cebf0fcd8fa37a28d4e99d8330fb (patch) | |
tree | 43d011e54f547dd3e46970f987f7d8b0a12c37c5 /bot | |
parent | Remove redundant function ten_recent_msg (diff) |
Use persistant storage for candy scores
Diffstat (limited to 'bot')
-rw-r--r-- | bot/exts/halloween/candy_collection.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py index 8afababf..78fee586 100644 --- a/bot/exts/halloween/candy_collection.py +++ b/bot/exts/halloween/candy_collection.py @@ -1,7 +1,7 @@ import json import logging -import os import random +from pathlib import Path from typing import Union import discord @@ -9,11 +9,10 @@ from discord.ext import commands from bot.constants import Channels, Month from bot.utils.decorators import in_month +from bot.utils.persist import make_persistent log = logging.getLogger(__name__) -json_location = os.path.join("bot", "resources", "halloween", "candy_collection.json") - # chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy) ADD_CANDY_REACTION_CHANCE = 20 # 5% ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10% @@ -38,8 +37,10 @@ class CandyCollection(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot - with open(json_location, encoding="utf8") as candy: - candy_data = json.load(candy) + self.json_file = make_persistent(Path("bot", "resources", "halloween", "candy_collection.json")) + + with self.json_file.open() as fp: + candy_data = json.load(fp) # The rank data self.candy_records = candy_data.get("records", dict()) @@ -145,8 +146,8 @@ class CandyCollection(commands.Cog): def save_to_json(self) -> None: """Save JSON to a local file.""" - with open(json_location, 'w', encoding="utf8") as outfile: - json.dump(dict(records=self.candy_records), outfile) + with self.json_file.open('w') as fp: + json.dump(dict(records=self.candy_records), fp) @in_month(Month.OCTOBER) @commands.command() |