lunarflu HF staff commited on
Commit
ce27741
1 Parent(s): 96097d8

persistent storage test

Browse files
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -14,9 +14,10 @@ intents = discord.Intents.all()
14
  bot = commands.Bot(command_prefix='!', intents=intents)
15
 
16
 
17
-
18
  """"""
19
  XP_PER_MESSAGE = 10 # 100k messages = 1M exp = lvl 100
 
 
20
  """"""
21
 
22
 
@@ -24,21 +25,33 @@ XP_PER_MESSAGE = 10 # 100k messages = 1M exp = lvl 100
24
  async def on_ready():
25
  print(f'Logged in as {bot.user.name}')
26
  print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
27
- print(f"xp_data: {xp_data}")
28
-
 
29
 
 
30
  try:
31
  with open('xp_data.json', 'r') as f:
32
  xp_data = json.load(f)
33
  except FileNotFoundError:
34
  xp_data = {}
35
 
 
 
 
36
 
37
  def save_xp_data():
38
- with open('xp_data.json', 'w') as f:
39
  json.dump(xp_data, f)
40
 
41
 
 
 
 
 
 
 
 
42
  @bot.event
43
  async def on_message(message):
44
  try:
@@ -53,7 +66,8 @@ async def on_message(message):
53
 
54
  user = bot.get_user(811235357663297546)
55
  try:
56
- await user.send(f"xp_data: {xp_data}")
 
57
  except discord.HTTPException:
58
  await user.send("Failed to send a DM.")
59
 
 
14
  bot = commands.Bot(command_prefix='!', intents=intents)
15
 
16
 
 
17
  """"""
18
  XP_PER_MESSAGE = 10 # 100k messages = 1M exp = lvl 100
19
+ data_file_path = '/data/xp_data.json'
20
+ xp_data = {}
21
  """"""
22
 
23
 
 
25
  async def on_ready():
26
  print(f'Logged in as {bot.user.name}')
27
  print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
28
+ global xp_data
29
+ xp_data = load_xp_data()
30
+ print('XP data loaded:', xp_data)
31
 
32
+ """
33
  try:
34
  with open('xp_data.json', 'r') as f:
35
  xp_data = json.load(f)
36
  except FileNotFoundError:
37
  xp_data = {}
38
 
39
+ """
40
+
41
+
42
 
43
  def save_xp_data():
44
+ with open(data_file_path, 'w') as f:
45
  json.dump(xp_data, f)
46
 
47
 
48
+ def load_xp_data():
49
+ if os.path.exists(data_file_path):
50
+ with open(data_file_path, 'r') as f:
51
+ return json.load(f)
52
+ return {}
53
+
54
+
55
  @bot.event
56
  async def on_message(message):
57
  try:
 
66
 
67
  user = bot.get_user(811235357663297546)
68
  try:
69
+ file = discord.File(data_file_path)
70
+ await user.send(f"xp_data: {xp_data}", file=file)
71
  except discord.HTTPException:
72
  await user.send("Failed to send a DM.")
73