standarovskid commited on
Commit
583d369
·
verified ·
1 Parent(s): 639fabb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -74
app.py CHANGED
@@ -5,16 +5,13 @@ import logging
5
  import threading
6
  from threading import Event
7
  from typing import Optional
8
-
9
  import discord
10
  import gradio as gr
11
  from discord import Permissions
12
  from discord.ext import commands
13
  from discord.utils import oauth_url
14
-
15
  import gradio_client as grc
16
  from gradio_client.utils import QueueError
17
-
18
  from aiohttp.resolver import AsyncResolver
19
 
20
  # Setup Discord bot
@@ -23,24 +20,20 @@ intents = discord.Intents.default()
23
  intents.message_content = True
24
  intents.members = True
25
 
26
-
27
  event = Event()
28
 
29
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
30
 
31
-
32
  async def wait(job):
33
  while not job.done():
34
  await asyncio.sleep(0.2)
35
 
36
-
37
  def get_client(session: Optional[str] = None) -> grc.Client:
38
  client = grc.Client("https://standarovskid-bot.hf.space/", hf_token=os.getenv("HF_TOKEN"))
39
  if session:
40
  client.session_hash = session
41
  return client
42
 
43
-
44
  def truncate_response(response: str) -> str:
45
  ending = "...\nTruncating response to 2000 characters due to discord api limits."
46
  if len(response) > 2000:
@@ -48,7 +41,6 @@ def truncate_response(response: str) -> str:
48
  else:
49
  return response
50
 
51
-
52
  async def main():
53
  # Setup DNS resolver to avoid potential issues
54
  resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
@@ -64,19 +56,9 @@ async def main():
64
  print(f"Synced commands: {', '.join([s.name for s in synced])}.")
65
  event.set()
66
  print("------")
67
-
68
-
69
  thread_to_client = {}
70
  thread_to_user = {}
71
 
