lunarflu HF staff commited on
Commit
7ed4c18
β€’
1 Parent(s): a903c30

revert premerge

Browse files
Files changed (1) hide show
  1. app.py +33 -80
app.py CHANGED
@@ -100,7 +100,6 @@ def update_google_sheet():
100
  name = "levelbot"
101
  #worksheet = gspread_bot.open(name).sheet1
102
  global_df['discord_user_id'] = global_df['discord_user_id'].astype(str)
103
- global_df['discord_exp'] = global_df['discord_exp'].astype(str)
104
  # could just do this / format
105
  set_with_dataframe(worksheet, global_df)
106
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
@@ -128,7 +127,7 @@ processed_users = set()
128
 
129
  async def periodic_api_test(): # needs rewrite, can do same thing and interact with csv -> push to google sheets
130
  try:
131
- # this should be gspread type function (do not mix with discord at all
132
  global worksheet2_df
133
  # get
134
  for index, user in enumerate(worksheet2_df['hf_user_name']):
@@ -220,7 +219,6 @@ async def add_exp(member_id):
220
 
221
  print("test1")
222
  # discord_user_id column
223
- # iterate over items of first column (discord_user_id)
224
  for index, cell_value in global_df.iloc[:, 0].items():
225
  # print("test2")
226
  # remove L (read, so no need to replace)
@@ -241,79 +239,35 @@ async def add_exp(member_id):
241
 
242
  # increment the old experience value (better not to replace outright)
243
  old_xp = global_df.loc[index, 'discord_exp']
244
-
245
-
246
-
247
-
248
- # add to discord exp (this is input)
249
- # add discord exp and hub exp to total_exp
250
- # set level based on total_exp (this is output)
251
-
252
-
253
-
254
  # remove L (write, so we replace)
255
  old_xp = str(old_xp)
256
  if old_xp.startswith("L") and old_xp.endswith("L"):
257
  old_xp = old_xp[1:-1]
258
-
259
- # set old level; use this for more accurate logging and jumping multiple levels at once (for example, verifying)
260
- old_level = calculate_level(int(old_xp))
261
-
262
- # str -> int temporarily for adding
263
  new_xp = int(old_xp) + XP_PER_MESSAGE
264
-
265
- # ----------------------------------------------------
266
- total_exp = global_df.loc[index, 'total_exp']
267
- hub_xp = global_df.loc[index, 'hub_exp']
268
-
269
- total_exp = str(total_exp)
270
- hub_xp = str(hub_xp)
271
-
272
- if total_exp.startswith("L") and total_exp.endswith("L"):
273
- total_exp = total_exp[1:-1]
274
- if hub_xp.startswith("L") and hub_xp.endswith("L"):
275
- hub_xp = hub_xp[1:-1]
276
-
277
- old_total_xp = int(total_exp)
278
-
279
- # check if hub exp not empty
280
- if hub_xp.strip():
281
- total_exp = int(new_xp) + int(hub_xp)
282
- else:
283
- total_exp = int(new_xp)
284
-
285
- # ----------------------------------------------------
286
-
287
- # total v
288
- current_level = calculate_level(total_exp)
289
-
290
- # convert back to string + google sheet proofing
291
  new_xp = str(new_xp)
292
  if not new_xp.startswith("L") and not new_xp.endswith("L"):
293
  new_xp = f"L" + str(new_xp) + f"L"
294
- global_df.loc[index, 'discord_exp'] = new_xp # do not change column name
295
 
296
- # after
297
- total_exp = str(total_exp)
298
- if not total_exp.startswith("L") and not total_exp.endswith("L"):
299
- total_exp = f"L" + str(total_exp) + f"L"
300
-
301
  # add back to dataframe in memory after checking redundantly;
302
- if total_exp.startswith("L") and total_exp.endswith("L"):
303
  print("test5")
304
- global_df.loc[index, 'total_exp'] = total_exp # do not change column name
305
- print(f"Record for {member} updated from {old_total_xp} to {global_df.loc[index, 'total_exp']} (+{XP_PER_MESSAGE}) ")
306
 
