-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
90 lines (82 loc) · 4.11 KB
/
config.py
File metadata and controls
90 lines (82 loc) · 4.11 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
import os
from dotenv import load_dotenv
import re
import logging
logger = logging.getLogger(__name__)
# Load .env file if present, but allow environment variables to be set on the system
env_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(env_path):
# `override=False` ensures system environment variables take precedence
load_dotenv(env_path, override=False)
logger.info(f"Loaded .env file from {env_path}")
else:
logger.info(f".env file not found at {env_path}; relying on system environment variables")
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)}")
# Discord tokens are base64-like, ~59-100 chars
if not re.match(r'^[A-Za-z0-9._-]{50,100}$', self.discord_token):
logger.error(
"Invalid Discord token format (length: %d)", len(self.discord_token)
)
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(
"Successfully loaded discord token (length: %d)", len(self.discord_token)
)
# 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)}")
if not re.match(r'^[A-Za-z0-9]{16}$', self.pollinations_token): # Assuming 16-char alphanumeric format from example
logger.error(
"Invalid Pollinations token format (length: %d)",
len(self.pollinations_token),
)
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(
"Successfully loaded pollinations token (length: %d)",
len(self.pollinations_token),
)
# Bot configuration
self.default_model = "unity"
try:
with open("system_instructions.txt", "r") as f:
self.system_instructions = f.read().strip()
except FileNotFoundError:
logger.error("system_instructions.txt not found")
raise FileNotFoundError("system_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"
self.max_history = 20
self.max_memories = 5
allowed_channels_env = os.getenv("ALLOWED_CHANNELS", "")
self.allowed_channels = {
ch.strip() for ch in allowed_channels_env.split(",") if ch.strip()
}
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()