-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
70 lines (53 loc) · 2.21 KB
/
Copy pathbot.py
File metadata and controls
70 lines (53 loc) · 2.21 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
import discord
import os
from dotenv import load_dotenv
from config import DISCORD_TOKEN
from llm_brain import generate_response
load_dotenv()
# User history storage: {user_id: [(role, text), ...]}
# We store only the last 10 messages to keep the context clean and to allow for continuation of stories
user_histories = {}
# Sends the messages to discord server
async def send_message(message, user_message, is_private):
user_id = message.author.id
# Get or create history for this user
if user_id not in user_histories:
user_histories[user_id] = []
history = user_histories[user_id]
try:
# Generate response using the conversation history
response = generate_response(user_message, history=history)
if is_private:
await message.author.send(response)
else:
await message.channel.send(response)
# Update history with the exchange
user_histories[user_id].append(("User", user_message))
user_histories[user_id].append(("PIO", response))
# Trim history to prevent context bloat (keep last 10 turns)
if len(user_histories[user_id]) > 20:
user_histories[user_id] = user_histories[user_id][-20:]
except Exception as e:
print(f"Error sending message: {e}")
# Starts discord server connection
def run_discord_bot():
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'{client.user} is now running (PIO_Bot_v2.1)') #Verification that bot is functional
@client.event
async def on_message(message):
if message.author == client.user:
return
username = str(message.author)
user_message = str(message.content)
channel = str(message.channel)
print(f'{username} said: "{user_message}" ({channel})')
if user_message.startswith('?'):
user_message = user_message[1:]
await send_message(message, user_message, is_private=True)
else:
await send_message(message, user_message, is_private=False)
client.run(DISCORD_TOKEN)