standarovskid commited on
Commit
ea1c14b
·
verified ·
1 Parent(s): f0b176c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -23
app.py CHANGED
@@ -3,7 +3,6 @@ import uvloop
3
  import os
4
  import logging
5
  import threading
6
- from threading import Event
7
  from typing import Optional
8
  import discord
9
  import gradio as gr
@@ -14,6 +13,7 @@ import gradio_client as grc
14
  from gradio_client.utils import QueueError
15
  import aiohttp
16
  from aiohttp.resolver import AsyncResolver
 
17
 
18
  # Setup Discord bot
19
  logging.basicConfig(level=logging.INFO)
@@ -21,8 +21,6 @@ intents = discord.Intents.default()
21
  intents.message_content = True
22
  intents.members = True
23
 
24
- event = Event()
25
-
26
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
27
 
28
  async def wait(job):
@@ -35,13 +33,6 @@ def get_client(session: Optional[str] = None) -> grc.Client:
35
  client.session_hash = session
36
  return client
37
 
38
- def truncate_response(response: str) -> str:
39
- ending = "...\nTruncating response to 2000 characters due to discord api limits."
40
- if len(response) > 2000:
41
- return response[: 2000 - len(ending)] + ending
42
- else:
43
- return response
44
-
45
  async def main():
46
  # Setup DNS resolver to avoid potential issues
47
  resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
@@ -85,7 +76,7 @@ async def main():
85
  try:
86
  job.result()
87
  response = job.outputs()[-1]
88
- await thread.send(truncate_response(response))
89
  thread_to_client[thread.id] = client
90
  thread_to_user[thread.id] = ctx.author.id
91
  except QueueError:
@@ -105,7 +96,7 @@ async def main():
105
  try:
106
  job.result()
107
  response = job.outputs()[-1]
108
- await message.reply(truncate_response(response))
109
  except QueueError:
110
  await message.reply(
111
  "The gradio space powering this bot is really busy! Please try again later!"
@@ -128,17 +119,9 @@ async def main():
128
  except Exception as e:
129
  print(f"Error: {e}")
130
 
131
- # running in thread
132
- def run_bot():
133
- if not DISCORD_TOKEN:
134
- print("DISCORD_TOKEN NOT SET")
135
- event.set()
136
- else:
137
- bot.run(DISCORD_TOKEN)
138
-
139
- threading.Thread(target=run_bot).start()
140
-
141
- event.wait()
142
 
143
  if __name__ == "__main__":
144
  asyncio.run(main())
 
3
  import os
4
  import logging
5
  import threading
 
6
  from typing import Optional
7
  import discord
8
  import gradio as gr
 
13
  from gradio_client.utils import QueueError
14
  import aiohttp
15
  from aiohttp.resolver import AsyncResolver
16
+ import utils
17
 
18
  # Setup Discord bot
19
  logging.basicConfig(level=logging.INFO)
 
21
  intents.message_content = True
22
  intents.members = True
23
 
 
 
24
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
25
 
26
  async def wait(job):
 
33
  client.session_hash = session
34
  return client
35
 
 
 
 
 
 
 
 
36
  async def main():
37
  # Setup DNS resolver to avoid potential issues
38
  resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
 
76
  try:
77
  job.result()
78
  response = job.outputs()[-1]
79
+
80
  thread_to_client[thread.id] = client
81
  thread_to_user[thread.id] = ctx.author.id
82
  except QueueError:
 
96
  try:
97
  job.result()
98
  response = job.outputs()[-1]
99
+
100
  except QueueError:
101
  await message.reply(
102
  "The gradio space powering this bot is really busy! Please try again later!"
 
119
  except Exception as e:
120
  print(f"Error: {e}")
121
 
122
+ # Start keepalive server and bot
123
+ utils.keepalive_run()
124
+ await bot.start(os.getenv('DISCORD_TOKEN'))
 
 
 
 
 
 
 
 
125
 
126
  if __name__ == "__main__":
127
  asyncio.run(main())