Spaces:
Running on CPU Upgrade

lunarflu HF Staff commited on
Commit
a987f79
Β·
verified Β·
1 Parent(s): 6c5c8b5

testing hub merge

Browse files
Files changed (1) hide show
  1. app.py +74 -27
app.py CHANGED
@@ -100,6 +100,7 @@ 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
  # could just do this / format
104
  set_with_dataframe(worksheet, global_df)
105
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
@@ -219,6 +220,7 @@ async def add_exp(member_id):
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,35 +241,78 @@ async def add_exp(member_id):
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,12 +320,12 @@ async def add_exp(member_id):
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,15 +336,15 @@ async def add_exp(member_id):
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,19 +362,20 @@ async def add_exp(member_id):
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,7 +402,8 @@ async def add_exp(member_id):
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
 
@@ -437,11 +484,11 @@ async def remove_huggingfolks():
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()
 
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")
 
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
 
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_xp = global_df.loc[index, 'total_exp']
267
+ hub_xp = global_df.loc[index, 'hub_exp']
268
+
269
+ total_xp = str(total_xp)
270
+ hub_xp = str(hub_xp)
271
+
272
+ if total_xp.startswith("L") and total_xp.endswith("L"):
273
+ total_xp = total_xp[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_xp)
278
+
279
+ # check if hub exp not empty
280
+ if hub_xp.strip():
281
+ total_xp = int(new_xp) + int(hub_xp)
282
+ else:
283
+ total_xp = int(new_xp)
284
+
285
+ # ----------------------------------------------------
286
+
287
+ # total v
288
+ current_level = calculate_level(total_xp)
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
+ if not total_xp.startswith("L") and not total_xp.endswith("L"):
298
+ total_xp = f"L" + str(total_xp) + f"L"
299
+
300
  # add back to dataframe in memory after checking redundantly;
301
+ if total_xp.startswith("L") and total_xp.endswith("L"):
302
  print("test5")
303
+ global_df.loc[index, 'total_exp'] = total_xp # do not change column name
304
+ print(f"Record for {member} updated from {old_total_xp} to {global_df.loc[index, 'total_exp']} (+{XP_PER_MESSAGE}) ")
305
 
306
  # level up
 
307
  verified_role = guild.get_role(900063512829755413)
308
 
309
  print(f"Current_level for {member}: {current_level}")
310
  if current_level >= 2 and current_level <=30:
311
  print("test6")
312
  current_role = lvls[current_level]
313
+ if current_role not in member.roles: # if we need to level up / update role
314
 
315
+ # finding leaderboard rank + excluding huggingfolks (still need exclusion)
316
  try:
317
  # cell_value = L1234567890L
318
  # make copy
 
320
  # do calculations
321
  copy_df = global_df.copy()
322
  copy_df['discord_user_id'] = copy_df['discord_user_id'].str.strip('L').astype(str)
323
+ copy_df['total_xp'] = copy_df['total_xp'].str.strip('L').astype(int)
324
  row = copy_df[copy_df['discord_user_id'] == str(member_id)]
325
  print(f"Row found for {member_id}!")
326
+ target_exp = row['total_xp'].values[0]
327
+ rank = (copy_df['total_xp'] > target_exp).sum() + 1
328
+ print(f"The rank for discord_id {member_id} based on total_xp is: {rank}")
329
  except Exception as e:
330
  print(f"Discord ID {member_id} not found in the DataFrame.")
331
  rank = "πŸ€—"
 
336
  if verified_role not in member.roles:
337
 
338
 
339
+ # L12345L -> `12345` -> 12345
340
+ total_xp = total_xp[1:-1]
341
+ total_xp = int(total_xp)
342
 
343
+ if total_xp % 10 == 0: # staggers messages so we don't send one every time exp is earned
344
  # claim exp (-30 for level 3, but +100 as bonus exp. This scales infinitely until the member verifies,
345
  # so they can continue earning exp, it just won't translate to levels and the leaderboard.
346
  # This way they can claim at any time and get a big boost in levels!
347
+ claim_exp = total_xp + 70
348
 
349
  # send embed
350
  embed = Embed(color=Color.red())
 
362
  embed.set_image(url='https://cdn.discordapp.com/attachments/1150399343912833024/1205537451242688573/download_1.png?ex=65d8bb3e&is=65c6463e&hm=042fe7dd3521887db0bd48eeb846de1cc7c75194f9e95215c23512ff61ea3475&')
363
 
364
  lunar = bot.get_user(811235357663297546)
365
+ #await member.send(embed=embed)
366
+ await lunar.send(embed=embed)
367
  print(f"Sent verification cap embed to {member}")
368
  return
369
 
370
 
371
  # increment the old level value (better to replace outright)
372
+ # only increment level column if you are lvl2 or 3+ with verified role (this may make some members not appear)
373
  global_df.loc[index, 'discord_level'] = current_level # do not change column name
374
 
375
  await member.add_roles(current_role)
376
  print(f"Level Up! Gave {member} {current_role}")
377
+ await member.remove_roles(lvls[old_level])
378
+ print(f"Removed {lvls[old_level]} from {member}")
379
  #print(f"{member} Level up! {current_level-1} -> {current_level}!")
380
  if current_role in member.roles: # needs update; reference exp reward for verification
381
  #await member.send(f"Level up! {current_level-1} -> {current_level}!")
 
402
  embed.add_field(name="Verify Here:", value=verification_link, inline=True)
403
 
404
  lunar = bot.get_user(811235357663297546)
405
+ #await member.send(embed=embed)
406
+ await lunar.send(embed=embed)
407
  print(f"Sent levelup embed to {member}")
408
  #You can verify your account to earn 100 points! To verify, do A.
409
 
 
484
 
485
  # drop first column (discord id -> this is so we can display the important stuff in the leaderboard)
486
  community_global_df.drop(community_global_df.columns[0], axis=1, inplace=True)
487
+ community_global_df['total_xp'] = community_global_df['total_xp'].str.strip('L').astype(int)
488
+ community_global_df['total_xp'] = pd.to_numeric(community_global_df['total_xp'])
489
+ community_global_df = community_global_df.nlargest(len(community_global_df), 'total_xp')
490
 
491
+ top_30_exp = community_global_df.nlargest(30, 'total_xp')
492
 
493
  top_30_exp['D'] = ['πŸ₯‡','πŸ₯ˆ','πŸ₯‰','','','','','','','','','','','','','','','','','','','','','','','','','','','']
494
  top_30_rows = top_30_exp.values.tolist()