-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
95 lines (87 loc) · 4.77 KB
/
config.py
File metadata and controls
95 lines (87 loc) · 4.77 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
import os
from dotenv import load_dotenv
import re
import logging
logger = logging.getLogger(__name__)
# Load .env file
env_path = os.path.join(os.path.dirname(__file__), '.env')
if not os.path.exists(env_path):
logger.error(f".env file not found at {env_path}")
raise FileNotFoundError(f".env file not found at {env_path}")
load_dotenv(env_path)
logger.info(f"Loaded .env file from {env_path}")
class Config:
def __init__(self):
# Load and validate Discord token
self.discord_token = os.getenv("DISCORD_TOKEN")
if self.discord_token:
self.discord_token = self.discord_token.strip() # Remove whitespace
logger.debug(f"Discord token length: {len(self.discord_token)}, first 10 chars: {self.discord_token[:10]}")
# Discord tokens are base64-like, ~59-100 chars
if not re.match(r'^[A-Za-z0-9._-]{50,100}$', self.discord_token):
logger.error(f"Invalid Discord token format: {self.discord_token[:10]}... (truncated)")
raise ValueError("Invalid Discord token format. Ensure no spaces, quotes, or invalid characters.")
else:
logger.error("DISCORD_TOKEN not found in environment variables")
raise ValueError("DISCORD_TOKEN not found in environment variables")
logger.info(f"Successfully loaded discord token: {self.discord_token[:10]}... (truncated for security)")
# Load and validate Pollinations token
self.pollinations_token = os.getenv("POLLINATIONS_TOKEN")
if self.pollinations_token:
self.pollinations_token = self.pollinations_token.strip()
logger.debug(f"Pollinations token length: {len(self.pollinations_token)}, first 10 chars: {self.pollinations_token[:10]}")
if not re.match(r'^[A-Za-z0-9]{16}$', self.pollinations_token): # Assuming 16-char alphanumeric format from example
logger.error(f"Invalid Pollinations token format: {self.pollinations_token[:10]}... (truncated)")
raise ValueError("Invalid Pollinations token format. Ensure it's correct and no invalid characters.")
else:
logger.error("POLLINATIONS_TOKEN not found in environment variables")
raise ValueError("POLLINATIONS_TOKEN not found in environment variables")
logger.info(f"Successfully loaded pollinations token: {self.pollinations_token[:10]}... (truncated for security)")
# Bot configuration
# Default to the gpt-5-nano model instead of the legacy "unity" model
self.default_model = "gpt-5-nano"
try:
with open("system_instructions.txt", "r", encoding="utf-8") as f:
self.system_instructions = f.read().strip()
except FileNotFoundError:
logger.error("system_instructions.txt not found")
raise FileNotFoundError("system_instructions.txt not found")
try:
with open("info_request_instructions.txt", "r", encoding="utf-8") as f:
self.info_request_instructions = f.read().strip()
except FileNotFoundError:
logger.error("info_request_instructions.txt not found")
raise FileNotFoundError("info_request_instructions.txt not found")
self.api_url = f"https://text.pollinations.ai/openai?token={self.pollinations_token}"
self.models_url = "https://text.pollinations.ai/models"
allowed_channels_env = os.getenv("ALLOWED_CHANNELS", "").strip()
if allowed_channels_env:
# Support comma or whitespace separated lists and remove empty entries
self.allowed_channels = [
c.strip() for c in re.split(r"[\s,]+", allowed_channels_env) if c.strip()
]
else:
# Empty list means all channels are allowed
self.allowed_channels = []
logger.info(
"Allowed channels: %s",
self.allowed_channels if self.allowed_channels else "all",
)
self.max_history = 20
self.max_memories = 5
self.code_keywords = [
"code", "script", "program", "function", "class",
"method", "javascript", "python", "java", "html", "css"
]
self.image_keywords = [
"image", "picture", "photo", "generate", "create", "draw", "art"
]
self.memory_regex = r"\$\$ memory \$\$([\s\S]*?)\$\$ \/memory \$\$"
self.code_block_regex = r"```(\w*)\n([\s\S]*?)\n```"
self.url_regex = r"https?://[^\s>]+"
def is_image_request(self, message: str) -> bool:
return any(keyword in message.lower() for keyword in self.image_keywords)
def is_code_request(self, message: str) -> bool:
return any(keyword in message.lower() for keyword in self.code_keywords)
def extract_image_prompt(self, message: str) -> str:
return message.strip()