Files
xtrm-agent/xtrm_agent/channels/discord.py
Kaloyan Danchev b3608b35fa Connect to Discord, switch to NVIDIA NIM providers
- Enable Discord channel, respond to all messages from allowed users
- Switch all agents to NVIDIA NIM (Kimi K2.5, DeepSeek V3.1)
- Auto-approve all tools for non-interactive Docker deployment
- Fix tool call arguments serialization (dict → JSON string)
- Fix Dockerfile to copy source before uv sync

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-18 15:34:15 +02:00

93 lines
3.1 KiB
Python

"""Discord channel — bot integration via discord.py."""
from __future__ import annotations
import asyncio
import os
import discord
from loguru import logger
from xtrm_agent.bus import InboundMessage, MessageBus, OutboundMessage
from xtrm_agent.channels.base import BaseChannel
class DiscordChannel(BaseChannel):
"""Discord bot channel."""
def __init__(
self,
bus: MessageBus,
token_env: str = "DISCORD_BOT_TOKEN",
default_agent: str = "coder",
allowed_users: list[str] | None = None,
) -> None:
super().__init__(bus)
self.token_env = token_env
self.default_agent = default_agent
self.allowed_users = set(allowed_users or [])
self._outbound_queue = bus.subscribe_outbound("discord")
intents = discord.Intents.default()
intents.message_content = True
self.client = discord.Client(intents=intents)
self._setup_events()
def _setup_events(self) -> None:
@self.client.event
async def on_ready() -> None:
logger.info(f"Discord bot connected as {self.client.user}")
@self.client.event
async def on_message(message: discord.Message) -> None:
if message.author == self.client.user:
return
if message.author.bot:
return
# Check allowlist — if set, only respond to listed users
if self.allowed_users and str(message.author.id) not in self.allowed_users:
return
content = message.content
# Strip bot mention from content
if self.client.user:
content = content.replace(f"<@{self.client.user.id}>", "").strip()
msg = InboundMessage(
channel="discord",
sender_id=str(message.author.id),
chat_id=str(message.channel.id),
content=content,
metadata={"guild_id": str(message.guild.id) if message.guild else ""},
)
await self.bus.publish_inbound(msg)
# Wait for response and send it
try:
async with message.channel.typing():
out = await asyncio.wait_for(self._outbound_queue.get(), timeout=300)
await self._send_chunked(message.channel, out.content)
except asyncio.TimeoutError:
await message.channel.send("Sorry, I timed out processing your request.")
async def _send_chunked(
self, channel: discord.abc.Messageable, content: str
) -> None:
"""Send a message, splitting into 2000-char chunks if needed."""
while content:
chunk = content[:2000]
content = content[2000:]
await channel.send(chunk)
async def start(self) -> None:
token = os.environ.get(self.token_env)
if not token:
logger.error(f"Discord token not found in env var '{self.token_env}'")
return
logger.info("Starting Discord bot...")
await self.client.start(token)
async def stop(self) -> None:
await self.client.close()