Spaces:
Sleeping
Sleeping
discord bot
Browse files- app.py +105 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# bot.py
|
2 |
+
import discord
|
3 |
+
from gradio_client import Client
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Get tokens from environment variables (Hugging Face Spaces secrets)
|
8 |
+
TOKEN = os.getenv("DISCORD_TOKEN")
|
9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
10 |
+
|
11 |
+
# Check if tokens are available
|
12 |
+
if not TOKEN or not HF_TOKEN:
|
13 |
+
raise ValueError("DISCORD_TOKEN and HF_TOKEN must be set as environment variables in Spaces secrets")
|
14 |
+
|
15 |
+
# Set up Discord intents
|
16 |
+
intents = discord.Intents.default()
|
17 |
+
intents.message_content = True
|
18 |
+
|
19 |
+
# Initialize Discord client
|
20 |
+
client = discord.Client(intents=intents)
|
21 |
+
|
22 |
+
# Initialize Hugging Face Inference client
|
23 |
+
hf_client = InferenceClient(api_key=HF_TOKEN)
|
24 |
+
|
25 |
+
# Function to process message and get response
|
26 |
+
async def get_ai_response(message_content):
|
27 |
+
try:
|
28 |
+
messages = [
|
29 |
+
{"role": "user", "content": message_content}
|
30 |
+
]
|
31 |
+
|
32 |
+
# Create a streaming response
|
33 |
+
response = ""
|
34 |
+
stream = hf_client.chat.completions.create(
|
35 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
36 |
+
messages=messages,
|
37 |
+
temperature=0.5,
|
38 |
+
max_tokens=2048,
|
39 |
+
top_p=0.7,
|
40 |
+
stream=True
|
41 |
+
)
|
42 |
+
|
43 |
+
# Collect the streamed response
|
44 |
+
for chunk in stream:
|
45 |
+
content = chunk.choices[0].delta.content
|
46 |
+
if content:
|
47 |
+
response += content
|
48 |
+
|
49 |
+
return response if response else "I couldn't generate a response."
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
return f"An error occurred: {str(e)}"
|
53 |
+
|
54 |
+
@client.event
|
55 |
+
async def on_ready():
|
56 |
+
print(f'We have logged in as {client.user}')
|
57 |
+
|
58 |
+
@client.event
|
59 |
+
async def on_message(message):
|
60 |
+
# Ignore messages from the bot itself
|
61 |
+
if message.author == client.user:
|
62 |
+
return
|
63 |
+
|
64 |
+
# Check if the bot is mentioned in the message
|
65 |
+
if client.user in message.mentions:
|
66 |
+
# Extract the message content without the bot mention
|
67 |
+
clean_message = message.content.replace(f"<@{client.user.id}>", "").strip()
|
68 |
+
|
69 |
+
if not clean_message:
|
70 |
+
await message.channel.send("Please provide some text for me to respond to!")
|
71 |
+
return
|
72 |
+
|
73 |
+
# Send initial response to show bot is processing
|
74 |
+
processing_message = await message.channel.send("Processing your request...")
|
75 |
+
|
76 |
+
# Get AI response
|
77 |
+
response = await get_ai_response(clean_message)
|
78 |
+
|
79 |
+
# Split response if it's too long for Discord's 2000 character limit
|
80 |
+
if len(response) > 2000:
|
81 |
+
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
|
82 |
+
await processing_message.delete()
|
83 |
+
for chunk in chunks:
|
84 |
+
await message.channel.send(chunk)
|
85 |
+
else:
|
86 |
+
await processing_message.edit(content=response)
|
87 |
+
|
88 |
+
# Error handling for connection issues
|
89 |
+
@client.event
|
90 |
+
async def on_error(event, *args, **kwargs):
|
91 |
+
print(f"An error occurred: {event}")
|
92 |
+
with open('error.log', 'a') as f:
|
93 |
+
f.write(f"{event}\n")
|
94 |
+
|
95 |
+
# Run the bot
|
96 |
+
def main():
|
97 |
+
try:
|
98 |
+
client.run(TOKEN)
|
99 |
+
except Exception as e:
|
100 |
+
print(f"Failed to start bot: {e}")
|
101 |
+
with open('error.log', 'a') as f:
|
102 |
+
f.write(f"Failed to start bot: {e}\n")
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
discord.py
|
2 |
+
huggingface_hub
|
3 |
+
gradio
|