-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
112 lines (91 loc) · 3.6 KB
/
bot.py
File metadata and controls
112 lines (91 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import discord
from discord.ext import commands
from cerebras.cloud.sdk import Cerebras
from threading import Thread
from flask import Flask
# Initialize Discord bot
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Initialize Cerebras client
cerebras_client = Cerebras(
api_key=os.environ.get("CEREBRAS_API_KEY"),
)
# Store conversation history per user
conversation_history = {}
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
print(f'Bot is in {len(bot.guilds)} guilds')
@bot.command(name='ask', help='Ask the AI a question. Usage: !ask <your question>')
async def ask(ctx, *, question: str):
"""Ask the AI a question"""
user_id = ctx.author.id
# Initialize conversation history for new users
if user_id not in conversation_history:
conversation_history[user_id] = []
# Add user message to history
conversation_history[user_id].append({
"role": "user",
"content": question
})
# Keep only last 10 messages to avoid token limits
if len(conversation_history[user_id]) > 10:
conversation_history[user_id] = conversation_history[user_id][-10:]
try:
async with ctx.typing():
# Get response from Cerebras
chat_completion = cerebras_client.chat.completions.create(
messages=conversation_history[user_id],
model="llama-3.3-70b",
)
response = chat_completion.choices[0].message.content
# Add assistant response to history
conversation_history[user_id].append({
"role": "assistant",
"content": response
})
# Split long responses to fit Discord's 2000 character limit
if len(response) > 2000:
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
for chunk in chunks:
await ctx.send(chunk)
else:
await ctx.send(response)
except Exception as e:
await ctx.send(f"An error occurred: {str(e)}")
print(f"Error: {e}")
@bot.command(name='reset', help='Reset your conversation history')
async def reset(ctx):
"""Reset conversation history for the user"""
user_id = ctx.author.id
if user_id in conversation_history:
conversation_history[user_id] = []
await ctx.send("Your conversation history has been reset!")
@bot.command(name='ping', help='Check if the bot is responsive')
async def ping(ctx):
"""Simple ping command to check bot responsiveness"""
await ctx.send(f'Pong! Latency: {round(bot.latency * 1000)}ms')
# Run the bot
if __name__ == "__main__":
# Create a simple Flask app to keep the web service alive
app = Flask(__name__)
@app.route('/')
def home():
return "Discord Bot is running!"
@app.route('/health')
def health():
return "OK", 200
# Run Flask in a separate thread
def run_flask():
port = int(os.environ.get("PORT", 10000))
app.run(host='0.0.0.0', port=port)
flask_thread = Thread(target=run_flask)
flask_thread.daemon = True
flask_thread.start()
# Run the Discord bot
discord_token = os.environ.get("DISCORD_BOT_TOKEN")
if not discord_token:
raise ValueError("DISCORD_BOT_TOKEN environment variable not set")
bot.run(discord_token)