lunarflu HF staff commited on
Commit
cbfe4d5
·
verified ·
1 Parent(s): 960e451
Files changed (1) hide show
  1. app.py +13 -14
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
- # Setup an async scheduler for periodic tasks like a bot restart
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 to verify a member and add the role if needed.
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() # Ensure all guild members are cached
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 based on how often you expect updates
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 mode so it doesn't interfere with the bot's event loop
126
- demo.launch(share=False, block=False)
127
- # Start the Discord bot (this will run until the bot is stopped)
128
- await bot.start(DISCORD_TOKEN)
 
 
 
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: