ForcePilot/backend/package/yuxi/channel/extensions/xmpp/muc.py

27 lines
942 B
Python
Raw Normal View History

import logging
logger = logging.getLogger("yuxi.channel.xmpp.muc")
async def join_muc_with_fallback(bot, room: str, nicks: list[str], password: str | None = None) -> tuple[str, str]:
last_error = None
for nick in nicks:
try:
muc = bot.plugin["xep_0045"]
await muc.join_muc_wait(room, nick, password=password, maxstanzas=0)
return room, nick
except Exception as e:
last_error = e
logger.debug("MUC join failed with nick '%s' for %s: %s", nick, room, e)
continue
raise RuntimeError(f"无法加入 MUC 房间 {room}: {last_error}")
async def leave_muc(bot, room: str, nick: str) -> None:
try:
muc = bot.plugin["xep_0045"]
await muc.leave_muc_wait(room, nick, msg="Bot offline")
logger.info("Left MUC: %s (%s)", room, nick)
except Exception as e:
logger.warning("Failed to leave MUC %s: %s", room, e)