307
  # level up
 
308
  verified_role = guild.get_role(900063512829755413)
309
 
310
  print(f"Current_level for {member}: {current_level}")
311
  if current_level >= 2 and current_level <=30:
312
  print("test6")
313
  current_role = lvls[current_level]
314
- if current_role not in member.roles: # if we need to level up / update role
315
 
316
- # finding leaderboard rank + excluding huggingfolks (still need exclusion)
317
  try:
318
  # cell_value = L1234567890L
319
  # make copy
@@ -321,12 +275,12 @@ async def add_exp(member_id):
321
  # do calculations
322
  copy_df = global_df.copy()
323
  copy_df['discord_user_id'] = copy_df['discord_user_id'].str.strip('L').astype(str)
324
- copy_df['total_exp'] = copy_df['total_exp'].str.strip('L').astype(int)
325
  row = copy_df[copy_df['discord_user_id'] == str(member_id)]
326
  print(f"Row found for {member_id}!")
327
- target_exp = row['total_exp'].values[0]
328
- rank = (copy_df['total_exp'] > target_exp).sum() + 1
329
- print(f"The rank for discord_id {member_id} based on total_exp is: {rank}")
330
  except Exception as e:
331
  print(f"Discord ID {member_id} not found in the DataFrame.")
332
  rank = "πŸ€—"
@@ -337,15 +291,15 @@ async def add_exp(member_id):
337
  if verified_role not in member.roles:
338
 
339
 
340
- # L12345L -> `12345` -> 12345
341
- total_exp = total_exp[1:-1]
342
- total_exp = int(total_exp)
343
 
344
- if total_exp % 10 == 0: # staggers messages so we don't send one every time exp is earned
345
  # claim exp (-30 for level 3, but +100 as bonus exp. This scales infinitely until the member verifies,
346
  # so they can continue earning exp, it just won't translate to levels and the leaderboard.
347
  # This way they can claim at any time and get a big boost in levels!
348
- claim_exp = total_exp + 70
349
 
350
  # send embed
351
  embed = Embed(color=Color.red())
@@ -363,20 +317,19 @@ async def add_exp(member_id):
363
  embed.set_image(url='https://cdn.discordapp.com/attachments/1150399343912833024/1205537451242688573/download_1.png?ex=65d8bb3e&is=65c6463e&hm=042fe7dd3521887db0bd48eeb846de1cc7c75194f9e95215c23512ff61ea3475&')
364
 
365
  lunar = bot.get_user(811235357663297546)
366
- #await member.send(embed=embed)
367
- await lunar.send(embed=embed)
368
  print(f"Sent verification cap embed to {member}")
369
  return
370
 
371
 
372
  # increment the old level value (better to replace outright)
373
- # only increment level column if you are lvl2 or 3+ with verified role (this may make some members not appear)
374
  global_df.loc[index, 'discord_level'] = current_level # do not change column name
375
 
376
  await member.add_roles(current_role)
377
  print(f"Level Up! Gave {member} {current_role}")
378
- await member.remove_roles(lvls[old_level])
379
- print(f"Removed {lvls[old_level]} from {member}")
380
  #print(f"{member} Level up! {current_level-1} -> {current_level}!")
381
  if current_role in member.roles: # needs update; reference exp reward for verification
382
  #await member.send(f"Level up! {current_level-1} -> {current_level}!")
@@ -403,8 +356,7 @@ async def add_exp(member_id):
403
  embed.add_field(name="Verify Here:", value=verification_link, inline=True)
404
 
405
  lunar = bot.get_user(811235357663297546)
406
- #await member.send(embed=embed)
407
- await lunar.send(embed=embed)
408
  print(f"Sent levelup embed to {member}")
409
  #You can verify your account to earn 100 points! To verify, do A.
410
 
@@ -479,16 +431,17 @@ async def remove_huggingfolks():
479
  # make a copy while discord id column still exists -> use for rank in discord embeds
480
  community_global_df_with_id = community_global_df_with_id.copy()
481
 
 
 
 
 
