Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
test hub merge (added roles up to 40)
Browse files
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")
|
@@ -127,7 +128,7 @@ processed_users = set()
|
|
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']):
|
@@ -181,6 +182,7 @@ async def add_exp(member_id):
|
|
181 |
lvl8 = guild.get_role(1171861968597024868)
|
182 |
lvl9 = guild.get_role(1171862009982242836)
|
183 |
lvl10 = guild.get_role(1164188093713223721)
|
|
|
184 |
lvl11 = guild.get_role(1171524944354607104)
|
185 |
lvl12 = guild.get_role(1171524990257082458)
|
186 |
lvl13 = guild.get_role(1171525021928263791)
|
@@ -202,11 +204,23 @@ async def add_exp(member_id):
|
|
202 |
lvl28 = guild.get_role(1196055908267081849)
|
203 |
lvl29 = guild.get_role(1196055970804150352)
|
204 |
lvl30 = guild.get_role(1196056027720847380)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
lvls = {
|
207 |
1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
|
208 |
11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15, 16: lvl16, 17: lvl17, 18: lvl18, 19: lvl19, 20: lvl20,
|
209 |
21: lvl21, 22: lvl22, 23: lvl23, 24: lvl24, 25: lvl25, 26: lvl26, 27: lvl27, 28: lvl28, 29: lvl29, 30: lvl30,
|
|
|
210 |
}
|
211 |
|
212 |
# xp = int64
|
@@ -219,6 +233,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 +254,73 @@ 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 |
-
|
|
|
|
|
|
|
|
|
247 |
new_xp = int(old_xp) + XP_PER_MESSAGE
|
248 |
-
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
256 |
print("test5")
|
257 |
-
global_df.loc[index, '
|
258 |
-
print(f"Record for {member} updated from {
|
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 <=
|
266 |
print("test6")
|
267 |
current_role = lvls[current_level]
|
268 |
-
if current_role not in member.roles: # if we
|
269 |
|
270 |
-
# finding leaderboard rank + excluding huggingfolks
|
271 |
try:
|
272 |
# cell_value = L1234567890L
|
273 |
# make copy
|
@@ -275,12 +328,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['
|
279 |
row = copy_df[copy_df['discord_user_id'] == str(member_id)]
|
280 |
print(f"Row found for {member_id}!")
|
281 |
-
target_exp = row['
|
282 |
-
rank = (copy_df['
|
283 |
-
print(f"The rank for discord_id {member_id} based on
|
284 |
except Exception as e:
|
285 |
print(f"Discord ID {member_id} not found in the DataFrame.")
|
286 |
rank = "π€"
|
@@ -291,15 +344,15 @@ async def add_exp(member_id):
|
|
291 |
if verified_role not in member.roles:
|
292 |
|
293 |
|
294 |
-
#
|
295 |
-
|
296 |
-
|
297 |
|
298 |
-
if
|
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 =
|
303 |
|
304 |
# send embed
|
305 |
embed = Embed(color=Color.red())
|
@@ -317,19 +370,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[
|
332 |
-
print(f"Removed {lvls[
|
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}!")
|
@@ -339,7 +393,7 @@ async def add_exp(member_id):
|
|
339 |
# send embed
|
340 |
embed = Embed(color=Color.blue())
|
341 |
embed.set_author(name=f"{member}", icon_url=member.avatar.url if member.avatar else bot.user.avatar.url)
|
342 |
-
embed.title = f"Level Up! `{
|
343 |
msg = f'π€ Congrats {member}! You just leveled up in the Hugging Face Discord server'
|
344 |
embed.description = f"{msg}."
|
345 |
embed.add_field(name="Leaderboard Ranking:", value=f"π **{rank}**\n\nhttps://discord.com/channels/879548962464493619/1197143964994773023", inline=True)
|
@@ -356,7 +410,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 |
|
@@ -431,17 +486,16 @@ async def remove_huggingfolks():
|
|
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[
|
441 |
-
community_global_df[
|
442 |
-
community_global_df
|
|
|
|
|
|
|
443 |
|
444 |
-
top_30_exp = community_global_df.nlargest(30, '
|
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")
|
|
|
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']):
|
|
|
182 |
lvl8 = guild.get_role(1171861968597024868)
|
183 |
lvl9 = guild.get_role(1171862009982242836)
|
184 |
lvl10 = guild.get_role(1164188093713223721)
|
185 |
+
|
186 |
lvl11 = guild.get_role(1171524944354607104)
|
187 |
lvl12 = guild.get_role(1171524990257082458)
|
188 |
lvl13 = guild.get_role(1171525021928263791)
|
|
|
204 |
lvl28 = guild.get_role(1196055908267081849)
|
205 |
lvl29 = guild.get_role(1196055970804150352)
|
206 |
lvl30 = guild.get_role(1196056027720847380)
|
207 |
+
|
208 |
+
lvl31 = guild.get_role(1206542603261186078)
|
209 |
+
lvl32 = guild.get_role(1206542673549205514)
|
210 |
+
lvl33 = guild.get_role(1206542690939048007)
|
211 |
+
lvl34 = guild.get_role(1206542707862806568)
|
212 |
+
lvl35 = guild.get_role(1206542723633512468)
|
213 |
+
lvl36 = guild.get_role(1206542738728681485)
|
214 |
+
lvl37 = guild.get_role(1206542754625101866)
|
215 |
+
lvl38 = guild.get_role(1206542771314364416)
|
216 |
+
lvl39 = guild.get_role(1206542785973321758)
|
217 |
+
lvl40 = guild.get_role(1206542802155208725)
|
218 |
|
219 |
lvls = {
|
220 |
1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
|
221 |
11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15, 16: lvl16, 17: lvl17, 18: lvl18, 19: lvl19, 20: lvl20,
|
222 |
21: lvl21, 22: lvl22, 23: lvl23, 24: lvl24, 25: lvl25, 26: lvl26, 27: lvl27, 28: lvl28, 29: lvl29, 30: lvl30,
|
223 |
+
31: lvl31, 32: lvl32, 33: lvl33, 34: lvl34, 35: lvl35, 36: lvl36, 37: lvl37, 38: lvl38, 39: lvl39, 40: lvl40,
|
224 |
}
|
225 |
|
226 |
# xp = int64
|
|
|
233 |
|
234 |
print("test1")
|
235 |
# discord_user_id column
|
236 |
+
# iterate over items of first column (discord_user_id)
|
237 |
for index, cell_value in global_df.iloc[:, 0].items():
|
238 |
# print("test2")
|
239 |
# remove L (read, so no need to replace)
|
|
|
254 |
|
255 |
# increment the old experience value (better not to replace outright)
|
256 |
old_xp = global_df.loc[index, 'discord_exp']
|
257 |
+
|
258 |
+
# add to discord exp (this is input)
|
259 |
+
# add discord exp and hub exp to total_exp
|
260 |
+
# set level based on total_exp (this is output)
|
261 |
+
|
262 |
# remove L (write, so we replace)
|
263 |
old_xp = str(old_xp)
|
264 |
if old_xp.startswith("L") and old_xp.endswith("L"):
|
265 |
old_xp = old_xp[1:-1]
|
266 |
+
|
267 |
+
# set old level; use this for more accurate logging and jumping multiple levels at once (for example, verifying)
|
268 |
+
old_level = calculate_level(int(old_xp))
|
269 |
+
|
270 |
+
# str -> int temporarily for adding
|
271 |
new_xp = int(old_xp) + XP_PER_MESSAGE
|
272 |
+
|
273 |
+
# ----------------------------------------------------
|
274 |
+
total_exp = global_df.loc[index, 'total_exp']
|
275 |
+
hub_xp = global_df.loc[index, 'hub_exp']
|
276 |
+
|
277 |
+
total_exp = str(total_exp)
|
278 |
+
hub_xp = str(hub_xp)
|
279 |
+
|
280 |
+
if total_exp.startswith("L") and total_exp.endswith("L"):
|
281 |
+
total_exp = total_exp[1:-1]
|
282 |
+
if hub_xp.startswith("L") and hub_xp.endswith("L"):
|
283 |
+
hub_xp = hub_xp[1:-1]
|
284 |
+
|
285 |
+
old_total_xp = int(total_exp)
|
286 |
+
|
287 |
+
# check if hub exp not empty
|
288 |
+
if hub_xp.strip():
|
289 |
+
total_exp = int(new_xp) + int(hub_xp)
|
290 |
+
else:
|
291 |
+
total_exp = int(new_xp)
|
292 |
+
# ----------------------------------------------------
|
293 |
+
|
294 |
+
# total v
|
295 |
+
current_level = calculate_level(total_exp)
|
296 |
+
|
297 |
+
# convert back to string + google sheet proofing
|
298 |
new_xp = str(new_xp)
|
299 |
if not new_xp.startswith("L") and not new_xp.endswith("L"):
|
300 |
new_xp = f"L" + str(new_xp) + f"L"
|
301 |
+
global_df.loc[index, 'discord_exp'] = new_xp # do not change column name
|
302 |
|
303 |
+
# after
|
304 |
+
total_exp = str(total_exp)
|
305 |
+
if not total_exp.startswith("L") and not total_exp.endswith("L"):
|
306 |
+
total_exp = f"L" + str(total_exp) + f"L"
|
307 |
+
|
308 |
# add back to dataframe in memory after checking redundantly;
|
309 |
+
if total_exp.startswith("L") and total_exp.endswith("L"):
|
310 |
print("test5")
|
311 |
+
global_df.loc[index, 'total_exp'] = total_exp # do not change column name
|
312 |
+
print(f"Record for {member} updated from {old_total_xp} to {global_df.loc[index, 'total_exp']} (+{XP_PER_MESSAGE}) ")
|
313 |
|
314 |
# level up
|
|
|
315 |
verified_role = guild.get_role(900063512829755413)
|
316 |
|
317 |
print(f"Current_level for {member}: {current_level}")
|
318 |
+
if current_level >= 2 and current_level <=40:
|
319 |
print("test6")
|
320 |
current_role = lvls[current_level]
|
321 |
+
if current_role not in member.roles: # if we need to level up / update role
|
322 |
|
323 |
+
# finding leaderboard rank + excluding huggingfolks (still need exclusion)
|
324 |
try:
|
325 |
# cell_value = L1234567890L
|
326 |
# make copy
|
|
|
328 |
# do calculations
|
329 |
copy_df = global_df.copy()
|
330 |
copy_df['discord_user_id'] = copy_df['discord_user_id'].str.strip('L').astype(str)
|
331 |
+
copy_df['total_exp'] = copy_df['total_exp'].str.strip('L').astype(int)
|
332 |
row = copy_df[copy_df['discord_user_id'] == str(member_id)]
|
333 |
print(f"Row found for {member_id}!")
|
334 |
+
target_exp = row['total_exp'].values[0]
|
335 |
+
rank = (copy_df['total_exp'] > target_exp).sum() + 1
|
336 |
+
print(f"The rank for discord_id {member_id} based on total_exp is: {rank}")
|
337 |
except Exception as e:
|
338 |
print(f"Discord ID {member_id} not found in the DataFrame.")
|
339 |
rank = "π€"
|
|
|
344 |
if verified_role not in member.roles:
|
345 |
|
346 |
|
347 |
+
# L12345L -> `12345` -> 12345
|
348 |
+
total_exp = total_exp[1:-1]
|
349 |
+
total_exp = int(total_exp)
|
350 |
|
351 |
+
if total_exp % 10 == 0: # staggers messages so we don't send one every time exp is earned
|
352 |
# claim exp (-30 for level 3, but +100 as bonus exp. This scales infinitely until the member verifies,
|
353 |
# so they can continue earning exp, it just won't translate to levels and the leaderboard.
|
354 |
# This way they can claim at any time and get a big boost in levels!
|
355 |
+
claim_exp = total_exp + 70
|
356 |
|
357 |
# send embed
|
358 |
embed = Embed(color=Color.red())
|
|
|
370 |
embed.set_image(url='https://cdn.discordapp.com/attachments/1150399343912833024/1205537451242688573/download_1.png?ex=65d8bb3e&is=65c6463e&hm=042fe7dd3521887db0bd48eeb846de1cc7c75194f9e95215c23512ff61ea3475&')
|
371 |
|
372 |
lunar = bot.get_user(811235357663297546)
|
373 |
+
#await member.send(embed=embed)
|
374 |
+
await lunar.send(embed=embed)
|
375 |
print(f"Sent verification cap embed to {member}")
|
376 |
return
|
377 |
|
378 |
|
379 |
# increment the old level value (better to replace outright)
|
380 |
+
# only increment level column if you are lvl2 or 3+ with verified role (this may make some members not appear)
|
381 |
global_df.loc[index, 'discord_level'] = current_level # do not change column name
|
382 |
|
383 |
await member.add_roles(current_role)
|
384 |
print(f"Level Up! Gave {member} {current_role}")
|
385 |
+
await member.remove_roles(lvls[old_level])
|
386 |
+
print(f"Removed {lvls[old_level]} from {member}")
|
387 |
#print(f"{member} Level up! {current_level-1} -> {current_level}!")
|
388 |
if current_role in member.roles: # needs update; reference exp reward for verification
|
389 |
#await member.send(f"Level up! {current_level-1} -> {current_level}!")
|
|
|
393 |
# send embed
|
394 |
embed = Embed(color=Color.blue())
|
395 |
embed.set_author(name=f"{member}", icon_url=member.avatar.url if member.avatar else bot.user.avatar.url)
|
396 |
+
embed.title = f"Level Up! `{old_level-1}` -> `{current_level}`"
|
397 |
msg = f'π€ Congrats {member}! You just leveled up in the Hugging Face Discord server'
|
398 |
embed.description = f"{msg}."
|
399 |
embed.add_field(name="Leaderboard Ranking:", value=f"π **{rank}**\n\nhttps://discord.com/channels/879548962464493619/1197143964994773023", inline=True)
|
|
|
410 |
embed.add_field(name="Verify Here:", value=verification_link, inline=True)
|
411 |
|
412 |
lunar = bot.get_user(811235357663297546)
|
413 |
+
#await member.send(embed=embed)
|
414 |
+
await lunar.send(embed=embed)
|
415 |
print(f"Sent levelup embed to {member}")
|
416 |
#You can verify your account to earn 100 points! To verify, do A.
|
417 |
|
|
|
486 |
# make a copy while discord id column still exists -> use for rank in discord embeds
|
487 |
community_global_df_with_id = community_global_df_with_id.copy()
|
488 |
|
|
|
|
|
|
|
|
|
489 |
# drop first column (discord id -> this is so we can display the important stuff in the leaderboard)
|
490 |
community_global_df.drop(community_global_df.columns[0], axis=1, inplace=True)
|
491 |
+
community_global_df.drop(community_global_df.columns[1], axis=1, inplace=True)
|
492 |
+
community_global_df.drop(community_global_df.columns[2], axis=1, inplace=True)
|
493 |
+
community_global_df.drop(community_global_df.columns[2], axis=1, inplace=True)
|
494 |
+
community_global_df['total_exp'] = community_global_df['total_exp'].str.strip('L').astype(int)
|
495 |
+
community_global_df['total_exp'] = pd.to_numeric(community_global_df['total_exp'])
|
496 |
+
community_global_df = community_global_df.nlargest(len(community_global_df), 'total_exp')
|
497 |
|
498 |
+
top_30_exp = community_global_df.nlargest(30, 'total_exp')
|
499 |
|
500 |
top_30_exp['D'] = ['π₯','π₯','π₯','','','','','','','','','','','','','','','','','','','','','','','','','','','']
|
501 |
top_30_rows = top_30_exp.values.tolist()
|