Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,31 +24,29 @@ async def on_ready():
|
|
| 24 |
print(f'Logged in as {bot.user.name}')
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
xp_data
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
def save_xp_data():
|
| 35 |
with open('xp_data.json', 'w') as f:
|
| 36 |
json.dump(xp_data, f)
|
| 37 |
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
@bot.event
|
| 42 |
async def on_message(message):
|
| 43 |
try:
|
| 44 |
#if message.author != bot.user: # disabling this means we can track bot usage over time
|
| 45 |
"""AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)"""
|
| 46 |
author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP)
|
| 47 |
-
xp_data.setdefault(author_id,
|
| 48 |
-
|
| 49 |
-
xp_data[author_id]
|
| 50 |
-
save_xp_data()
|
| 51 |
-
|
| 52 |
await bot.process_commands(message)
|
| 53 |
|
| 54 |
except Exception as e:
|
|
@@ -64,9 +62,9 @@ def calculate_level(xp):
|
|
| 64 |
async def level(ctx):
|
| 65 |
author_id = str(ctx.author.id)
|
| 66 |
if author_id in xp_data:
|
| 67 |
-
|
| 68 |
-
level = calculate_level(
|
| 69 |
-
await ctx.send(f'You are at level {level} with {
|
| 70 |
else:
|
| 71 |
await ctx.send('You have not earned any XP yet.')
|
| 72 |
|
|
|
|
| 24 |
print(f'Logged in as {bot.user.name}')
|
| 25 |
|
| 26 |
|
| 27 |
+
def load_xp_data():
|
| 28 |
+
try:
|
| 29 |
+
with open('xp_data.json', 'r') as f:
|
| 30 |
+
return json.load(f)
|
| 31 |
+
except FileNotFoundError:
|
| 32 |
+
return {}
|
| 33 |
+
|
| 34 |
|
| 35 |
def save_xp_data():
|
| 36 |
with open('xp_data.json', 'w') as f:
|
| 37 |
json.dump(xp_data, f)
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
| 40 |
@bot.event
|
| 41 |
async def on_message(message):
|
| 42 |
try:
|
| 43 |
#if message.author != bot.user: # disabling this means we can track bot usage over time
|
| 44 |
"""AWAIT LEVEL ALGORITM OR SOMETHING (MULTIPLE FILES?)"""
|
| 45 |
author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP)
|
| 46 |
+
xp_data.setdefault(author_id, [])
|
| 47 |
+
timestamp = int(time.time())
|
| 48 |
+
xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
|
| 49 |
+
save_xp_data(xp_data)
|
|
|
|
| 50 |
await bot.process_commands(message)
|
| 51 |
|
| 52 |
except Exception as e:
|
|
|
|
| 62 |
async def level(ctx):
|
| 63 |
author_id = str(ctx.author.id)
|
| 64 |
if author_id in xp_data:
|
| 65 |
+
total_xp = sum(xp for _, xp in xp_data[author_id])
|
| 66 |
+
level = calculate_level(total_xp)
|
| 67 |
+
await ctx.send(f'You are at level {level} with {total_xp} total XP.')
|
| 68 |
else:
|
| 69 |
await ctx.send('You have not earned any XP yet.')
|
| 70 |
|