482
  # drop first column (discord id -> this is so we can display the important stuff in the leaderboard)
483
  community_global_df.drop(community_global_df.columns[0], axis=1, inplace=True)
484
- community_global_df.drop(community_global_df.columns[1], axis=1, inplace=True)
485
- community_global_df.drop(community_global_df.columns[2], axis=1, inplace=True)
486
- community_global_df.drop(community_global_df.columns[2], axis=1, inplace=True)
487
- community_global_df['total_exp'] = community_global_df['total_exp'].str.strip('L').astype(int)
488
- community_global_df['total_exp'] = pd.to_numeric(community_global_df['total_exp'])
489
- community_global_df = community_global_df.nlargest(len(community_global_df), 'total_exp')
490
 
491
- top_30_exp = community_global_df.nlargest(30, 'total_exp')
492
 
493
  top_30_exp['D'] = ['πŸ₯‡','πŸ₯ˆ','πŸ₯‰','','','','','','','','','','','','','','','','','','','','','','','','','','','']
494
  top_30_rows = top_30_exp.values.tolist()
@@ -589,4 +542,4 @@ with demo:
589
  #with gr.TabItem("πŸ“ˆ Hub-only leaderboard", elem_id="hub-table", id=2):
590
  except Exception as e:
591
  print(f"on_message Error: {e}")
592
- demo.queue().launch()
 
100
  name = "levelbot"
101
  #worksheet = gspread_bot.open(name).sheet1
102
  global_df['discord_user_id'] = global_df['discord_user_id'].astype(str)
 
103
  # could just do this / format
104
  set_with_dataframe(worksheet, global_df)
105
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 
127
 
128
  async def periodic_api_test(): # needs rewrite, can do same thing and interact with csv -> push to google sheets
129
  try:
130
+ # this should be gspread type function (do not mix with discord at all)
131
  global worksheet2_df
132
  # get
133
  for index, user in enumerate(worksheet2_df['hf_user_name']):
 
219
 
220
  print("test1")
221
  # discord_user_id column
 
222
  for index, cell_value in global_df.iloc[:, 0].items():
223
  # print("test2")
224
  # remove L (read, so no need to replace)
 
239
 
240
  # increment the old experience value (better not to replace outright)
241
  old_xp = global_df.loc[index, 'discord_exp']
 
 
 
 
 
 
 
 
 
 
242
  # remove L (write, so we replace)
243
  old_xp = str(old_xp)
244
  if old_xp.startswith("L") and old_xp.endswith("L"):
245
  old_xp = old_xp[1:-1]
246
+ # str -> int temporarily
 
 
 
 
247
  new_xp = int(old_xp) + XP_PER_MESSAGE
248
+ # check + convert back to string + google sheet proofing
249
+ current_level = calculate_level(new_xp)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  new_xp = str(new_xp)
251
  if not new_xp.startswith("L") and not new_xp.endswith("L"):
252
  new_xp = f"L" + str(new_xp) + f"L"
 
253
 
 
 
 
 
 
254
  # add back to dataframe in memory after checking redundantly;
255
+ if new_xp.startswith("L") and new_xp.endswith("L"):
256
  print("test5")
257
+ global_df.loc[index, 'discord_exp'] = new_xp # do not change column name
258
+ print(f"Record for {member} updated from {old_xp} to {global_df.loc[index, 'discord_exp']} (+{XP_PER_MESSAGE}) ")
259
 
260
  # level up
261
+
262
  verified_role = guild.get_role(900063512829755413)
263
 
264
  print(f"Current_level for {member}: {current_level}")
265
  if current_level >= 2 and current_level <=30:
266
  print("test6")
267
  current_role = lvls[current_level]
268
+ if current_role not in member.roles: # if we leveled up
269
 
270
+ # finding leaderboard rank + excluding huggingfolks
271
  try:
272
  # cell_value = L1234567890L
273
  # make copy
 
275
  # do calculations
276
  copy_df = global_df.copy()
277
  copy_df['discord_user_id'] = copy_df['discord_user_id'].str.strip('L').astype(str)
