lunarflu HF staff commited on
Commit
714224b
1 Parent(s): 6c5f294

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -17,6 +17,8 @@ bot = commands.Bot(command_prefix='!', intents=intents)
17
 
18
  XP_PER_MESSAGE = 10
19
 
 
 
20
 
21
  def load_xp_data():
22
  try:
@@ -38,18 +40,24 @@ async def on_ready():
38
 
39
  @bot.event
40
  async def on_message(message):
 
 
41
  if message.author.bot:
42
  return
43
 
44
- author_id = str(message.author.id)
45
-
46
- xp_data.setdefault(author_id, [])
47
- timestamp = int(time.time())
48
- xp_data[author_id].append((timestamp, XP_PER_MESSAGE))
 
 
49
 
 
 
50
  save_xp_data(xp_data)
51
 
52
- await bot.process_commands(message)
53
 
54
 
55
  def calculate_level(xp):
@@ -58,12 +66,14 @@ def calculate_level(xp):
58
 
59
  @bot.command()
60
  async def level(ctx):
 
61
  if ctx.author.id == 811235357663297546: # lunarflu
62
- author_id = str(ctx.author.id)
63
- if author_id in xp_data:
64
- total_xp = sum(xp for _, xp in xp_data[author_id])
65
- level = calculate_level(total_xp)
66
- await ctx.send(f'You are at level {level} with {total_xp} total XP.')
 
67
  # progress bar with ascii? could be nice, easy
68
  else:
69
  await ctx.send('You have not earned any XP yet.')
 
17
 
18
  XP_PER_MESSAGE = 10
19
 
20
+ xp_data = {}
21
+
22
 
23
  def load_xp_data():
24
  try:
 
40
 
41
  @bot.event
42
  async def on_message(message):
43
+ global xp_data
44
+
45
  if message.author.bot:
46
  return
47
 
48
+ if message.author.id not in xp_data:
49
+ xp_data[message.author.id] = 0
50
+
51
+ old = xp_data[message.author.id]
52
+ new = old + XP_PER_MESSAGE
53
+ xp_data[message.author.id] = new
54
+ level = calculate_level(new)
55
 
56
+ print(f"{message.author.mention} xp: {xp_data[message.author.id]}")
57
+ print(f"{message.author.mention} level: {level}")
58
  save_xp_data(xp_data)
59
 
60
+
61
 
62
 
63
  def calculate_level(xp):
 
66
 
67
  @bot.command()
68
  async def level(ctx):
69
+ global xp_data
70
  if ctx.author.id == 811235357663297546: # lunarflu
71
+ if ctx.author.id in xp_data:
72
+
73
+ xp = xp_data[ctx.author.id]
74
+ level = calculate_level(xp)
75
+
76
+ await ctx.send(f'You are at level {level} with {xp} total XP.')
77
  # progress bar with ascii? could be nice, easy
78
  else:
79
  await ctx.send('You have not earned any XP yet.')