Spaces:
Paused
Paused
fix?
Browse files
app.py
CHANGED
|
@@ -14,10 +14,10 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
| 14 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 15 |
|
| 16 |
# Create Discord bot with all intents
|
| 17 |
-
intents = discord.Intents.all()
|
| 18 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
scheduler = AsyncIOScheduler()
|
| 22 |
|
| 23 |
def restart_bot():
|
|
@@ -31,7 +31,7 @@ def periodic_restart():
|
|
| 31 |
|
| 32 |
async def process_row(row, guild, role):
|
| 33 |
"""
|
| 34 |
-
Process a single CSV row
|
| 35 |
"""
|
| 36 |
hf_user_name = row['hf_user_name']
|
| 37 |
if pd.notna(hf_user_name) and hf_user_name.lower() != 'n/a':
|
|
@@ -44,6 +44,7 @@ async def process_row(row, guild, role):
|
|
| 44 |
|
| 45 |
if not member:
|
| 46 |
return
|
|
|
|
| 47 |
if role not in member.roles:
|
| 48 |
try:
|
| 49 |
await member.add_roles(role)
|
|
@@ -63,7 +64,6 @@ async def give_verified_roles():
|
|
| 63 |
"""
|
| 64 |
while True:
|
| 65 |
try:
|
| 66 |
-
# Fetch the CSV from Google Sheets
|
| 67 |
async with aiohttp.ClientSession() as session:
|
| 68 |
try:
|
| 69 |
async with session.get(
|
|
@@ -85,7 +85,6 @@ async def give_verified_roles():
|
|
| 85 |
await asyncio.sleep(30)
|
| 86 |
continue
|
| 87 |
|
| 88 |
-
# Get the guild and role objects
|
| 89 |
guild = bot.get_guild(879548962464493619)
|
| 90 |
if not guild:
|
| 91 |
print("Guild not found.")
|
|
@@ -97,15 +96,13 @@ async def give_verified_roles():
|
|
| 97 |
await asyncio.sleep(30)
|
| 98 |
continue
|
| 99 |
|
| 100 |
-
await guild.chunk() #
|
| 101 |
|
| 102 |
-
# Process all rows concurrently
|
| 103 |
tasks = [process_row(row, guild, role) for _, row in global_df.iterrows()]
|
| 104 |
await asyncio.gather(*tasks)
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Error in give_verified_roles loop: {e}")
|
| 107 |
-
# Adjust the sleep interval
|
| 108 |
-
await asyncio.sleep(30)
|
| 109 |
|
| 110 |
@bot.event
|
| 111 |
async def on_ready():
|
|
@@ -114,7 +111,6 @@ async def on_ready():
|
|
| 114 |
# Start the background role verification loop
|
| 115 |
bot.loop.create_task(give_verified_roles())
|
| 116 |
|
| 117 |
-
# A simple Gradio interface function
|
| 118 |
def greet(name):
|
| 119 |
return "Hello " + name + "!"
|
| 120 |
|
|
@@ -122,10 +118,13 @@ def greet(name):
|
|
| 122 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 123 |
|
| 124 |
async def main():
|
| 125 |
-
# Launch Gradio in non-blocking
|
| 126 |
-
demo.launch
|
| 127 |
-
#
|
| 128 |
-
await
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
| 131 |
try:
|
|
|
|
| 14 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 15 |
|
| 16 |
# Create Discord bot with all intents
|
| 17 |
+
intents = discord.Intents.all()
|
| 18 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 19 |
|
| 20 |
+
# Set up an async scheduler for periodic tasks (like a bot restart)
|
| 21 |
scheduler = AsyncIOScheduler()
|
| 22 |
|
| 23 |
def restart_bot():
|
|
|
|
| 31 |
|
| 32 |
async def process_row(row, guild, role):
|
| 33 |
"""
|
| 34 |
+
Process a single CSV row: if the Discord member associated with the row hasn't received the role, add it.
|
| 35 |
"""
|
| 36 |
hf_user_name = row['hf_user_name']
|
| 37 |
if pd.notna(hf_user_name) and hf_user_name.lower() != 'n/a':
|
|
|
|
| 44 |
|
| 45 |
if not member:
|
| 46 |
return
|
| 47 |
+
|
| 48 |
if role not in member.roles:
|
| 49 |
try:
|
| 50 |
await member.add_roles(role)
|
|
|
|
| 64 |
"""
|
| 65 |
while True:
|
| 66 |
try:
|
|
|
|
| 67 |
async with aiohttp.ClientSession() as session:
|
| 68 |
try:
|
| 69 |
async with session.get(
|
|
|
|
| 85 |
await asyncio.sleep(30)
|
| 86 |
continue
|
| 87 |
|
|
|
|
| 88 |
guild = bot.get_guild(879548962464493619)
|
| 89 |
if not guild:
|
| 90 |
print("Guild not found.")
|
|
|
|
| 96 |
await asyncio.sleep(30)
|
| 97 |
continue
|
| 98 |
|
| 99 |
+
await guild.chunk() # Cache all guild members
|
| 100 |
|
|
|
|
| 101 |
tasks = [process_row(row, guild, role) for _, row in global_df.iterrows()]
|
| 102 |
await asyncio.gather(*tasks)
|
| 103 |
except Exception as e:
|
| 104 |
print(f"Error in give_verified_roles loop: {e}")
|
| 105 |
+
await asyncio.sleep(30) # Adjust the sleep interval as needed
|
|
|
|
| 106 |
|
| 107 |
@bot.event
|
| 108 |
async def on_ready():
|
|
|
|
| 111 |
# Start the background role verification loop
|
| 112 |
bot.loop.create_task(give_verified_roles())
|
| 113 |
|
|
|
|
| 114 |
def greet(name):
|
| 115 |
return "Hello " + name + "!"
|
| 116 |
|
|
|
|
| 118 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 119 |
|
| 120 |
async def main():
|
| 121 |
+
# Launch Gradio in a separate thread (non-blocking) without using the unsupported 'block' parameter
|
| 122 |
+
gradio_thread = asyncio.to_thread(demo.launch, share=False)
|
| 123 |
+
# Run both the Discord bot and Gradio concurrently
|
| 124 |
+
await asyncio.gather(
|
| 125 |
+
bot.start(DISCORD_TOKEN),
|
| 126 |
+
gradio_thread
|
| 127 |
+
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
try:
|