278
+ copy_df['discord_exp'] = copy_df['discord_exp'].str.strip('L').astype(int)
279
  row = copy_df[copy_df['discord_user_id'] == str(member_id)]
280
  print(f"Row found for {member_id}!")
281
+ target_exp = row['discord_exp'].values[0]
282
+ rank = (copy_df['discord_exp'] > target_exp).sum() + 1
283
+ print(f"The rank for discord_id {member_id} based on discord_exp is: {rank}")
284
  except Exception as e:
285
  print(f"Discord ID {member_id} not found in the DataFrame.")
286
  rank = "πŸ€—"
 
291
  if verified_role not in member.roles:
292
 
293
 
294
+ # XP has L; remove, calculate
295
+ new_xp = new_xp[1:-1]
296
+ new_xp = int(new_xp)
297
 
298
+ if new_xp % 10 == 0: # staggers messages so we don't send one every time exp is earned
299
  # claim exp (-30 for level 3, but +100 as bonus exp. This scales infinitely until the member verifies,
300
  # so they can continue earning exp, it just won't translate to levels and the leaderboard.
301
  # This way they can claim at any time and get a big boost in levels!
302
+ claim_exp = new_xp + 70
303
 
304
  # send embed
305
  embed = Embed(color=Color.red())
 
317
  embed.set_image(url='https://cdn.discordapp.com/attachments/1150399343912833024/1205537451242688573/download_1.png?ex=65d8bb3e&is=65c6463e&hm=042fe7dd3521887db0bd48eeb846de1cc7c75194f9e95215c23512ff61ea3475&')
318
 
319
  lunar = bot.get_user(811235357663297546)
320
+ await member.send(embed=embed)
 
321
  print(f"Sent verification cap embed to {member}")
322
  return
323
 
324
 
325
  # increment the old level value (better to replace outright)
326
+ # only increment level column if you are lvl2 or 3+ with verified role
327
  global_df.loc[index, 'discord_level'] = current_level # do not change column name
328
 
329
  await member.add_roles(current_role)
330
  print(f"Level Up! Gave {member} {current_role}")
331
+ await member.remove_roles(lvls[current_level-1])
332
+ print(f"Removed {lvls[current_level-1]} from {member}")
333
  #print(f"{member} Level up! {current_level-1} -> {current_level}!")
334
  if current_role in member.roles: # needs update; reference exp reward for verification
335
  #await member.send(f"Level up! {current_level-1} -> {current_level}!")
 
356
  embed.add_field(name="Verify Here:", value=verification_link, inline=True)
357
 
358
  lunar = bot.get_user(811235357663297546)
359
+ await member.send(embed=embed)
 
360
  print(f"Sent levelup embed to {member}")
361
  #You can verify your account to earn 100 points! To verify, do A.
362
 
 
431
  # make a copy while discord id column still exists -> use for rank in discord embeds
432
  community_global_df_with_id = community_global_df_with_id.copy()
433
 
434
+
435
+
436
+
437
+
438
  # drop first column (discord id -> this is so we can display the important stuff in the leaderboard)
439
  community_global_df.drop(community_global_df.columns[0], axis=1, inplace=True)
440
+ community_global_df['discord_exp'] = community_global_df['discord_exp'].str.strip('L').astype(int)
441
+ community_global_df['discord_exp'] = pd.to_numeric(community_global_df['discord_exp'])
442
+ community_global_df = community_global_df.nlargest(len(community_global_df), 'discord_exp')
 
 
 
443
 
444
+ top_30_exp = community_global_df.nlargest(30, 'discord_exp')
445
 
446
  top_30_exp['D'] = ['πŸ₯‡','πŸ₯ˆ','πŸ₯‰','','','','','','','','','','','','','','','','','','','','','','','','','','','']
447
  top_30_rows = top_30_exp.values.tolist()
 
542
  #with gr.TabItem("πŸ“ˆ Hub-only leaderboard", elem_id="hub-table", id=2):
543
  except Exception as e:
544
  print(f"on_message Error: {e}")
545
+ demo.queue().launch()