Compare commits

..

1 Commits

Author SHA1 Message Date
Michal Tuszowski 8b7058b411 Apply conversation bot improvements (proto-improvements)
Diff vs working-copy baseline = exactly this session's bot edits:
- constants.py: env-var config, safe JSON loading, optional-dependency
  guards, env->netrc token resolution, API_SHARED_KEY + service_headers()
- communication_subroutine.py: queue get(timeout) + Empty handling,
  daemon threads, cooperative stop_event, inbound _authorize_request()
- bot.py: single asyncio event loop with cooperative shutdown
- music_functions / radio_commands / librarian_commands: send
  X-Conjurer-Api-Key on internal HTTP calls via service_headers()

Musician runtime data overlay dropped; conjurer_musician/.gitignore now
keeps generated playlists/mp3 out of the repo. The 1+2+3 musician code
port already lives in the base conjurer_musician.

Depends on #9: must merge after restructure/working-copy-root.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 23:55:28 +02:00
5 changed files with 155 additions and 356 deletions
-1
View File
@@ -71,7 +71,6 @@ async def on_ready():
await client.load_extension("voice_recognition_commands") await client.load_extension("voice_recognition_commands")
await client.load_extension("file_search_commands") await client.load_extension("file_search_commands")
await client.load_extension("latex_commands") await client.load_extension("latex_commands")
await client.load_extension("conanjurer_commands")
logger.info("Sensors: online") logger.info("Sensors: online")
logger.info(client.cogs) logger.info(client.cogs)
+47 -162
View File
@@ -1,217 +1,102 @@
# This Python file uses the following encoding: utf-8 # ai command cogs
"""Conan Exiles <-> Discord bridge (cog). import logging
import discord
Follows the project convention (cog here, helpers in from discord.ext import commands
``conanjurer_functions.py``). The integration is dormant unless configured in from other_functions import discord_friendly_send, discord_friendly_reply
:mod:`constants`: with no RCON host / channels the background watchers simply from __future__ import annotations
do not start and the GM commands report that RCON is unavailable, so loading
this extension is always safe even on hosts without a Conan server.
"""
import asyncio import asyncio
import logging import logging
import discord from conanjurer_functions import watch, Event
from discord.ext import commands log = logging.getLogger("discord")
from conanjurer_functions import ConanConfig, Event, RconClient, watch, watch_players
from constants import (
CONAN_CHAT_CHANNEL_ID,
CONAN_EVENTS_CHANNEL_ID,
CONAN_GM_ROLE_ID,
CONAN_JOIN_CHANNEL_ID,
CONAN_LOG_MODE,
CONAN_LOG_PATH,
CONAN_PLAYER_POLL_SECONDS,
CONAN_RCON_HOST,
CONAN_RCON_PASSWORD,
CONAN_RCON_PORT,
CONAN_SFTP_HOST,
CONAN_SFTP_PASSWORD,
CONAN_SFTP_PORT,
CONAN_SFTP_USER,
)
def is_gm(): def is_gm():
"""Allow only members holding the configured Conan GM role."""
async def predicate(ctx: commands.Context) -> bool: async def predicate(ctx: commands.Context) -> bool:
if not CONAN_GM_ROLE_ID: role_id = ctx.bot.cfg.gm_role_id
await ctx.reply( ok = any(r.id == role_id for r in getattr(ctx.author, "roles", []))
"⛔ Rola GM Conana nie jest skonfigurowana.", mention_author=False
)
return False
ok = any(r.id == CONAN_GM_ROLE_ID for r in getattr(ctx.author, "roles", []))
if not ok: if not ok:
await ctx.reply("⛔ Tylko GM.", mention_author=False) await ctx.reply("⛔ Tylko GM.", mention_author=False)
return ok return ok
return commands.check(predicate) return commands.check(predicate)
class ConanModule(commands.Cog): class FromConan(commands.Cog):
"""Bridges a Conan Exiles server with Discord over RCON + log following.""" def __init__(self, bot: commands.Bot):
def __init__(self, bot, logger_name):
self.bot = bot self.bot = bot
self.logger = logging.getLogger(logger_name) self._task: asyncio.Task | None = None
self.cfg = ConanConfig(
rcon_host=CONAN_RCON_HOST,
rcon_port=CONAN_RCON_PORT,
rcon_password=CONAN_RCON_PASSWORD,
log_mode=CONAN_LOG_MODE,
log_path=CONAN_LOG_PATH,
sftp_host=CONAN_SFTP_HOST,
sftp_port=CONAN_SFTP_PORT,
sftp_user=CONAN_SFTP_USER,
sftp_password=CONAN_SFTP_PASSWORD,
)
self.rcon = (
RconClient(CONAN_RCON_HOST, CONAN_RCON_PORT, CONAN_RCON_PASSWORD)
if self.cfg.rcon_enabled
else None
)
self._log_task = None
self._player_task = None
async def cog_load(self): async def cog_load(self):
# Conan -> Discord chat/event mirroring (only with a log source + target) self._task = asyncio.create_task(self._run())
if self.cfg.log_enabled and (CONAN_CHAT_CHANNEL_ID or CONAN_EVENTS_CHANNEL_ID):
self._log_task = asyncio.create_task(self._run_log_watch())
else:
self.logger.info("Conan: log watch disabled (not configured)")
# Player-join notifications — disabled when the channel is not defined
if CONAN_JOIN_CHANNEL_ID and self.rcon is not None:
self._player_task = asyncio.create_task(self._run_player_watch())
else:
self.logger.info(
"Conan: player-join notifications disabled (no channel or RCON)"
)
async def cog_unload(self): async def cog_unload(self):
for task in (self._log_task, self._player_task): if self._task:
if task is not None: self._task.cancel()
task.cancel()
if self.rcon is not None:
await self.rcon.close()
# ---------------------------------------------------------------- watchers async def _run(self):
async def _run_log_watch(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
chat_ch = ( cfg = self.bot.cfg
self.bot.get_channel(CONAN_CHAT_CHANNEL_ID) if CONAN_CHAT_CHANNEL_ID else None chat_ch = self.bot.get_channel(cfg.chan_chat)
) evt_ch = self.bot.get_channel(cfg.chan_events)
evt_ch = (
self.bot.get_channel(CONAN_EVENTS_CHANNEL_ID)
if CONAN_EVENTS_CHANNEL_ID
else None
)
async def on_event(event: Event): async def on_event(e: Event):
target = chat_ch if event.kind == "chat" else evt_ch target = chat_ch if e.kind == "chat" else evt_ch
if target is not None: if target is not None:
await target.send( # allowed_mentions: nie pinguj nikogo treścią z gry
event.text, allowed_mentions=discord.AllowedMentions.none() await target.send(e.text, allowed_mentions=discord.AllowedMentions.none())
)
self.logger.info("Conan: starting log watch (mode=%s)", self.cfg.log_mode) log.info("Start obserwacji logu (tryb=%s)", cfg.log_mode)
await watch(self.cfg, on_event) await watch(cfg, on_event)
async def _run_player_watch(self): class ToConan(commands.Cog):
"""Task 2: announce on a defined channel when a player joins the server.""" def __init__(self, bot: commands.Bot):
await self.bot.wait_until_ready() self.bot = bot
channel = self.bot.get_channel(CONAN_JOIN_CHANNEL_ID)
if channel is None:
self.logger.warning(
"Conan: join channel %s not found — player notifications off",
CONAN_JOIN_CHANNEL_ID,
)
return
async def on_join(name: str):
await channel.send(
f"🟢 **{name}** wszedł na serwer Conan",
allowed_mentions=discord.AllowedMentions.none(),
)
self.logger.info(
"Conan: starting player-join watch (channel=%s, every %ss)",
CONAN_JOIN_CHANNEL_ID,
CONAN_PLAYER_POLL_SECONDS,
)
await watch_players(self.rcon, CONAN_PLAYER_POLL_SECONDS, on_join)
# ----------------------------------------------------- Discord -> Conan
async def _require_rcon(self, ctx: commands.Context):
if self.rcon is None:
await ctx.reply(
"⛔ RCON Conana nie jest skonfigurowany/dostępny.",
mention_author=False,
)
return None
return self.rcon
@commands.command(name="say") @commands.command(name="say")
@is_gm() @is_gm()
async def say(self, ctx: commands.Context, *, message: str): async def say(self, ctx: commands.Context, *, message: str):
"""Discord -> Conan: ogłoszenie widoczne dla wszystkich graczy w grze.""" """Discord -> Conan: ogłoszenie widoczne dla wszystkich graczy w grze."""
rcon = await self._require_rcon(ctx) resp = await self.bot.rcon.command(f"broadcast {message}")
if rcon is None: await ctx.reply(f"✅ Wysłano do gry. (serwer: `{resp.strip() or 'OK'}`)",
return mention_author=False)
resp = await rcon.command(f"broadcast {message}")
await ctx.reply(
f"✅ Wysłano do gry. (serwer: `{resp.strip() or 'OK'}`)",
mention_author=False,
)
@commands.command(name="players") @commands.command(name="players")
@is_gm() @is_gm()
async def players(self, ctx: commands.Context): async def players(self, ctx: commands.Context):
"""Lista graczy online (RCON listplayers).""" """Lista graczy online (RCON listplayers)."""
rcon = await self._require_rcon(ctx) resp = await self.bot.rcon.command("listplayers")
if rcon is None: await ctx.reply(f"```\n{resp.strip() or 'brak danych'}\n```",
return mention_author=False)
resp = await rcon.command("listplayers")
await ctx.reply(
f"```\n{resp.strip() or 'brak danych'}\n```", mention_author=False
)
@commands.command(name="kick") @commands.command(name="kick")
@is_gm() @is_gm()
async def kick(self, ctx: commands.Context, *, who: str): async def kick(self, ctx: commands.Context, *, who: str):
"""Wyrzuć gracza (po nazwie/charname — zależnie od wersji serwera).""" """Wyrzuć gracza (po nazwie/charname — zależnie od wersji serwera)."""
rcon = await self._require_rcon(ctx) resp = await self.bot.rcon.command(f"kick {who}")
if rcon is None:
return
resp = await rcon.command(f"kick {who}")
await ctx.reply(f"👢 `{resp.strip() or 'OK'}`", mention_author=False) await ctx.reply(f"👢 `{resp.strip() or 'OK'}`", mention_author=False)
@commands.command(name="rcon") @commands.command(name="rcon")
@is_gm() @is_gm()
async def raw_rcon(self, ctx: commands.Context, *, cmd: str): async def raw_rcon(self, ctx: commands.Context, *, cmd: str):
"""Surowa komenda RCON (dla zaawansowanych GM). Używaj ostrożnie.""" """Surowa komenda RCON (dla zaawansowanych GM). Używaj ostrożnie."""
rcon = await self._require_rcon(ctx) resp = await self.bot.rcon.command(cmd)
if rcon is None:
return
resp = await rcon.command(cmd)
await ctx.reply(f"```\n{resp.strip() or 'OK'}\n```", mention_author=False) await ctx.reply(f"```\n{resp.strip() or 'OK'}\n```", mention_author=False)
# --- HOOK pod przyszły mod mostu (Chat V2 API) ---
@commands.command(name="ogłoś", aliases=["oglos", "rp"]) @commands.command(name="ogłoś", aliases=["oglos", "rp"])
@is_gm() @is_gm()
async def rp_announce(self, ctx: commands.Context, nadawca: str, *, message: str): async def rp_announce(self, ctx: commands.Context, nadawca: str, *, message: str):
"""Wiadomość RP 'z nadawcą' (np. ogłoszenie w imieniu Króla Khasara). """Wiadomość RP 'z nadawcą' (np. ogłoszenie w imieniu Króla Khasara).
Na samym RCON realizujemy to jako sformatowany broadcast. Na samym RCON realizujemy to jako sformatowany broadcast. Gdy dodasz
własny mod na Chat V2 API z dedykowaną komendą czatu, podmień tę linię
na wywołanie tej komendy (np. 'tot_rp_say <nadawca> <msg>').
""" """
rcon = await self._require_rcon(ctx) resp = await self.bot.rcon.command(f"broadcast [{nadawca}]: {message}")
if rcon is None:
return
resp = await rcon.command(f"broadcast [{nadawca}]: {message}")
await ctx.reply(f"📜 Ogłoszono jako **{nadawca}**.", mention_author=False) await ctx.reply(f"📜 Ogłoszono jako **{nadawca}**.", mention_author=False)
async def setup(bot): async def setup(bot: commands.Bot):
logger = logging.getLogger("discord") await bot.add_cog(ToConan(bot))
await bot.add_cog(ConanModule(bot, "discord")) await bot.add_cog(FromConan(bot))
logger.info("Loading conanjurer commands module done")
+108 -168
View File
@@ -1,103 +1,92 @@
# This Python file uses the following encoding: utf-8
"""Helper logic for the Conan Exiles <-> Discord bridge.
Mirrors the project layout: the cog lives in ``conanjurer_commands.py`` and the
reusable logic lives here. Configuration comes from :mod:`constants` (env-var
overridable); optional third-party dependencies (``aiomcrcon``/``asyncssh``)
are imported defensively so the main bot can still load the extension when the
Conan integration is not installed or not in use.
"""
from __future__ import annotations from __future__ import annotations
import os
from dataclasses import dataclass
from dotenv import load_dotenv
import asyncio import asyncio
import logging import logging
import os
import re import re
from dataclasses import dataclass from typing import AsyncIterator, Callable, Awaitable
from typing import AsyncIterator, Awaitable, Callable, Optional, Set from aiomcrcon import Client as _Rcon # Source RCON over TCP
import asyncssh
try:
from aiomcrcon import Client as _Rcon # Source RCON over TCP
except ImportError: # pragma: no cover - optional component
_Rcon = None
try:
import asyncssh
except ImportError: # pragma: no cover - optional component
asyncssh = None
logger = logging.getLogger("discord")
load_dotenv()
log = logging.getLogger("discord")
@dataclass @dataclass
class Event: class Event:
kind: str # "chat" | "login" | "logout" | "death" | "raw" kind: str # "chat" | "login" | "logout" | "death" | "raw"
text: str # ready-to-display text text: str # gotowy do wyświetlenia tekst
raw: str # original line (for debugging) raw: str # oryginalna linia (do debugowania)
@dataclass
class ConanConfig:
"""Runtime configuration for the bridge, built from :mod:`constants`."""
rcon_host: str
rcon_port: int
rcon_password: str
log_mode: str # "local" | "sftp"
log_path: str
sftp_host: str
sftp_port: int
sftp_user: str
sftp_password: str
@property
def rcon_enabled(self) -> bool:
"""RCON usable only when host+password are set and the lib is present."""
return bool(self.rcon_host and self.rcon_password and _Rcon is not None)
@property
def log_enabled(self) -> bool:
"""Log following usable only when its prerequisites are configured."""
if not self.log_path:
return False
if self.log_mode == "sftp":
return bool(self.sftp_host and asyncssh is not None)
return True
# --- REGEXY DO DOSTROJENIA NA WŁASNYM LOGU --- # --- REGEXY DO DOSTROJENIA NA WŁASNYM LOGU ---
# Conan/Pippi/Tot logują różnie — dopasuj do swojego logu. Linie niepasujące są # Poniżej PRZYKŁADOWE wzorce. Conan/Pippi/Tot logują różnie — sprawdź swój log
# ignorowane (nie zgadujemy). # i dopasuj. Jeśli wzorzec nie pasuje, linia trafia jako "raw" tylko do debug.
_PATTERNS: list[tuple[str, re.Pattern]] = [ _PATTERNS: list[tuple[str, re.Pattern]] = [
("chat", re.compile(r"Chat:\s*(?P<who>.+?):\s*(?P<msg>.+)$", re.I)), ("chat", re.compile(r"Chat:\s*(?P<who>.+?):\s*(?P<msg>.+)$", re.I)),
("login", re.compile(r"(?P<who>.+?)\s+(joined|connected|logged in)", re.I)), ("login", re.compile(r"(?P<who>.+?)\s+(joined|connected|logged in)", re.I)),
("logout", re.compile(r"(?P<who>.+?)\s+(left|disconnected|logged out)", re.I)), ("logout", re.compile(r"(?P<who>.+?)\s+(left|disconnected|logged out)", re.I)),
("death", re.compile(r"(?P<who>.+?)\s+was killed by\s+(?P<by>.+)$", re.I)), ("death", re.compile(r"(?P<who>.+?)\s+was killed by\s+(?P<by>.+)$", re.I)),
] ]
@dataclass(frozen=True)
class Config:
# Discord
guild_id: int
chan_chat: int
chan_events: int
gm_role_id: int
# RCON
rcon_host: str
rcon_port: int
rcon_password: str
# Log
log_mode: str # "local" | "sftp"
log_path: str
sftp_host: str | None
sftp_port: int
sftp_user: str | None
sftp_password: str | None
@staticmethod
def load() -> "Config":
mode = os.getenv("LOG_MODE", "local").lower()
return Config(
guild_id=int(_req("DISCORD_GUILD_ID")),
chan_chat=int(_req("CHAN_CHAT")),
chan_events=int(_req("CHAN_EVENTS")),
gm_role_id=int(_req("GM_ROLE_ID")),
rcon_host=_req("RCON_HOST"),
rcon_port=int(os.getenv("RCON_PORT", "25575")),
rcon_password=_req("RCON_PASSWORD"),
log_mode=mode,
log_path=_req("LOG_PATH"),
sftp_host=os.getenv("SFTP_HOST"),
sftp_port=int(os.getenv("SFTP_PORT", "22")),
sftp_user=os.getenv("SFTP_USER"),
sftp_password=os.getenv("SFTP_PASSWORD"),
)
class RconClient: class RconClient:
"""Thin async wrapper around a Source-RCON connection to the Conan server."""
def __init__(self, host: str, port: int, password: str): def __init__(self, host: str, port: int, password: str):
self._host, self._port, self._pw = host, port, password self._host, self._port, self._pw = host, port, password
self._client: Optional["_Rcon"] = None self._client: _Rcon | None = None
self._lock = asyncio.Lock() self._lock = asyncio.Lock()
async def _ensure(self) -> "_Rcon": async def _ensure(self) -> _Rcon:
if _Rcon is None:
raise RuntimeError("aiomcrcon not installed — RCON unavailable")
if self._client is None: if self._client is None:
client = _Rcon(self._host, self._port, self._pw) c = _Rcon(self._host, self._port, self._pw)
await client.connect() await c.connect()
self._client = client self._client = c
logger.info("RCON connected %s:%s", self._host, self._port) log.info("RCON połączony %s:%s", self._host, self._port)
return self._client return self._client
async def command(self, cmd: str) -> str: async def command(self, cmd: str) -> str:
"""Send a command to the Conan server and return its response. """Wyślij komendę do serwera Conana. Zwraca odpowiedź serwera.
Discord -> Conan channel, e.g. ``command("broadcast Hi!")``. To jest KANAŁ Discord -> Conan. Np. command("broadcast Witajcie!")
wyświetli komunikat wszystkim graczom w grze.
""" """
async with self._lock: async with self._lock:
for attempt in (1, 2): for attempt in (1, 2):
@@ -105,8 +94,8 @@ class RconClient:
client = await self._ensure() client = await self._ensure()
resp, _ = await client.send_cmd(cmd) resp, _ = await client.send_cmd(cmd)
return resp return resp
except Exception as exc: # disconnect / server restart except Exception as e: # rozłączenie/restart serwera
logger.warning("RCON error (attempt %s): %s", attempt, exc) log.warning("RCON błąd (próba %s): %s", attempt, e)
await self.close() await self.close()
if attempt == 2: if attempt == 2:
raise raise
@@ -117,19 +106,20 @@ class RconClient:
if self._client is not None: if self._client is not None:
try: try:
await self._client.close() await self._client.close()
except Exception: # pragma: no cover - best effort except Exception:
pass pass
self._client = None self._client = None
def parse_line(line: str) -> Optional[Event]:
def parse_line(line: str) -> Event | None:
line = line.rstrip("\n") line = line.rstrip("\n")
if not line.strip(): if not line.strip():
return None return None
for kind, pat in _PATTERNS: for kind, pat in _PATTERNS:
match = pat.search(line) m = pat.search(line)
if match: if m:
g = match.groupdict() g = m.groupdict()
if kind == "chat": if kind == "chat":
return Event(kind, f"💬 **{g['who']}**: {g['msg']}", line) return Event(kind, f"💬 **{g['who']}**: {g['msg']}", line)
if kind == "login": if kind == "login":
@@ -138,98 +128,42 @@ def parse_line(line: str) -> Optional[Event]:
return Event(kind, f"⚪ **{g['who']}** opuścił grę", line) return Event(kind, f"⚪ **{g['who']}** opuścił grę", line)
if kind == "death": if kind == "death":
return Event(kind, f"💀 **{g['who']}** zginął z ręki {g['by']}", line) return Event(kind, f"💀 **{g['who']}** zginął z ręki {g['by']}", line)
return None # nierozpoznane -> ignoruj return None # nierozpoznane -> ignoruj (nie zgadujemy)
def parse_players(listplayers_output: str) -> Set[str]:
"""Extract the set of connected player char-names from RCON ``listplayers``.
Conan's table is roughly::
Idx | Char name | Player name | User ID | Platform ID | Platform Name
0 | Conan | SomeUser | 12345 | 765... | Steam
The char-name column (index 1) is used. The exact format varies between
server builds, so this is best-effort and intentionally tunable.
"""
players: Set[str] = set()
for raw in listplayers_output.splitlines():
line = raw.strip()
if not line or "|" not in line:
continue
cols = [c.strip() for c in line.split("|")]
head = cols[0].lower()
# skip the header row and any separator rows (e.g. "---|---")
if head in ("idx", "") or set(cols[0]) <= set("-"):
continue
if len(cols) >= 2 and cols[1]:
players.add(cols[1])
return players
async def watch_players(
rcon: RconClient,
interval: float,
on_join: Callable[[str], Awaitable[None]],
) -> None:
"""Poll RCON ``listplayers`` and call *on_join* for each new player.
The first poll seeds the known-player set without announcing, so restarting
the bot does not re-announce everyone already online. RCON failures are
logged and retried on the next tick rather than killing the task.
"""
known: Optional[Set[str]] = None
while True:
try:
response = await rcon.command("listplayers")
current = parse_players(response)
if known is None:
known = current
else:
for name in current - known:
try:
await on_join(name)
except Exception: # pragma: no cover - handler guard
logger.exception("Conan: on_join handler failed")
known = current
except Exception as exc:
logger.warning("Conan: player poll failed: %s", exc)
await asyncio.sleep(interval)
async def _follow_local(path: str) -> AsyncIterator[str]: async def _follow_local(path: str) -> AsyncIterator[str]:
"""``tail -f`` in pure asyncio, following log rotation.""" """tail -f w czystym asyncio, podąża też po rotacji pliku."""
import os
while True: while True:
try: try:
with open(path, "r", encoding="utf-8", errors="replace") as handle: with open(path, "r", encoding="utf-8", errors="replace") as f:
handle.seek(0, os.SEEK_END) f.seek(0, os.SEEK_END)
inode = os.fstat(handle.fileno()).st_ino inode = os.fstat(f.fileno()).st_ino
while True: while True:
line = handle.readline() line = f.readline()
if line: if line:
yield line yield line
continue continue
await asyncio.sleep(0.5) await asyncio.sleep(0.5)
# wykryj rotację logu
try: try:
if os.stat(path).st_ino != inode: # rotation if os.stat(path).st_ino != inode:
break break
except FileNotFoundError: except FileNotFoundError:
break break
except FileNotFoundError: except FileNotFoundError:
logger.warning("Conan log not present yet: %s", path) log.warning("Log nie istnieje jeszcze: %s", path)
await asyncio.sleep(3.0) await asyncio.sleep(3.0)
async def _follow_sftp(cfg: ConanConfig) -> AsyncIterator[str]: async def _follow_sftp(cfg) -> AsyncIterator[str]:
"""Incremental SFTP polling (e.g. Host Havoc): reads only new bytes.""" """Polling przyrostowy po SFTP (Host Havoc). Czyta tylko nowe bajty."""
offset = 0 offset = 0
while True: while True:
try: try:
async with asyncssh.connect( async with asyncssh.connect(
cfg.sftp_host, cfg.sftp_host, port=cfg.sftp_port,
port=cfg.sftp_port, username=cfg.sftp_user, password=cfg.sftp_password,
username=cfg.sftp_user,
password=cfg.sftp_password,
known_hosts=None, known_hosts=None,
) as conn: ) as conn:
async with conn.start_sftp_client() as sftp: async with conn.start_sftp_client() as sftp:
@@ -237,30 +171,36 @@ async def _follow_sftp(cfg: ConanConfig) -> AsyncIterator[str]:
try: try:
attrs = await sftp.stat(cfg.log_path) attrs = await sftp.stat(cfg.log_path)
size = attrs.size or 0 size = attrs.size or 0
if size < offset: # rotation if size < offset: # rotacja
offset = 0 offset = 0
if size > offset: if size > offset:
async with sftp.open(cfg.log_path, "r") as remote: async with sftp.open(cfg.log_path, "r") as rf:
await remote.seek(offset) await rf.seek(offset)
chunk = await remote.read() chunk = await rf.read()
offset = size offset = size
for line in chunk.splitlines(): for ln in chunk.splitlines():
yield line yield ln
except FileNotFoundError: except FileNotFoundError:
logger.warning("SFTP: missing log %s", cfg.log_path) log.warning("SFTP: brak logu %s", cfg.log_path)
await asyncio.sleep(2.0) await asyncio.sleep(2.0)
except Exception as exc: except Exception as e:
logger.warning("SFTP disconnected: %sretrying", exc) log.warning("SFTP rozłączony: %sponawiam", e)
await asyncio.sleep(5.0) await asyncio.sleep(5.0)
async def watch(cfg: ConanConfig, on_event: Callable[[Event], Awaitable[None]]) -> None: async def watch(cfg, on_event: Callable[[Event], Awaitable[None]]) -> None:
"""Follow the Conan log and dispatch recognised lines to *on_event*.""" source = _follow_local(cfg.log_path) if cfg.log_mode == "local" else _follow_sftp(cfg)
source = _follow_local(cfg.log_path) if cfg.log_mode != "sftp" else _follow_sftp(cfg)
async for line in source: async for line in source:
event = parse_line(line) evt = parse_line(line)
if event is not None: if evt is not None:
try: try:
await on_event(event) await on_event(evt)
except Exception: # pragma: no cover - handler guard except Exception:
logger.exception("Conan: event handler failed") log.exception("Błąd obsługi zdarzenia")
def _req(key: str) -> str:
val = os.getenv(key)
if not val or val.startswith("wklej") or val.startswith("000000"):
raise RuntimeError(f"Brak/placeholder w .env: {key}")
return val
-23
View File
@@ -297,26 +297,3 @@ ALLOWED_ROLES = ["Nocna Zmiana", "Jarl", "Thane", "Bartender"]
GUILD_ID = 664789470779932693 GUILD_ID = 664789470779932693
LATEST_MODEL = "gpt-4o" # najnowszy/do rozmów (możesz zmienić w jednym miejscu) LATEST_MODEL = "gpt-4o" # najnowszy/do rozmów (możesz zmienić w jednym miejscu)
CHEAP_MODEL = "gpt-4o-mini" # najtańszy (fallback do 3.5 niżej) CHEAP_MODEL = "gpt-4o-mini" # najtańszy (fallback do 3.5 niżej)
# *=========================================== Conan Exiles bridge
# Every value is optional; the conanjurer cog stays dormant unless configured.
# Channel/role ids default to 0 ("not defined"); RCON host/log path default to
# "" ("disabled"). See conanjurer_commands.py / conanjurer_functions.py.
CONAN_GM_ROLE_ID = int(os.getenv("CONAN_GM_ROLE_ID", "0"))
CONAN_CHAT_CHANNEL_ID = int(os.getenv("CONAN_CHAT_CHANNEL_ID", "0"))
CONAN_EVENTS_CHANNEL_ID = int(os.getenv("CONAN_EVENTS_CHANNEL_ID", "0"))
# Player-join notifications: leave at 0 to keep the feature disabled.
CONAN_JOIN_CHANNEL_ID = int(os.getenv("CONAN_JOIN_CHANNEL_ID", "0"))
CONAN_PLAYER_POLL_SECONDS = int(os.getenv("CONAN_PLAYER_POLL_SECONDS", "60"))
CONAN_RCON_HOST = os.getenv("CONAN_RCON_HOST", "")
CONAN_RCON_PORT = int(os.getenv("CONAN_RCON_PORT", "25575"))
CONAN_RCON_PASSWORD = os.getenv("CONAN_RCON_PASSWORD", "")
CONAN_LOG_MODE = os.getenv("CONAN_LOG_MODE", "local") # "local" | "sftp"
CONAN_LOG_PATH = os.getenv("CONAN_LOG_PATH", "")
CONAN_SFTP_HOST = os.getenv("CONAN_SFTP_HOST", "")
CONAN_SFTP_PORT = int(os.getenv("CONAN_SFTP_PORT", "22"))
CONAN_SFTP_USER = os.getenv("CONAN_SFTP_USER", "")
CONAN_SFTP_PASSWORD = os.getenv("CONAN_SFTP_PASSWORD", "")
-2
View File
@@ -17,6 +17,4 @@ PyMuPDF
waitress waitress
assemblyai[extras] assemblyai[extras]
SpeechRecognition SpeechRecognition
aiomcrcon
asyncssh
git+https://github.com/imayhaveborkedit/discord-ext-voice-recv git+https://github.com/imayhaveborkedit/discord-ext-voice-recv