diff --git a/aura.py b/aura.py index 6ae567f..599a167 100644 --- a/aura.py +++ b/aura.py @@ -1,4 +1,6 @@ -from json import dump, load +from json import dumps, loads +import redis +from config import REDIS_URL class User: def __init__(self, balance: int) -> None: @@ -20,31 +22,27 @@ class User: class Aura: data: dict[str, User] = {} - file = '' - def __init__(self, file: str) -> None: - with open(file, 'r')as f: - self.file = file - data = load(f) - for k in data: - self.data[k] = User(data[k]['balance']) + def __init__(self) -> None: + redis_host = REDIS_URL.split(':') + self.r = redis.Redis(host=redis_host[0], port=int(redis_host[1]), decode_responses=True) + self.load() def load(self) -> None: - with open(self.file, 'r')as f: - data = load(f) - for k in data: - self.data[k] = User(data[k]['balance']) + data = loads(self.r.get('data')) + for k in data: + self.data[k] = User(data[k]['balance']) def get(self, user_id) -> User | None: if user_id in self.data: - return self.data[user_id] + return self.r.get(str(user_id)) def add(self, user_id) -> User: self.data[user_id] = User(100) + self.r.set return self.data[user_id] def save(self): data = {} for k in self.data: data[k] = self.data[k].to_dict() - with open(self.file, 'w')as f: - dump(data, f, indent=2) \ No newline at end of file + self.r.set('data', dumps(data, indent=2)) \ No newline at end of file diff --git a/bot.py b/bot.py index 502379c..1bfe0d2 100644 --- a/bot.py +++ b/bot.py @@ -4,7 +4,7 @@ import discord from discord import app_commands from discord.ext import commands -aura = Aura('aura.json') +aura = Aura() client = discord.Client(intents=discord.Intents.all()) # bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) tree = app_commands.CommandTree(client) diff --git a/config.py b/config.py index 387b6a9..674b7ff 100644 --- a/config.py +++ b/config.py @@ -2,5 +2,7 @@ import os BOT_TOKEN=os.environ['BOT_TOKEN'] USE_INVITE_SERVER=os.environ['USE_INVITE']=='true' INVITE_SERVER_URL='127.0.0.1' +INVITE_SERVER_PORT=os.environ['PORT'] INVITE=os.environ['INVITE'] -PUNISH_ROLES=(1279588925257683014,) \ No newline at end of file +PUNISH_ROLES=(1279588925257683014,) +REDIS_URL=os.environ['REDIS_URL'] \ No newline at end of file diff --git a/invite_sys.py b/invite_sys.py index 2d37086..6ef14be 100644 --- a/invite_sys.py +++ b/invite_sys.py @@ -15,4 +15,4 @@ def root(id): aura.save() return redirect(INVITE, code=302) -app.run('127.0.0.1', environ['PORT']) \ No newline at end of file +app.run('127.0.0.1', INVITE_SERVER_PORT) \ No newline at end of file diff --git a/main.py b/main.py index ddf1123..40d334a 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,5 @@ import os env = os.environ.copy() -# import install from config import * from subprocess import run as run_start from threading import Thread @@ -14,7 +13,4 @@ t2.daemon = True if USE_INVITE_SERVER:t1.start() t2.start() while 1: - time.sleep(.5) -# # if input() == 'q': -# if USE_INVITE_SERVER:t1.join(0) -# t2.join(0) \ No newline at end of file + time.sleep(.5) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b630490..091c609 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask -discord.py \ No newline at end of file +discord.py +redis \ No newline at end of file