-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjaybot.py
More file actions
125 lines (107 loc) · 4.27 KB
/
jaybot.py
File metadata and controls
125 lines (107 loc) · 4.27 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
113
114
115
116
117
118
119
120
121
122
123
124
125
import discord
import os
import aiohttp
import asyncio
import random
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
OLLAMA_URL = os.getenv("OLLAMA_URL")
MODEL = "llama3.2:latest"
GENERAL_PERSONA_PATH = "persona/jay-persona.txt"
MINECRAFT_PERSONA_PATH = "persona/minecraft-persona.txt"
MENTIONED_PERSONA_PATH = "persona/mentioned-persona.txt"
# Function to interact with the LLM
async def ask_llm(prompt: str):
payload = {
"model": MODEL,
"prompt": prompt,
"stream": False
}
print(f"Sending prompt to LLM:\n{prompt}\n")
# Make the HTTP request to the LLM server
async with aiohttp.ClientSession() as session:
async with session.post(OLLAMA_URL, json=payload) as resp:
data = await resp.json()
return data["response"]
# Bot is ready
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
print(f"Using Model: {MODEL} and Persona: {GENERAL_PERSONA_PATH}")
# Respond to messages
@client.event
async def on_message(message):
# Determine the prompt based on the message context
if message.author.display_name == "Jay": # Do not respond to self
return
elif "minecraft" in message.channel.name.lower() and "jay" in message.content.lower(): # Alternate prompt for Minecraft channel
history = await get_message_history(message.channel, limit=10)
prompt = minecraft_prompt(history)
elif "jay" in message.content.lower(): # Alternate prompt if mentioned
history = await get_message_history(message.channel, limit=10)
prompt = mentioned_prompt(history)
else: # Default prompt for all other messages
print("Say nothing.")
return
#history = await get_message_history(message.channel, limit=10)
#prompt = general_prompt(history)
if prompt:
reply = await ask_llm(prompt)
# Handle special commands for reactions or declining to comment
if "$NO_COMMENT" in reply or "NO_COMMENT" in reply:
print("Declined to comment.")
return
elif "$THUMBS_UP" in reply or "THUMBS_UP" in reply:
print("Reacted with 👍")
await message.add_reaction("👍")
elif "$THUMBS_DOWN" in reply or "THUMBS_DOWN" in reply:
print("Reacted with 👎")
await message.add_reaction("👎")
elif "$HEART" in reply or "HEART" in reply:
print("Reacted with ❤️")
await message.add_reaction("❤️")
else:
print(f"Responded with: {reply}")
await message.channel.send(reply)
print()
# Load last 10 messages from the Minecraft channel for context
async def get_message_history(channel, limit=10):
messages = []
async for msg in channel.history(limit=limit):
if "minecraft-bridge" in msg.author.display_name.lower() and msg.embeds: # Handle embedded messages (Minecraft death messages)
for embed in msg.embeds:
messages.append(embed.author.name)
elif "minecraft-bridge" in msg.author.display_name.lower(): # Special handling for minecraft-bridge messages
content = msg.content
if "»" in content: # Split username and message
content = content.split(" » ", 1)
messages.append(f"{content[0]}: {content[1]}")
else: # Server status messages, etc.
messages.append(content)
else: # Regular messages
messages.append(f"{msg.author.display_name}: {msg.content}")
# print(messages)
messages.reverse() # Oldest first
print(f"Loaded message history for context:\n{chr(10).join(messages)}\n")
return '\n'.join(messages)
# Create the Minecraft server prompt
def minecraft_prompt(history):
with open(MINECRAFT_PERSONA_PATH, "r") as F:
persona = F.read()
prompt = f"{persona}\n{history}\n"
return prompt
# Create the mentioned prompt
def mentioned_prompt(history):
with open(MENTIONED_PERSONA_PATH, "r") as F:
persona = F.read()
prompt = f"{persona}\n{history}\n"
return prompt
# Create the general purpose prompt
def general_prompt(history):
with open(GENERAL_PERSONA_PATH, "r") as F:
persona = F.read()
prompt = f"{persona}\n{history}\n"
return prompt
# Start the bot
client.run(os.getenv("DISCORD_TOKEN"))