import discord import os import threading from discord.ext import commands import json import datetime import requests import os.path import random import gspread import re import asyncio import csv from tabulate import tabulate import logging import time from PIL import Image, ImageDraw ,ImageFont,ImageOps import requests from io import BytesIO import gradio_client import gradio as gr from gradio_client import Client from huggingface_hub import HfApi, list_models, list_liked_repos, list_metrics DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None) intents = discord.Intents.all() bot = commands.Bot(command_prefix='!', intents=intents) logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) """""" XP_PER_MESSAGE = 10 # 100k messages = 1M exp = lvl 100 """""" service_account = json.loads(os.environ.get('KEY')) file_path = 'service_account.json' with open(file_path, 'w') as json_file: json.dump(service_account, json_file) gspread_bot = gspread.service_account(filename='service_account.json') #worksheet = gspread_bot.open("levelbot").sheet1 worksheet2 = gspread_bot.open("hf_discord_verified_users_test").sheet1 """""" bot_ids = [1136614989411655780, 1166392942387265536, 1158038249835610123, 1130774761031610388, 1155489509518098565, 1155169841276260546, 1152238037355474964, 1154395078735953930] """""" api = HfApi() """""" @bot.event async def on_ready(): print(f'Logged in as {bot.user.name}') print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}") def calculate_level(xp): return int(xp ** (1.0 / 3.0)) def calculate_xp(level): return (int(level ** 3)) processed_users = set() async def periodic_api_test(): try: await asyncio.sleep(1) column_values_3 = worksheet2.col_values(3) column_values_8 = worksheet2.col_values(8) for i, user in enumerate(column_values_3): if user in processed_users: continue url = f"https://huggingface.co/api/users/{user}/overview" response = requests.get(url) if response.status_code == 200: data = response.json() likes = data["numLikes"] models = data["numModels"] datasets = data["numDatasets"] spaces = data["numSpaces"] discussions = data["numDiscussions"] papers = data["numPapers"] upvotes = data["numUpvotes"] worksheet2.update(values=[[likes, models, datasets, spaces, discussions, papers, upvotes]], range_name=f'G{i+1}:M{i+1}') processed_users.add(user) else: print(f"Failed to retrieve data for user {user}. Status code: {response.status_code}") except Exception as e: print(f"periodic_api_test Error: {e}") """ async def search_csv(file_path, column_index, search_value): with open(file_path, 'r', newline='') as csvfile: reader = csv.reader(csvfile) first_row = next(reader) print(first_row) for row in reader: if row and len(row) > column_index and row[column_index] == search_value: print(row) def is_csv_empty(file_path): if not os.path.isfile(file_path): print(f"The file '{file_path}' does not exist.") return False with open(file_path, 'r', newline='') as csvfile: reader = csv.reader(csvfile) try: first_row = next(reader) print(first_row) except StopIteration: print(f"The file '{file_path}' is empty.") return False if not any(first_row): print(f"The file '{file_path}' has no data.") return False return True """ def blocking_io(member_id): try: time.sleep(1) worksheet = gspread_bot.open("levelbot").sheet1 guild = bot.get_guild(879548962464493619) member = guild.get_member(member_id) print(f" {member}") print(f"2") cell = worksheet.find(str(member.id)) length = len(worksheet.col_values(1)) print(f"3") if cell is None: print(f"creating new record for {member}") # if not, create new record string_member_id = str(member.id) xp = 10 current_level = calculate_level(xp) member_name = member.name worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}') # initial role assignment if current_level == 1: if lvl1 not in member.roles: return(1) else: print(f"4") if cell: # if so, update that row... xp = worksheet.cell(cell.row, cell.col+2).value xp = int(xp) + XP_PER_MESSAGE current_level = calculate_level(xp) print(f"updating record for {member}: {xp} xp") # write with added xp worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}') # level up print(f"current_level: {current_level}") if current_level >= 2 and current_level <=30: # pass current_level as int? return(current_level) except Exception as e: print(f"blocking_io Error: {e}") async def add_exp(member_id): try: await asyncio.sleep(1) loop = asyncio.get_running_loop() result = await loop.run_in_executor(None, blocking_io, member_id) guild = bot.get_guild(879548962464493619) member = guild.get_member(member_id) lvl1 = guild.get_role(1171861537699397733) lvl2 = guild.get_role(1171861595115245699) lvl3 = guild.get_role(1171861626715115591) lvl4 = guild.get_role(1171861657975259206) lvl5 = guild.get_role(1171861686580412497) lvl6 = guild.get_role(1171861900301172736) lvl7 = guild.get_role(1171861936258941018) lvl8 = guild.get_role(1171861968597024868) lvl9 = guild.get_role(1171862009982242836) lvl10 = guild.get_role(1164188093713223721) lvl11 = guild.get_role(1171524944354607104) lvl12 = guild.get_role(1171524990257082458) lvl13 = guild.get_role(1171525021928263791) lvl14 = guild.get_role(1171525062201966724) lvl15 = guild.get_role(1171525098465918996) lvl16 = guild.get_role(1176826165546201099) lvl17 = guild.get_role(1176826221301092392) lvl18 = guild.get_role(1176826260643659776) lvl19 = guild.get_role(1176826288816791693) lvl20 = guild.get_role(1176826319447801896) lvl21 = guild.get_role(1195030831174008902) lvl22 = guild.get_role(1195030883351150592) lvl23 = guild.get_role(1196055555006009445) lvl24 = guild.get_role(1196055640917938216) lvl25 = guild.get_role(1196055712506318869) lvl26 = guild.get_role(1196055775924195378) lvl27 = guild.get_role(1196055837018435664) lvl28 = guild.get_role(1196055908267081849) lvl29 = guild.get_role(1196055970804150352) lvl30 = guild.get_role(1196056027720847380) lvls = { 1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10, 11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15, 16: lvl16, 17: lvl17, 18: lvl18, 19: lvl19, 20: lvl20, 21: lvl21, 22: lvl22, 23: lvl23, 24: lvl24, 25: lvl25, 26: lvl26, 27: lvl27, 28: lvl28, 29: lvl29, 30: lvl30, } if result == 1: await member.add_roles(lvl1) print(f"Gave {member} {lvl1}") print(f"result: {result}") if result >= 2: current_level = result current_role = lvls[current_level] if current_role not in member.roles: await member.add_roles(current_role) print(f"Giving {member} {current_role}") await member.remove_roles(lvls[current_level-1]) print(f"Removed {lvls[current_level-1]} from {member}") #print(f"{member} Level up! {current_level-1} -> {current_level}!") if current_role in member.roles: await member.send(f"Level up! {current_level-1} -> {current_level}!") except Exception as e: print(f"add_exp Error: {e}") @bot.event async def on_message(message): try: if message.author.id not in bot_ids: if "!help_xp" not in message.content: print(f"adding exp from message {message.author}") await asyncio.sleep(1) await add_exp(message.author.id) #await periodic_api_test() # run this ~1 per day otherwise will lead to issues await bot.process_commands(message) except Exception as e: print(f"on_message Error: {e}") @bot.event async def on_reaction_add(reaction, user): try: if user.id not in bot_ids: print(f"adding exp from react {user.id}") await asyncio.sleep(1) await add_exp(user.id) except Exception as e: print(f"on_reaction_add Error: {e}") @bot.command(name='update_leaderboard') async def update_leaderboard(ctx, num_results: int = 10): if ctx.author.id == 811235357663297546: await asyncio.sleep(1) worksheet = gspread_bot.open("levelbot").sheet1 names_list = worksheet.col_values(2)[1:] levels_list = worksheet.col_values(4)[1:] exp_list = worksheet.col_values(3)[1:] channel = bot.get_channel(1197143964994773023) message = await channel.fetch_message(1197148293164187678) # for 3 lists combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)] combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list] combined_list = sorted(combined_list, key=lambda x: x[1], reverse=True) print(combined_list) top_results = combined_list[:num_results] #print(top_results) """ # get position, then find that value in updated_names_list levels_list = list(map(int, levels_list)) data_pairs = list(zip(names_list, levels_list)) sorted_data_pairs = sorted(data_pairs, key=lambda x: x[1], reverse=True) top_data_pairs = sorted_data_pairs[:num_results] """ # remove huggingfolks guild = ctx.guild role = discord.utils.get(guild.roles, id=897376942817419265) if role is None: await ctx.send("Role not found.") return members_with_role = [member.name for member in guild.members if role in member.roles] top_results = [r for r in top_results if r[0] not in members_with_role] for name, level, xp in top_results: print(f"Name: {name}, Level: {level}, Exp: {xp}") def xp_required_to_next_level(current_level, current_xp): level_floor_xp = calculate_xp(current_level) level_ceiling_xp = calculate_xp(current_level+1) xp_to_level_up = level_ceiling_xp - current_xp return (xp_to_level_up) # put into message / leaderboard new_leaderboard_data = [(name, level, str(xp_required_to_next_level(int(level), int(xp)))) for name, level, xp in top_results] new_table = tabulate(new_leaderboard_data, headers=["Name", "Level", "XP to level up"], tablefmt="plain") await message.edit(content=f"Updated Leaderboard:\n```\n{new_table}\n```") @bot.command(name='xp_help') async def xp_help(ctx): help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers" await ctx.author.send(help_message) @bot.command() async def write_csv(ctx): worksheet = gspread_bot.open("levelbot").sheet1 values = worksheet.get_all_values() target_id = '811235357663297546' variable_xp = 0 csv_file_path = 'test.csv' with open(csv_file_path, 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerows(values) # modifying csv data with open(csv_file_path, 'r') as csv_file: csv_reader = csv.reader(csv_file) rows = list(csv_reader) for row in rows: print(row) if target_id in row: index_of_target = row.index(target_id) # 2 to right cell_to_increment_index = index_of_target + 2 # +10 try: row[cell_to_increment_index] = str(int(row[cell_to_increment_index]) + variable_xp) print(row[cell_to_increment_index]) except (IndexError, ValueError): print(f"Error: Unable to increment cell at index {cell_to_increment_index}") break # write modified data and send in discord with open(csv_file_path, 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerows(rows) csv_data_str = '\n'.join(','.join(row) for row in rows) csv_file = discord.File(csv_file_path) await ctx.channel.send(content="csv", file=csv_file) #target_user = await bot.fetch_user(int(target_id)) #await target_user.send(content="csv", file=csv_file) # do simple operation on file (change one value) # post current version to discord # recover csv from discord channel, read into memory @bot.command() async def read_csv(ctx): async for message in ctx.channel.history(limit=10): if message.attachments: attachment = message.attachments[0] if attachment.filename.endswith('.csv'): csv_content = await attachment.read() csv_data = [] csv_lines = csv_content.decode('utf-8').splitlines() csv_reader = csv.reader(csv_lines) for row in csv_reader: csv_data.append(row) print(csv_data) await ctx.send("discord -> read -> updated HF Hub") # react to the given message, helps with explainability await message.add_reaction('✅') break """ @bot.command(name='send_message') async def send_message(ctx): channel = ctx.channel leaderboard_data = [("User 1", 100), ("User 2", 80), ("User 3", 60)] table = tabulate(leaderboard_data, headers=["User", "Score"], tablefmt="plain") message = await channel.send(f"Leaderboard:\n```\n{table}\n```") """ # embeds with user pfps? # name, pfp, time in server.... # discord_level column # pick 10 highest # update # weekly do different count # count number of messages per user for every channel (total messages) # fix sheet if necessary # might need cell location data to pull both level and username at same time # add emojis for some color # check if members are still in the server def create_progress_bar(w = 800,h = 100 ,progress_percentage=0.25): progress = Image.new("RGB", (w,h)) draw = ImageDraw.Draw(progress) draw_progress(20, h*0.5, w-20*3, 25, progress_percentage,draw) return progress def draw_progress(x, y, width, height, progress_percentage,draw, fg="#FFD21E", fg2="#6B7280"): # Draw the background draw.rectangle((x+(height/2), y, x+width+(height/2), y+height), fill=fg2, width=10) draw.ellipse((x+width, y, x+height+width, y+height), fill=fg2) draw.ellipse((x, y, x+height, y+height), fill=fg2) width = int(width*progress_percentage) # Draw the part of the progress bar that is actually filled draw.rectangle((x+(height/2), y, x+width+(height/2), y+height), fill=fg, width=10) draw.ellipse((x+width, y, x+height+width, y+height), fill=fg) draw.ellipse((x, y, x+height, y+height), fill=fg) def create_base(w=800,h = 300,username = "username",current_xp=25,next_lvl_xp=100): base = Image.new("RGB",(w,h)) draw = ImageDraw.Draw(base) # requires a newer version of pillow font = ImageFont.load_default(size=50) # big text draw.text((20, 20),username,(255,255,255),font=font) font = ImageFont.load_default(size=30) xp_msg = f"{current_xp}/{next_lvl_xp} XP" pad = font.getlength(xp_msg) draw.text((w-30-pad, h-50),xp_msg,(255,255,255),font=font) return base async def process_avatar(url,hugg = True): response = requests.get(url) img = Image.open(BytesIO(response.content)) # resize image to a square img = ImageOps.fit(img, (189, 189)) # create circular mask mask = Image.new('L', (189, 189), 0) draw = ImageDraw.Draw(mask) draw.ellipse((0, 0, 189, 189), fill=255) # apply mask to image img.putalpha(mask) if hugg : img2 = Image.open("./assets/hugg.png") img.paste(img2,(0,0),img2) return img @bot.command(name='card') async def card(ctx): username=ctx.author.name url = ctx.author.display_avatar.url # TODO : # get the level of a user # get next lvl xp current_xp = 25 next_lvl_xp = 100 progress_percentage = current_xp/next_lvl_xp base = create_base(username=username,current_xp=current_xp,next_lvl_xp=next_lvl_xp) progress = create_progress_bar(progress_percentage=progress_percentage) avatar = await process_avatar(url) final = Image.new("RGB",(base.size[0],base.size[1]+progress.size[1])) final.paste(base,(0,0)) final.paste(progress,(0,base.size[1])) x_begin = final.size[0]-189-20 final.paste(avatar,(x_begin,20,x_begin+189,20+189),avatar) with BytesIO() as image_binary: final.save(image_binary, 'PNG') image_binary.seek(0) await ctx.send(file=discord.File(fp=image_binary, filename=f'{username}.png')) """""" DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None) def run_bot(): bot.run(DISCORD_TOKEN) threading.Thread(target=run_bot).start() def greet(name): return "Hello " + name + "!" demo = gr.Interface(fn=greet, inputs="text", outputs="text") demo.launch()