Spaces:
Sleeping
Sleeping
Commit ·
1c1c042
1
Parent(s): b072a48
Deploy Discord Bot
Browse files
app.py
CHANGED
|
@@ -36,29 +36,21 @@ def truncate_response(response: str) -> str:
|
|
| 36 |
return response[: 2000 - len(ending)] + ending
|
| 37 |
else:
|
| 38 |
return response
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
class MyClient(discord.Client):
|
| 42 |
-
def __init__(self, *, intents: discord.Intents):
|
| 43 |
-
super().__init__(intents=intents)
|
| 44 |
-
self.tree = discord.app_commands.CommandTree(self)
|
| 45 |
-
|
| 46 |
-
async def setup_hook(self):
|
| 47 |
-
synced = await self.tree.sync()
|
| 48 |
-
print(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 49 |
-
|
| 50 |
|
| 51 |
|
| 52 |
intents = discord.Intents.default()
|
| 53 |
intents.message_content = True
|
| 54 |
-
bot =
|
| 55 |
|
| 56 |
|
| 57 |
@bot.event
|
| 58 |
async def on_ready():
|
| 59 |
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
| 60 |
-
|
|
|
|
| 61 |
event.set()
|
|
|
|
|
|
|
| 62 |
|
| 63 |
thread_to_client = {}
|
| 64 |
thread_to_user = {}
|
|
@@ -71,15 +63,22 @@ thread_to_user = {}
|
|
| 71 |
# await ctx.send(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 72 |
|
| 73 |
|
| 74 |
-
@bot.
|
| 75 |
name="echo",
|
| 76 |
description="Enter some text to chat with the bot! Like this: /echo Hello, how are you?",
|
| 77 |
)
|
| 78 |
-
async def chat(
|
| 79 |
-
if
|
| 80 |
return
|
| 81 |
try:
|
| 82 |
-
message = await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
thread = await message.create_thread(name=prompt)
|
| 84 |
loop = asyncio.get_running_loop()
|
| 85 |
client = await loop.run_in_executor(None, get_client, None)
|
|
@@ -91,7 +90,7 @@ async def chat(interaction: discord.Interaction, prompt: str):
|
|
| 91 |
response = job.outputs()[-1]
|
| 92 |
await thread.send(truncate_response(response))
|
| 93 |
thread_to_client[thread.id] = client
|
| 94 |
-
thread_to_user[thread.id] =
|
| 95 |
except QueueError:
|
| 96 |
await thread.send(
|
| 97 |
"The gradio space powering this bot is really busy! Please try again later!"
|
|
|
|
| 36 |
return response[: 2000 - len(ending)] + ending
|
| 37 |
else:
|
| 38 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
intents = discord.Intents.default()
|
| 42 |
intents.message_content = True
|
| 43 |
+
bot = commands.Bot(command_prefix="/", intents=intents)
|
| 44 |
|
| 45 |
|
| 46 |
@bot.event
|
| 47 |
async def on_ready():
|
| 48 |
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
| 49 |
+
synced = await bot.tree.sync()
|
| 50 |
+
await ctx.send(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 51 |
event.set()
|
| 52 |
+
print("------")
|
| 53 |
+
|
| 54 |
|
| 55 |
thread_to_client = {}
|
| 56 |
thread_to_user = {}
|
|
|
|
| 63 |
# await ctx.send(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 64 |
|
| 65 |
|
| 66 |
+
@bot.hybrid_command(
|
| 67 |
name="echo",
|
| 68 |
description="Enter some text to chat with the bot! Like this: /echo Hello, how are you?",
|
| 69 |
)
|
| 70 |
+
async def chat(ctx, prompt: str):
|
| 71 |
+
if ctx.author.id == bot.user.id:
|
| 72 |
return
|
| 73 |
try:
|
| 74 |
+
message = await ctx.send("Creating thread...")
|
| 75 |
+
|
| 76 |
+
# User triggered bot via !echo
|
| 77 |
+
if ctx.message.content:
|
| 78 |
+
prompt = ctx.message.content.replace(
|
| 79 |
+
f"{bot.command_prefix}echo", ""
|
| 80 |
+
).strip()
|
| 81 |
+
|
| 82 |
thread = await message.create_thread(name=prompt)
|
| 83 |
loop = asyncio.get_running_loop()
|
| 84 |
client = await loop.run_in_executor(None, get_client, None)
|
|
|
|
| 90 |
response = job.outputs()[-1]
|
| 91 |
await thread.send(truncate_response(response))
|
| 92 |
thread_to_client[thread.id] = client
|
| 93 |
+
thread_to_user[thread.id] = ctx.author.id
|
| 94 |
except QueueError:
|
| 95 |
await thread.send(
|
| 96 |
"The gradio space powering this bot is really busy! Please try again later!"
|