72
-
73
- # @bot.command()
74
- # @commands.is_owner()
75
- # async def sync(ctx) -> None:
76
- # synced = await bot.tree.sync()
77
- # await ctx.send(f"Synced commands: {', '.join([s.name for s in synced])}.")
78
-
79
-
80
  @bot.hybrid_command(
81
  name="echo",
82
  description="Enter some text to chat with the bot! Like this: /echo Hello, how are you?",
@@ -109,11 +91,9 @@ async def main():
109
  await thread.send(
110
  "The gradio space powering this bot is really busy! Please try again later!"
111
  )
112
-
113
  except Exception as e:
114
  print(f"{e}")
115
 
116
-
117
  async def continue_chat(message):
118
  """Continues a given conversation based on chathistory"""
119
  try:
@@ -133,7 +113,6 @@ async def main():
133
  except Exception as e:
134
  print(f"Error: {e}")
135
 
136
-
137
  @bot.event
138
  async def on_message(message):
139
  """Continue the chat"""
@@ -148,7 +127,6 @@ async def main():
148
  except Exception as e:
149
  print(f"Error: {e}")
150
 
151
-
152
  # running in thread
153
  def run_bot():
154
  if not DISCORD_TOKEN:
@@ -157,61 +135,9 @@ async def main():
157
  else:
158
  bot.run(DISCORD_TOKEN)
159
 
160
-
161
  threading.Thread(target=run_bot).start()
162
 
163
  event.wait()
164
 
165
- if not DISCORD_TOKEN:
166
- welcome_message = """
167
-
168
- ## You have not specified a DISCORD_TOKEN, which means you have not created a bot account. Please follow these steps:
169
-
170
- ### 1. Go to https://discord.com/developers/applications and click 'New Application'
171
-
172
- ### 2. Give your bot a name 🤖
173
-
174
- ![](https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/BotName.png)
175
-
176
- ## 3. In Settings > Bot, click the 'Reset Token' button to get a new token. Write it down and keep it safe 🔐
177
-
178
- ![](https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/ResetToken.png)
179
-
180
- ## 4. Optionally make the bot public if you want anyone to be able to add it to their servers
181
-
182
- ## 5. Scroll down and enable 'Message Content Intent' under 'Priviledged Gateway Intents'
183
-
184
- ![](https://gradio-builds.s3.amazonaws.com/demo-files/discordbots/MessageContentIntent.png)
185
-
186
- ## 6. Save your changes!
187
-
188
- ## 7. The token from step 3 is the DISCORD_TOKEN. Rerun the deploy_discord command, e.g client.deploy_discord(discord_bot_token=DISCORD_TOKEN, ...), or add the token as a space secret manually.
189
- """
190
- else:
191
- permissions = Permissions(326417525824)
192
- url = oauth_url(bot.user.id, permissions=permissions)
193
- welcome_message = f"""
194
- ## Add this bot to your server by clicking this link:
195
-
196
- {url}
197
-
198
- ## How to use it?
199
-
200
- The bot can be triggered via `!echo` followed by your text prompt.
201
-
202
- ## Enabling slash commands
203
-
204
- If you are the owner of this bot, call the `!sync` command from your discord server.
205
- This will allow anyone in your server to call the bot via `/echo`.
206
- This is known as a slash command and is a nicer experience than calling the bot via `!echo`.
207
-
208
- After calling `!sync`, it may take a few minutes for `/echo` to be recognized as a valid command
209
- in your server.
210
-
211
- ⚠️ Note ⚠️: It is best to create a separate bot per command if you intend to use slash commands. Also make sure
212
- none of your bots have matching command names.
213
- """
214
-
215
-
216
  if __name__ == "__main__":
217
  asyncio.run(main())
 
5
  import threading
6
  from threading import Event
7
  from typing import Optional
 
8
  import discord
9
  import gradio as gr
10
  from discord import Permissions
11
  from discord.ext import commands
12
  from discord.utils import oauth_url
 
13
  import gradio_client as grc
14
  from gradio_client.utils import QueueError
 
15
  from aiohttp.resolver import AsyncResolver
16
 
17
  # Setup Discord bot
 
20
  intents.message_content = True
21
  intents.members = True
22
 
 
23
  event = Event()
24
 
25
  DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
26
 
 
27
  async def wait(job):
28
  while not job.done():
29
  await asyncio.sleep(0.2)
30
 
 
31
  def get_client(session: Optional[str] = None) -> grc.Client:
32
  client = grc.Client("https://standarovskid-bot.hf.space/", hf_token=os.getenv("HF_TOKEN"))
33
  if session:
34
  client.session_hash = session
35
  return client
36
 
 
37
  def truncate_response(response: str) -> str:
38
  ending = "...\nTruncating response to 2000 characters due to discord api limits."
39
  if len(response) > 2000:
 
41
  else:
42
  return response
43
 
 
44
  async def main():
45
  # Setup DNS resolver to avoid potential issues
46
  resolver = AsyncResolver(nameservers=["1.1.1.1", "8.8.8.8"])
 
56
  print(f"Synced commands: {', '.join([s.name for s in synced])}.")
57
  event.set()
58
  print("------")
 
 
59
  thread_to_client = {}
60
  thread_to_user = {}
61
 
 
 
 
 
 
 
 
 
62
  @bot.hybrid_command(
63
  name="echo",
64
  description="Enter some text to chat with the bot! Like this: /echo Hello, how are you?",
 
91
  await thread.send(
92
  "The gradio space powering this bot is really busy! Please try again later!"
93
  )
 
94
  except Exception as e:
95
  print(f"{e}")
96
 
 
97
  async def continue_chat(message):
98
  """Continues a given conversation based on chathistory"""
99
  try:
 
113
  except Exception as e:
114
  print(f"Error: {e}")
115
 
 
116
  @bot.event
117
  async def on_message(message):
118
  """Continue the chat"""
 
127
  except Exception as e:
128
  print(f"Error: {e}")
129
 
 
130
  # running in thread
131
  def run_bot():
132
  if not DISCORD_TOKEN:
 
135
  else:
136
  bot.run(DISCORD_TOKEN)
137
 
 
138
  threading.Thread(target=run_bot).start()
139
 
140
  event.wait()
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  if __name__ == "__main__":
143
  asyncio.run(main())