Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
revert for now; some bug with writing to google sheets (maybe try individual rows)
Browse files
app.py
CHANGED
|
@@ -15,7 +15,6 @@ from tabulate import tabulate
|
|
| 15 |
import logging
|
| 16 |
import time
|
| 17 |
import pandas as pd
|
| 18 |
-
from apscheduler.schedulers.background import BackgroundScheduler
|
| 19 |
|
| 20 |
import gradio_client
|
| 21 |
import gradio as gr
|
|
@@ -26,8 +25,8 @@ DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
| 26 |
intents = discord.Intents.all()
|
| 27 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
|
| 33 |
""""""
|
|
@@ -45,40 +44,12 @@ bot_ids = [1136614989411655780, 1166392942387265536, 1158038249835610123, 113077
|
|
| 45 |
""""""
|
| 46 |
api = HfApi()
|
| 47 |
""""""
|
| 48 |
-
|
| 49 |
|
| 50 |
@bot.event
|
| 51 |
async def on_ready():
|
| 52 |
-
global csv_file
|
| 53 |
print(f'Logged in as {bot.user.name}')
|
| 54 |
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
|
| 55 |
-
# testing sheet -> read -> paste sheet
|
| 56 |
-
|
| 57 |
-
"""import data from google sheets -> HF Space .csv (doesn't make API call this way, as it's read-only)"""
|
| 58 |
-
data = pd.read_csv("https://docs.google.com/spreadsheets/d/1hQSsIg1Y9WdBF_CdNM1L1rUUREoxKqRTe3_ILo-WK8w/export?format=csv&gid=0")
|
| 59 |
-
print(f"csv successfully retrieved: {data}")
|
| 60 |
-
data.to_csv(csv_file, index=False)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
async def update_google_sheet():
|
| 64 |
-
"""save data from HF Space -> google sheets (makes 1 API call)"""
|
| 65 |
-
print("test")
|
| 66 |
-
data = open(csv_file, 'r').read()
|
| 67 |
-
print("test")
|
| 68 |
-
# 1hQSsIg1Y9WdBF_CdNM1L1rUUREoxKqRTe3_ILo-WK8w = live
|
| 69 |
-
# 1YJEwP7kZChI3Z2GKv4zFEfwwyQrO3zwXclv0QMrkOqA = test
|
| 70 |
-
spreadsheet_id = "1hQSsIg1Y9WdBF_CdNM1L1rUUREoxKqRTe3_ILo-WK8w"
|
| 71 |
-
print("test")
|
| 72 |
-
gspread_bot.import_csv(f"{spreadsheet_id}", data)
|
| 73 |
-
print({f"Google sheet {spreadsheet_id} successfully updated!"})
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
"""
|
| 77 |
-
scheduler = BackgroundScheduler()
|
| 78 |
-
scheduler.add_job(update_google_sheet, "interval", seconds=60)
|
| 79 |
-
scheduler.start()
|
| 80 |
-
"""
|
| 81 |
-
|
| 82 |
|
| 83 |
|
| 84 |
def calculate_level(xp):
|
|
@@ -91,7 +62,7 @@ def calculate_xp(level):
|
|
| 91 |
|
| 92 |
processed_users = set()
|
| 93 |
|
| 94 |
-
async def periodic_api_test():
|
| 95 |
try:
|
| 96 |
await asyncio.sleep(1)
|
| 97 |
column_values_3 = worksheet2.col_values(3)
|
|
@@ -123,12 +94,59 @@ async def periodic_api_test(): # needs rewrite, can do same thing and interact w
|
|
| 123 |
print(f"Failed to retrieve data for user {user}. Status code: {response.status_code}")
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
-
print(f"periodic_api_test Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
|
| 129 |
async def add_exp(member_id):
|
| 130 |
try:
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
guild = bot.get_guild(879548962464493619)
|
| 134 |
member = guild.get_member(member_id)
|
|
@@ -170,56 +188,24 @@ async def add_exp(member_id):
|
|
| 170 |
21: lvl21, 22: lvl22, 23: lvl23, 24: lvl24, 25: lvl25, 26: lvl26, 27: lvl27, 28: lvl28, 29: lvl29, 30: lvl30,
|
| 171 |
}
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
print(
|
| 184 |
-
|
| 185 |
-
print(f"
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
print(f"Current_level for {member}: {current_level}")
|
| 192 |
-
if current_level >= 2 and current_level <=30:
|
| 193 |
-
current_role = lvls[current_level]
|
| 194 |
-
if current_role not in member.roles:
|
| 195 |
-
await member.add_roles(current_role)
|
| 196 |
-
print(f"Level Up! Gave {member} {current_role}")
|
| 197 |
-
await member.remove_roles(lvls[current_level-1])
|
| 198 |
-
print(f"Removed {lvls[current_level-1]} from {member}")
|
| 199 |
-
#print(f"{member} Level up! {current_level-1} -> {current_level}!")
|
| 200 |
-
if current_role in member.roles:
|
| 201 |
-
await member.send(f"Level up! {current_level-1} -> {current_level}!")
|
| 202 |
-
|
| 203 |
-
if not member_found:
|
| 204 |
-
# if not, create new record
|
| 205 |
-
print(f"creating new record for {member}")
|
| 206 |
-
#string_member_id = str(member.id)
|
| 207 |
-
xp = 10 # define somewhere else?
|
| 208 |
-
current_level = calculate_level(xp)
|
| 209 |
-
member_name = member.name
|
| 210 |
-
row_data = [member_id, member_name, xp, current_level]
|
| 211 |
-
new_row_df = pd.DataFrame([row_data], columns=data.columns)
|
| 212 |
-
updated_df = data.append(new_row_df, ignore_index=True)
|
| 213 |
-
updated_df.to_csv(csv_file, index=False)
|
| 214 |
-
# initial role assignment
|
| 215 |
-
if current_level == 1:
|
| 216 |
-
if lvl1 not in member.roles:
|
| 217 |
-
await member.add_roles(lvl1)
|
| 218 |
-
print(f"Gave {member} {lvl1}") # can log this better
|
| 219 |
-
|
| 220 |
-
if member_id == 811235357663297546:
|
| 221 |
-
await update_google_sheet()
|
| 222 |
-
|
| 223 |
except Exception as e:
|
| 224 |
print(f"add_exp Error: {e}")
|
| 225 |
|
|
@@ -227,7 +213,7 @@ async def add_exp(member_id):
|
|
| 227 |
@bot.event
|
| 228 |
async def on_message(message):
|
| 229 |
try:
|
| 230 |
-
if message.author.id not in bot_ids:
|
| 231 |
if "!help_xp" not in message.content:
|
| 232 |
print(f"adding exp from message {message.author}")
|
| 233 |
await asyncio.sleep(1)
|
|
@@ -250,7 +236,7 @@ async def on_reaction_add(reaction, user):
|
|
| 250 |
|
| 251 |
|
| 252 |
@bot.command(name='update_leaderboard')
|
| 253 |
-
async def update_leaderboard(ctx, num_results: int = 10):
|
| 254 |
if ctx.author.id == 811235357663297546:
|
| 255 |
await asyncio.sleep(1)
|
| 256 |
worksheet = gspread_bot.open("levelbot").sheet1
|
|
@@ -261,6 +247,7 @@ async def update_leaderboard(ctx, num_results: int = 10): # needs rewrite
|
|
| 261 |
channel = bot.get_channel(1197143964994773023)
|
| 262 |
message = await channel.fetch_message(1197148293164187678)
|
| 263 |
|
|
|
|
| 264 |
# for 3 lists
|
| 265 |
combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)]
|
| 266 |
combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list]
|
|
@@ -309,9 +296,81 @@ async def xp_help(ctx):
|
|
| 309 |
help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers"
|
| 310 |
await ctx.author.send(help_message)
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
# embeds with user pfps?
|
| 313 |
# name, pfp, time in server....
|
| 314 |
|
|
|
|
| 315 |
# discord_level column
|
| 316 |
# pick 10 highest
|
| 317 |
# update
|
|
@@ -320,10 +379,12 @@ async def xp_help(ctx):
|
|
| 320 |
# count number of messages per user for every channel (total messages)
|
| 321 |
# fix sheet if necessary
|
| 322 |
|
|
|
|
| 323 |
# might need cell location data to pull both level and username at same time
|
| 324 |
# add emojis for some color
|
| 325 |
# check if members are still in the server
|
| 326 |
|
|
|
|
| 327 |
""""""
|
| 328 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 329 |
def run_bot():
|
|
|
|
| 15 |
import logging
|
| 16 |
import time
|
| 17 |
import pandas as pd
|
|
|
|
| 18 |
|
| 19 |
import gradio_client
|
| 20 |
import gradio as gr
|
|
|
|
| 25 |
intents = discord.Intents.all()
|
| 26 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 27 |
|
| 28 |
+
logger = logging.getLogger(__name__)
|
| 29 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 30 |
|
| 31 |
|
| 32 |
""""""
|
|
|
|
| 44 |
""""""
|
| 45 |
api = HfApi()
|
| 46 |
""""""
|
| 47 |
+
|
| 48 |
|
| 49 |
@bot.event
|
| 50 |
async def on_ready():
|
|
|
|
| 51 |
print(f'Logged in as {bot.user.name}')
|
| 52 |
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
def calculate_level(xp):
|
|
|
|
| 62 |
|
| 63 |
processed_users = set()
|
| 64 |
|
| 65 |
+
async def periodic_api_test():
|
| 66 |
try:
|
| 67 |
await asyncio.sleep(1)
|
| 68 |
column_values_3 = worksheet2.col_values(3)
|
|
|
|
| 94 |
print(f"Failed to retrieve data for user {user}. Status code: {response.status_code}")
|
| 95 |
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f"periodic_api_test Error: {e}")
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def blocking_io(member_id):
|
| 101 |
+
try:
|
| 102 |
+
time.sleep(1)
|
| 103 |
+
worksheet = gspread_bot.open("levelbot").sheet1
|
| 104 |
+
|
| 105 |
+
guild = bot.get_guild(879548962464493619)
|
| 106 |
+
member = guild.get_member(member_id)
|
| 107 |
+
print(f" {member}")
|
| 108 |
+
print(f"2")
|
| 109 |
+
|
| 110 |
+
cell = worksheet.find(str(member.id))
|
| 111 |
+
length = len(worksheet.col_values(1))
|
| 112 |
+
print(f"3")
|
| 113 |
+
if cell is None:
|
| 114 |
+
print(f"creating new record for {member}")
|
| 115 |
+
# if not, create new record
|
| 116 |
+
string_member_id = str(member.id)
|
| 117 |
+
xp = 10
|
| 118 |
+
current_level = calculate_level(xp)
|
| 119 |
+
member_name = member.name
|
| 120 |
+
worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}')
|
| 121 |
+
# initial role assignment
|
| 122 |
+
if current_level == 1:
|
| 123 |
+
if lvl1 not in member.roles:
|
| 124 |
+
return(1)
|
| 125 |
+
|
| 126 |
+
else:
|
| 127 |
+
print(f"4")
|
| 128 |
+
if cell:
|
| 129 |
+
# if so, update that row...
|
| 130 |
+
xp = worksheet.cell(cell.row, cell.col+2).value
|
| 131 |
+
xp = int(xp) + XP_PER_MESSAGE
|
| 132 |
+
current_level = calculate_level(xp)
|
| 133 |
+
print(f"updating record for {member}: {xp} xp")
|
| 134 |
+
# write with added xp
|
| 135 |
+
worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
|
| 136 |
+
# level up
|
| 137 |
+
print(f"current_level: {current_level}")
|
| 138 |
+
if current_level >= 2 and current_level <=30:
|
| 139 |
+
# pass current_level as int?
|
| 140 |
+
return(current_level)
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"blocking_io Error: {e}")
|
| 143 |
|
| 144 |
|
| 145 |
async def add_exp(member_id):
|
| 146 |
try:
|
| 147 |
+
await asyncio.sleep(1)
|
| 148 |
+
loop = asyncio.get_running_loop()
|
| 149 |
+
result = await loop.run_in_executor(None, blocking_io, member_id)
|
| 150 |
|
| 151 |
guild = bot.get_guild(879548962464493619)
|
| 152 |
member = guild.get_member(member_id)
|
|
|
|
| 188 |
21: lvl21, 22: lvl22, 23: lvl23, 24: lvl24, 25: lvl25, 26: lvl26, 27: lvl27, 28: lvl28, 29: lvl29, 30: lvl30,
|
| 189 |
}
|
| 190 |
|
| 191 |
+
if result == 1:
|
| 192 |
+
await member.add_roles(lvl1)
|
| 193 |
+
print(f"Gave {member} {lvl1}")
|
| 194 |
+
|
| 195 |
+
print(f"result: {result}")
|
| 196 |
+
if result >= 2:
|
| 197 |
+
current_level = result
|
| 198 |
+
current_role = lvls[current_level]
|
| 199 |
+
if current_role not in member.roles:
|
| 200 |
+
await member.add_roles(current_role)
|
| 201 |
+
print(f"Giving {member} {current_role}")
|
| 202 |
+
await member.remove_roles(lvls[current_level-1])
|
| 203 |
+
print(f"Removed {lvls[current_level-1]} from {member}")
|
| 204 |
+
#print(f"{member} Level up! {current_level-1} -> {current_level}!")
|
| 205 |
+
if current_role in member.roles:
|
| 206 |
+
await member.send(f"Level up! {current_level-1} -> {current_level}!")
|
| 207 |
+
|
| 208 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
except Exception as e:
|
| 210 |
print(f"add_exp Error: {e}")
|
| 211 |
|
|
|
|
| 213 |
@bot.event
|
| 214 |
async def on_message(message):
|
| 215 |
try:
|
| 216 |
+
if message.author.id not in bot_ids:
|
| 217 |
if "!help_xp" not in message.content:
|
| 218 |
print(f"adding exp from message {message.author}")
|
| 219 |
await asyncio.sleep(1)
|
|
|
|
| 236 |
|
| 237 |
|
| 238 |
@bot.command(name='update_leaderboard')
|
| 239 |
+
async def update_leaderboard(ctx, num_results: int = 10):
|
| 240 |
if ctx.author.id == 811235357663297546:
|
| 241 |
await asyncio.sleep(1)
|
| 242 |
worksheet = gspread_bot.open("levelbot").sheet1
|
|
|
|
| 247 |
channel = bot.get_channel(1197143964994773023)
|
| 248 |
message = await channel.fetch_message(1197148293164187678)
|
| 249 |
|
| 250 |
+
|
| 251 |
# for 3 lists
|
| 252 |
combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)]
|
| 253 |
combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list]
|
|
|
|
| 296 |
help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers"
|
| 297 |
await ctx.author.send(help_message)
|
| 298 |
|
| 299 |
+
|
| 300 |
+
@bot.command()
|
| 301 |
+
async def write_csv(ctx):
|
| 302 |
+
if ctx.author.id == 811235357663297546:
|
| 303 |
+
worksheet = gspread_bot.open("levelbot").sheet1
|
| 304 |
+
values = worksheet.get_all_values()
|
| 305 |
+
target_id = '811235357663297546'
|
| 306 |
+
variable_xp = 0
|
| 307 |
+
|
| 308 |
+
csv_file_path = 'test.csv'
|
| 309 |
+
with open(csv_file_path, 'w', newline='') as csv_file:
|
| 310 |
+
csv_writer = csv.writer(csv_file)
|
| 311 |
+
csv_writer.writerows(values)
|
| 312 |
+
|
| 313 |
+
# modifying csv data
|
| 314 |
+
with open(csv_file_path, 'r') as csv_file:
|
| 315 |
+
csv_reader = csv.reader(csv_file)
|
| 316 |
+
rows = list(csv_reader)
|
| 317 |
+
|
| 318 |
+
for row in rows:
|
| 319 |
+
print(row)
|
| 320 |
+
if target_id in row:
|
| 321 |
+
index_of_target = row.index(target_id)
|
| 322 |
+
|
| 323 |
+
# 2 to right
|
| 324 |
+
cell_to_increment_index = index_of_target + 2
|
| 325 |
+
|
| 326 |
+
# +10
|
| 327 |
+
try:
|
| 328 |
+
row[cell_to_increment_index] = str(int(row[cell_to_increment_index]) + variable_xp)
|
| 329 |
+
print(row[cell_to_increment_index])
|
| 330 |
+
except (IndexError, ValueError):
|
| 331 |
+
print(f"Error: Unable to increment cell at index {cell_to_increment_index}")
|
| 332 |
+
|
| 333 |
+
break
|
| 334 |
+
|
| 335 |
+
# write modified data and send in discord
|
| 336 |
+
with open(csv_file_path, 'w', newline='') as csv_file:
|
| 337 |
+
csv_writer = csv.writer(csv_file)
|
| 338 |
+
csv_writer.writerows(rows)
|
| 339 |
+
|
| 340 |
+
csv_data_str = '\n'.join(','.join(row) for row in rows)
|
| 341 |
+
csv_file = discord.File(csv_file_path)
|
| 342 |
+
|
| 343 |
+
await ctx.channel.send(content="csv", file=csv_file)
|
| 344 |
+
|
| 345 |
+
#target_user = await bot.fetch_user(int(target_id))
|
| 346 |
+
#await target_user.send(content="csv", file=csv_file)
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
@bot.command()
|
| 350 |
+
async def read_csv(ctx):
|
| 351 |
+
if ctx.author.id == 811235357663297546:
|
| 352 |
+
async for message in ctx.channel.history(limit=10):
|
| 353 |
+
if message.attachments:
|
| 354 |
+
attachment = message.attachments[0]
|
| 355 |
+
if attachment.filename.endswith('.csv'):
|
| 356 |
+
csv_content = await attachment.read()
|
| 357 |
+
csv_data = []
|
| 358 |
+
csv_lines = csv_content.decode('utf-8').splitlines()
|
| 359 |
+
csv_reader = csv.reader(csv_lines)
|
| 360 |
+
for row in csv_reader:
|
| 361 |
+
csv_data.append(row)
|
| 362 |
+
print(csv_data)
|
| 363 |
+
|
| 364 |
+
await ctx.send("discord -> read -> updated HF Hub")
|
| 365 |
+
# react to the given message, helps with explainability
|
| 366 |
+
await message.add_reaction('✅')
|
| 367 |
+
break
|
| 368 |
+
|
| 369 |
+
|
| 370 |
# embeds with user pfps?
|
| 371 |
# name, pfp, time in server....
|
| 372 |
|
| 373 |
+
|
| 374 |
# discord_level column
|
| 375 |
# pick 10 highest
|
| 376 |
# update
|
|
|
|
| 379 |
# count number of messages per user for every channel (total messages)
|
| 380 |
# fix sheet if necessary
|
| 381 |
|
| 382 |
+
|
| 383 |
# might need cell location data to pull both level and username at same time
|
| 384 |
# add emojis for some color
|
| 385 |
# check if members are still in the server
|
| 386 |
|
| 387 |
+
|
| 388 |
""""""
|
| 389 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 390 |
def run_bot():
|