41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
|
|
# context.py
|
||
|
|
|
||
|
|
import os
|
||
|
|
import yaml
|
||
|
|
import discord
|
||
|
|
|
||
|
|
base_dir = os.path.dirname(__file__)
|
||
|
|
with open(os.path.join(base_dir, "settings.yml"), "r", encoding="utf-8") as f:
|
||
|
|
settings = yaml.safe_load(f)
|
||
|
|
|
||
|
|
CONTEXT_LIMIT = settings["context"].get("max_messages", 15)
|
||
|
|
|
||
|
|
async def fetch_recent_context(channel, limit=CONTEXT_LIMIT):
|
||
|
|
messages = []
|
||
|
|
async for message in channel.history(limit=100):
|
||
|
|
# Skip other bots (but not Delta herself)
|
||
|
|
if message.author.bot and message.author.id != channel.guild.me.id:
|
||
|
|
continue
|
||
|
|
|
||
|
|
raw = message.clean_content
|
||
|
|
clean = raw.strip().replace("\n", " ").replace("\r", "")
|
||
|
|
clean = " ".join(clean.split()) # Collapse all extra whitespace
|
||
|
|
|
||
|
|
if not clean:
|
||
|
|
continue
|
||
|
|
|
||
|
|
if clean.startswith("!"):
|
||
|
|
continue
|
||
|
|
|
||
|
|
line = f"{message.created_at.strftime('%Y-%m-%d %H:%M')} - {message.author.display_name}: {clean}"
|
||
|
|
messages.append(line)
|
||
|
|
|
||
|
|
if len(messages) >= limit:
|
||
|
|
break
|
||
|
|
|
||
|
|
messages.reverse()
|
||
|
|
return messages
|
||
|
|
|
||
|
|
def format_context(lines: list[str]) -> str:
|
||
|
|
return "\n".join(lines)
|