Spaces:
Sleeping
Sleeping
Commit ·
126a245
1
Parent(s): f7b80dd
Deploy Discord Bot
Browse files
app.py
CHANGED
|
@@ -36,18 +36,31 @@ def truncate_response(response: str) -> str:
|
|
| 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 =
|
| 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 |
-
print(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 51 |
print("------")
|
| 52 |
event.set()
|
| 53 |
|
|
@@ -62,21 +75,21 @@ thread_to_user = {}
|
|
| 62 |
# await ctx.send(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 63 |
|
| 64 |
|
| 65 |
-
@bot.command(
|
| 66 |
name="chat",
|
| 67 |
description="Enter some text to chat with the bot! Like this: /chat Hello, how are you?",
|
| 68 |
)
|
| 69 |
-
async def chat(
|
| 70 |
if ctx.author.id == bot.user.id:
|
| 71 |
return
|
| 72 |
try:
|
| 73 |
-
message = await
|
| 74 |
|
| 75 |
-
# User triggered bot via !chat
|
| 76 |
-
if ctx.message.content:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
thread = await message.create_thread(name=prompt)
|
| 82 |
loop = asyncio.get_running_loop()
|
|
|
|
| 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 |
+
# In this basic example, we just synchronize the app commands to one guild.
|
| 47 |
+
# Instead of specifying a guild to every command, we copy over our global commands instead.
|
| 48 |
+
# By doing so, we don't have to wait up to an hour until they are shown to the end-user.
|
| 49 |
+
async def setup_hook(self):
|
| 50 |
+
# This copies the global commands over to your guild.
|
| 51 |
+
synced = await self.tree.sync()
|
| 52 |
+
print(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 53 |
+
|
| 54 |
|
| 55 |
|
| 56 |
intents = discord.Intents.default()
|
| 57 |
intents.message_content = True
|
| 58 |
+
bot = MyClient(intents=intents)
|
| 59 |
|
| 60 |
|
| 61 |
@bot.event
|
| 62 |
async def on_ready():
|
| 63 |
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
|
|
|
|
|
|
| 64 |
print("------")
|
| 65 |
event.set()
|
| 66 |
|
|
|
|
| 75 |
# await ctx.send(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 76 |
|
| 77 |
|
| 78 |
+
@bot.tree.command(
|
| 79 |
name="chat",
|
| 80 |
description="Enter some text to chat with the bot! Like this: /chat Hello, how are you?",
|
| 81 |
)
|
| 82 |
+
async def chat(interaction: discord.Interaction, prompt: str):
|
| 83 |
if ctx.author.id == bot.user.id:
|
| 84 |
return
|
| 85 |
try:
|
| 86 |
+
message = await interaction.channel.send("Creating thread...")
|
| 87 |
|
| 88 |
+
# # User triggered bot via !chat
|
| 89 |
+
# if ctx.message.content:
|
| 90 |
+
# prompt = ctx.message.content.replace(
|
| 91 |
+
# f"{bot.command_prefix}chat", ""
|
| 92 |
+
# ).strip()
|
| 93 |
|
| 94 |
thread = await message.create_thread(name=prompt)
|
| 95 |
loop = asyncio.get_running_loop()
|