This commit is contained in:
2024-09-03 18:09:44 +02:00
parent a74a278fa8
commit 86539ff62f
6 changed files with 21 additions and 24 deletions
+13 -15
View File
@@ -1,4 +1,6 @@
from json import dump, load from json import dumps, loads
import redis
from config import REDIS_URL
class User: class User:
def __init__(self, balance: int) -> None: def __init__(self, balance: int) -> None:
@@ -20,31 +22,27 @@ class User:
class Aura: class Aura:
data: dict[str, User] = {} data: dict[str, User] = {}
file = '' def __init__(self) -> None:
def __init__(self, file: str) -> None: redis_host = REDIS_URL.split(':')
with open(file, 'r')as f: self.r = redis.Redis(host=redis_host[0], port=int(redis_host[1]), decode_responses=True)
self.file = file self.load()
data = load(f)
for k in data:
self.data[k] = User(data[k]['balance'])
def load(self) -> None: def load(self) -> None:
with open(self.file, 'r')as f: data = loads(self.r.get('data'))
data = load(f) for k in data:
for k in data: self.data[k] = User(data[k]['balance'])
self.data[k] = User(data[k]['balance'])
def get(self, user_id) -> User | None: def get(self, user_id) -> User | None:
if user_id in self.data: if user_id in self.data:
return self.data[user_id] return self.r.get(str(user_id))
def add(self, user_id) -> User: def add(self, user_id) -> User:
self.data[user_id] = User(100) self.data[user_id] = User(100)
self.r.set
return self.data[user_id] return self.data[user_id]
def save(self): def save(self):
data = {} data = {}
for k in self.data: for k in self.data:
data[k] = self.data[k].to_dict() data[k] = self.data[k].to_dict()
with open(self.file, 'w')as f: self.r.set('data', dumps(data, indent=2))
dump(data, f, indent=2)
+1 -1
View File
@@ -4,7 +4,7 @@ import discord
from discord import app_commands from discord import app_commands
from discord.ext import commands from discord.ext import commands
aura = Aura('aura.json') aura = Aura()
client = discord.Client(intents=discord.Intents.all()) client = discord.Client(intents=discord.Intents.all())
# bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) # bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
tree = app_commands.CommandTree(client) tree = app_commands.CommandTree(client)
+2
View File
@@ -2,5 +2,7 @@ import os
BOT_TOKEN=os.environ['BOT_TOKEN'] BOT_TOKEN=os.environ['BOT_TOKEN']
USE_INVITE_SERVER=os.environ['USE_INVITE']=='true' USE_INVITE_SERVER=os.environ['USE_INVITE']=='true'
INVITE_SERVER_URL='127.0.0.1' INVITE_SERVER_URL='127.0.0.1'
INVITE_SERVER_PORT=os.environ['PORT']
INVITE=os.environ['INVITE'] INVITE=os.environ['INVITE']
PUNISH_ROLES=(1279588925257683014,) PUNISH_ROLES=(1279588925257683014,)
REDIS_URL=os.environ['REDIS_URL']
+1 -1
View File
@@ -15,4 +15,4 @@ def root(id):
aura.save() aura.save()
return redirect(INVITE, code=302) return redirect(INVITE, code=302)
app.run('127.0.0.1', environ['PORT']) app.run('127.0.0.1', INVITE_SERVER_PORT)
-4
View File
@@ -1,6 +1,5 @@
import os import os
env = os.environ.copy() env = os.environ.copy()
# import install
from config import * from config import *
from subprocess import run as run_start from subprocess import run as run_start
from threading import Thread from threading import Thread
@@ -15,6 +14,3 @@ if USE_INVITE_SERVER:t1.start()
t2.start() t2.start()
while 1: while 1:
time.sleep(.5) time.sleep(.5)
# # if input() == 'q':
# if USE_INVITE_SERVER:t1.join(0)
# t2.join(0)
+1
View File
@@ -1,2 +1,3 @@
Flask Flask
discord.py discord.py
redis