refactor(channels): make built-in channels self-contained (#4908)
* refactor(channels): own setup and instance contracts * refactor(channels): isolate management contracts * refactor(channels): normalize activation contracts * fix(channels): enforce management contracts * refactor(channels): finish setup ownership migration * fix(channels): harden management contracts * fix(channels): enforce lazy loading and runtime ownership * fix(feishu): make multi-instance startup idempotent * fix(webui): render channel setup contracts cleanly * fix(feishu): stop websocket clients cleanly * fix(channels): enforce persistence and activation gates * fix(channels): preserve global feature action scope * fix(channels): apply defaults for single plugins * fix(channels): enforce management contract boundaries * refactor(feishu): remove identity helper indirection * fix(channels): preserve management setup contracts * refactor(channels): generalize instance settings UI * refactor(channels): package channel plugins with web UI metadata * refactor(channels): make built-ins self-contained packages * test(channels): colocate tests with channel packages * fix(dingtalk): use official brand icon * feat(channels): colocate webui translations * docs(channels): clarify plugin ownership * test(exec): remove output wait race * refactor(channels): unify plugin descriptors * fix(channels): enforce descriptor-owned contracts * refactor(channels): finish package-owned plugin setup * refactor(channels): use repository-owned packages only * fix(channels): self-describe dependencies and runtime state * fix(channels): warn about legacy entry points
This commit is contained in:
1 parent
7aaac37bca
commit
462a0dfb0f
388 files changed
+17093
-5110
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Discord channel package."""
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Discord management contract."""
|
||||
|
||||
from nanobot.channels._manifest import DIRECT_GROUP_POLICIES, field, required
|
||||
from nanobot.channels.contracts import ChannelSetupSpec
|
||||
from nanobot.channels.discord.validation import validate
|
||||
from nanobot.channels.plugin import ChannelPlugin
|
||||
|
||||
SETUP_SPEC = ChannelSetupSpec(
|
||||
fields={
|
||||
"token": field("secret"),
|
||||
"allowFrom": field("list", snapshot=False),
|
||||
"allowChannels": field("list"),
|
||||
"groupPolicy": field("enum", choices=DIRECT_GROUP_POLICIES, default="mention"),
|
||||
},
|
||||
required=(required("token"),),
|
||||
official_url="https://discord.com/developers/applications",
|
||||
validator=validate,
|
||||
)
|
||||
|
||||
PLUGIN = ChannelPlugin(
|
||||
name="discord",
|
||||
display_name="Discord",
|
||||
runtime=f"{__package__}.runtime:DiscordChannel",
|
||||
setup=SETUP_SPEC,
|
||||
dependencies=("discord.py>=2.5.2,<3.0.0",),
|
||||
webui="webui/index.ts",
|
||||
)
|
||||
@@ -0,0 +1,834 @@
|
||||
"""Discord channel implementation using discord.py."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import importlib.util
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, Literal
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
from nanobot.bus.outbound_events import ProgressEvent
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.channels.base import BaseChannel
|
||||
from nanobot.command.builtin import build_help_text
|
||||
from nanobot.config.paths import get_media_dir
|
||||
from nanobot.config.schema import Base
|
||||
from nanobot.utils.helpers import safe_filename, split_message
|
||||
|
||||
DISCORD_AVAILABLE = importlib.util.find_spec("discord") is not None
|
||||
if TYPE_CHECKING:
|
||||
import aiohttp
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.abc import Messageable
|
||||
|
||||
if DISCORD_AVAILABLE:
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.abc import Messageable
|
||||
|
||||
MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024 # 20MB
|
||||
MAX_MESSAGE_LEN = 2000 # Discord message character limit
|
||||
TYPING_INTERVAL_S = 8
|
||||
|
||||
|
||||
@dataclass
|
||||
class _StreamBuf:
|
||||
"""Per-chat streaming accumulator for progressive Discord message edits."""
|
||||
|
||||
text: str = ""
|
||||
message: Any | None = None
|
||||
last_edit: float = 0.0
|
||||
stream_id: str | None = None
|
||||
|
||||
|
||||
class DiscordConfig(Base):
|
||||
"""Discord channel configuration."""
|
||||
|
||||
enabled: bool = False
|
||||
token: str = ""
|
||||
allow_from: list[str] = Field(default_factory=list)
|
||||
allow_channels: list[str] = Field(default_factory=list) # Allowed channel IDs (empty = all)
|
||||
intents: int = 37377
|
||||
group_policy: Literal["mention", "open"] = "mention"
|
||||
read_receipt_emoji: str = "👀"
|
||||
working_emoji: str = "🔧"
|
||||
working_emoji_delay: float = 2.0
|
||||
streaming: bool = True
|
||||
proxy: str | None = None
|
||||
proxy_username: str | None = None
|
||||
proxy_password: str | None = None
|
||||
|
||||
|
||||
if DISCORD_AVAILABLE:
|
||||
|
||||
class DiscordBotClient(discord.Client):
|
||||
"""discord.py client that forwards events to the channel."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
channel: DiscordChannel,
|
||||
*,
|
||||
intents: discord.Intents,
|
||||
proxy: str | None = None,
|
||||
proxy_auth: aiohttp.BasicAuth | None = None,
|
||||
) -> None:
|
||||
super().__init__(intents=intents, proxy=proxy, proxy_auth=proxy_auth)
|
||||
self._channel = channel
|
||||
self.tree = app_commands.CommandTree(self)
|
||||
self._register_app_commands()
|
||||
|
||||
async def on_ready(self) -> None:
|
||||
self._channel._bot_user_id = str(self.user.id) if self.user else None
|
||||
self._channel.logger.info("bot connected as user {}", self._channel._bot_user_id)
|
||||
try:
|
||||
synced = await self.tree.sync()
|
||||
self._channel.logger.info("app commands synced: {}", len(synced))
|
||||
except Exception as e:
|
||||
self._channel.logger.warning("app command sync failed: {}", e)
|
||||
|
||||
async def on_message(self, message: discord.Message) -> None:
|
||||
await self._channel._handle_discord_message(message)
|
||||
|
||||
async def on_thread_delete(self, thread: discord.Thread) -> None:
|
||||
self._channel._forget_channel(thread)
|
||||
|
||||
async def on_thread_update(self, before: discord.Thread, after: discord.Thread) -> None:
|
||||
if getattr(after, "archived", False):
|
||||
self._channel._forget_channel(after)
|
||||
else:
|
||||
self._channel._remember_channel(after)
|
||||
|
||||
async def _reply_ephemeral(self, interaction: discord.Interaction, text: str) -> bool:
|
||||
"""Send an ephemeral interaction response and report success."""
|
||||
try:
|
||||
await interaction.response.send_message(text, ephemeral=True)
|
||||
return True
|
||||
except Exception as e:
|
||||
self._channel.logger.warning("interaction response failed: {}", e)
|
||||
return False
|
||||
|
||||
async def _resolve_interaction_channel(
|
||||
self,
|
||||
interaction: discord.Interaction,
|
||||
) -> Any | None:
|
||||
channel_id = interaction.channel_id
|
||||
if channel_id is None:
|
||||
return None
|
||||
channel = getattr(interaction, "channel", None) or self.get_channel(channel_id)
|
||||
if channel is None:
|
||||
try:
|
||||
channel = await self.fetch_channel(channel_id)
|
||||
except Exception as e:
|
||||
self._channel.logger.warning("interaction channel {} unavailable: {}", channel_id, e)
|
||||
return None
|
||||
self._channel._remember_channel(channel)
|
||||
return channel
|
||||
|
||||
async def _interaction_channel_allowed(
|
||||
self,
|
||||
interaction: discord.Interaction,
|
||||
channel: Any | None,
|
||||
) -> bool:
|
||||
allow_channels = self._channel.config.allow_channels
|
||||
if not allow_channels:
|
||||
return True
|
||||
if channel is None:
|
||||
channel_id = interaction.channel_id
|
||||
return channel_id is not None and str(channel_id) in allow_channels
|
||||
channel_ids = self._channel._channel_allow_keys(channel)
|
||||
return not channel_ids.isdisjoint(allow_channels)
|
||||
|
||||
async def _forward_slash_command(
|
||||
self,
|
||||
interaction: discord.Interaction,
|
||||
command_text: str,
|
||||
) -> None:
|
||||
sender_id = str(interaction.user.id)
|
||||
channel_id = interaction.channel_id
|
||||
|
||||
if channel_id is None:
|
||||
self._channel.logger.warning("slash command missing channel_id: {}", command_text)
|
||||
return
|
||||
|
||||
if not self._channel.is_allowed(sender_id):
|
||||
await self._reply_ephemeral(interaction, "You are not allowed to use this bot.")
|
||||
return
|
||||
|
||||
channel = await self._resolve_interaction_channel(interaction)
|
||||
if not await self._interaction_channel_allowed(interaction, channel):
|
||||
await self._reply_ephemeral(interaction, "This channel is not allowed for this bot.")
|
||||
return
|
||||
|
||||
await self._reply_ephemeral(interaction, f"Processing {command_text}...")
|
||||
|
||||
metadata: dict[str, Any] = {
|
||||
"interaction_id": str(interaction.id),
|
||||
"guild_id": str(interaction.guild_id) if interaction.guild_id else None,
|
||||
"is_slash_command": True,
|
||||
}
|
||||
session_key = None
|
||||
if channel is not None:
|
||||
parent_channel_id = self._channel._channel_parent_key(channel)
|
||||
if parent_channel_id is not None:
|
||||
metadata["parent_channel_id"] = parent_channel_id
|
||||
metadata["context_chat_id"] = parent_channel_id
|
||||
metadata["thread_id"] = str(channel_id)
|
||||
session_key = f"{self._channel.name}:{parent_channel_id}:thread:{channel_id}"
|
||||
|
||||
await self._channel._handle_message(
|
||||
sender_id=sender_id,
|
||||
chat_id=str(channel_id),
|
||||
content=command_text,
|
||||
metadata=metadata,
|
||||
session_key=session_key,
|
||||
)
|
||||
|
||||
def _register_app_commands(self) -> None:
|
||||
commands = (
|
||||
("new", "Stop current task and start a new conversation", "/new"),
|
||||
("stop", "Stop the current task", "/stop"),
|
||||
("restart", "Restart the bot", "/restart"),
|
||||
("status", "Show bot status", "/status"),
|
||||
("history", "Show recent conversation messages", "/history"),
|
||||
)
|
||||
|
||||
for name, description, command_text in commands:
|
||||
|
||||
@self.tree.command(name=name, description=description)
|
||||
async def command_handler(
|
||||
interaction: discord.Interaction,
|
||||
_command_text: str = command_text,
|
||||
) -> None:
|
||||
await self._forward_slash_command(interaction, _command_text)
|
||||
|
||||
@self.tree.command(name="model", description="Show or switch runtime model preset")
|
||||
@app_commands.describe(preset="Optional model preset name, such as default")
|
||||
async def model_command(
|
||||
interaction: discord.Interaction,
|
||||
preset: str | None = None,
|
||||
) -> None:
|
||||
preset = (preset or "").strip()
|
||||
command_text = f"/model {preset}" if preset else "/model"
|
||||
await self._forward_slash_command(interaction, command_text)
|
||||
|
||||
@self.tree.command(name="trigger", description="Create a named local trigger for this chat")
|
||||
@app_commands.describe(name="Trigger name")
|
||||
async def trigger_command(
|
||||
interaction: discord.Interaction,
|
||||
name: str,
|
||||
) -> None:
|
||||
name = name.strip()
|
||||
command_text = f"/trigger {name}" if name else "/trigger"
|
||||
await self._forward_slash_command(interaction, command_text)
|
||||
|
||||
@self.tree.command(name="help", description="Show available commands")
|
||||
async def help_command(interaction: discord.Interaction) -> None:
|
||||
sender_id = str(interaction.user.id)
|
||||
if not self._channel.is_allowed(sender_id):
|
||||
await self._reply_ephemeral(interaction, "You are not allowed to use this bot.")
|
||||
return
|
||||
channel = await self._resolve_interaction_channel(interaction)
|
||||
if not await self._interaction_channel_allowed(interaction, channel):
|
||||
await self._reply_ephemeral(interaction, "This channel is not allowed for this bot.")
|
||||
return
|
||||
await self._reply_ephemeral(interaction, build_help_text())
|
||||
|
||||
@self.tree.error
|
||||
async def on_app_command_error(
|
||||
interaction: discord.Interaction,
|
||||
error: app_commands.AppCommandError,
|
||||
) -> None:
|
||||
command_name = interaction.command.qualified_name if interaction.command else "?"
|
||||
self._channel.logger.warning(
|
||||
"app command failed user={} channel={} cmd={} error={}",
|
||||
interaction.user.id,
|
||||
interaction.channel_id,
|
||||
command_name,
|
||||
error,
|
||||
)
|
||||
|
||||
async def send_outbound(self, msg: OutboundMessage) -> None:
|
||||
"""Send a nanobot outbound message using Discord transport rules."""
|
||||
channel_id = int(msg.chat_id)
|
||||
|
||||
channel = self._channel._known_channels.get(msg.chat_id) or self.get_channel(channel_id)
|
||||
if channel is None:
|
||||
try:
|
||||
channel = await self.fetch_channel(channel_id)
|
||||
except Exception as e:
|
||||
self._channel.logger.warning("channel {} unavailable: {}", msg.chat_id, e)
|
||||
raise
|
||||
|
||||
reference, mention_settings = self._build_reply_context(channel, msg.reply_to)
|
||||
sent_media = False
|
||||
failed_media: list[str] = []
|
||||
|
||||
for index, media_path in enumerate(msg.media or []):
|
||||
if await self._send_file(
|
||||
channel,
|
||||
media_path,
|
||||
reference=reference if index == 0 else None,
|
||||
mention_settings=mention_settings,
|
||||
):
|
||||
sent_media = True
|
||||
else:
|
||||
failed_media.append(Path(media_path).name)
|
||||
|
||||
for index, chunk in enumerate(
|
||||
self._build_chunks(msg.content or "", failed_media, sent_media)
|
||||
):
|
||||
kwargs: dict[str, Any] = {"content": chunk}
|
||||
if index == 0 and reference is not None and not sent_media:
|
||||
kwargs["reference"] = reference
|
||||
kwargs["allowed_mentions"] = mention_settings
|
||||
await channel.send(**kwargs)
|
||||
|
||||
async def _send_file(
|
||||
self,
|
||||
channel: Messageable,
|
||||
file_path: str,
|
||||
*,
|
||||
reference: discord.PartialMessage | None,
|
||||
mention_settings: discord.AllowedMentions,
|
||||
) -> bool:
|
||||
"""Send a file attachment via discord.py."""
|
||||
path = Path(file_path)
|
||||
if not path.is_file():
|
||||
self._channel.logger.warning("file not found, skipping: {}", file_path)
|
||||
return False
|
||||
|
||||
if path.stat().st_size > MAX_ATTACHMENT_BYTES:
|
||||
self._channel.logger.warning("file too large (>20MB), skipping: {}", path.name)
|
||||
return False
|
||||
|
||||
try:
|
||||
kwargs: dict[str, Any] = {"file": discord.File(path)}
|
||||
if reference is not None:
|
||||
kwargs["reference"] = reference
|
||||
kwargs["allowed_mentions"] = mention_settings
|
||||
await channel.send(**kwargs)
|
||||
self._channel.logger.info("file sent: {}", path.name)
|
||||
return True
|
||||
except Exception:
|
||||
self._channel.logger.exception("Error sending file {}", path.name)
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _build_chunks(content: str, failed_media: list[str], sent_media: bool) -> list[str]:
|
||||
"""Build outbound text chunks, including attachment-failure fallback text."""
|
||||
chunks = split_message(content, MAX_MESSAGE_LEN)
|
||||
if chunks or not failed_media or sent_media:
|
||||
return chunks
|
||||
fallback = "\n".join(f"[attachment: {name} - send failed]" for name in failed_media)
|
||||
return split_message(fallback, MAX_MESSAGE_LEN)
|
||||
|
||||
def _build_reply_context(
|
||||
self,
|
||||
channel: Messageable,
|
||||
reply_to: str | None,
|
||||
) -> tuple[discord.PartialMessage | None, discord.AllowedMentions]:
|
||||
"""Build reply context for outbound messages."""
|
||||
mention_settings = discord.AllowedMentions(replied_user=False)
|
||||
if not reply_to:
|
||||
return None, mention_settings
|
||||
try:
|
||||
message_id = int(reply_to)
|
||||
except ValueError:
|
||||
self._channel.logger.warning("Invalid reply target: {}", reply_to)
|
||||
return None, mention_settings
|
||||
|
||||
return channel.get_partial_message(message_id), mention_settings
|
||||
|
||||
|
||||
class DiscordChannel(BaseChannel):
|
||||
"""Discord channel using discord.py."""
|
||||
|
||||
name = "discord"
|
||||
display_name = "Discord"
|
||||
_STREAM_EDIT_INTERVAL = 0.8
|
||||
|
||||
@classmethod
|
||||
def default_config(cls) -> dict[str, Any]:
|
||||
return DiscordConfig().model_dump(by_alias=True)
|
||||
|
||||
@staticmethod
|
||||
def _channel_key(channel_or_id: Any) -> str:
|
||||
"""Normalize channel-like objects and ids to a stable string key."""
|
||||
channel_id = getattr(channel_or_id, "id", channel_or_id)
|
||||
return str(channel_id)
|
||||
|
||||
@classmethod
|
||||
def _channel_allow_keys(cls, channel: Any) -> set[str]:
|
||||
"""Return channel IDs that can satisfy allow_channels for this channel."""
|
||||
keys = {cls._channel_key(channel)}
|
||||
if parent_key := cls._channel_parent_key(channel):
|
||||
keys.add(parent_key)
|
||||
return keys
|
||||
|
||||
@classmethod
|
||||
def _channel_parent_key(cls, channel: Any) -> str | None:
|
||||
"""Return the parent channel key for a Discord thread-like channel."""
|
||||
parent_id = getattr(channel, "parent_id", None)
|
||||
if parent_id is not None:
|
||||
return cls._channel_key(parent_id)
|
||||
parent = getattr(channel, "parent", None)
|
||||
if parent is not None:
|
||||
return cls._channel_key(parent)
|
||||
return None
|
||||
|
||||
def __init__(self, config: Any, bus: MessageBus):
|
||||
if isinstance(config, dict):
|
||||
config = DiscordConfig.model_validate(config)
|
||||
super().__init__(config, bus)
|
||||
self.config: DiscordConfig = config
|
||||
self._client: DiscordBotClient | None = None
|
||||
self._typing_tasks: dict[str, asyncio.Task[None]] = {}
|
||||
self._bot_user_id: str | None = None
|
||||
self._pending_reactions: dict[str, Any] = {} # chat_id -> message object
|
||||
self._working_emoji_tasks: dict[str, asyncio.Task[None]] = {}
|
||||
self._stream_bufs: dict[str, _StreamBuf] = {}
|
||||
self._known_channels: dict[str, Any] = {}
|
||||
|
||||
def _remember_channel(self, channel: Any) -> None:
|
||||
self._known_channels[self._channel_key(channel)] = channel
|
||||
|
||||
def _forget_channel(self, channel_or_id: Any) -> None:
|
||||
self._known_channels.pop(self._channel_key(channel_or_id), None)
|
||||
|
||||
async def start(self) -> None:
|
||||
"""Start the Discord client."""
|
||||
if not DISCORD_AVAILABLE:
|
||||
self.logger.error("discord.py not installed. Run: nanobot plugins enable discord")
|
||||
return
|
||||
|
||||
if not self.config.token:
|
||||
self.logger.error("bot token not configured")
|
||||
return
|
||||
|
||||
try:
|
||||
intents = discord.Intents.none()
|
||||
intents.value = self.config.intents
|
||||
|
||||
proxy_auth = None
|
||||
has_user = bool(self.config.proxy_username)
|
||||
has_pass = bool(self.config.proxy_password)
|
||||
if has_user and has_pass:
|
||||
import aiohttp
|
||||
|
||||
proxy_auth = aiohttp.BasicAuth(
|
||||
login=self.config.proxy_username,
|
||||
password=self.config.proxy_password,
|
||||
)
|
||||
elif has_user != has_pass:
|
||||
self.logger.warning(
|
||||
"proxy auth incomplete: both proxy_username and "
|
||||
"proxy_password must be set; ignoring partial credentials",
|
||||
)
|
||||
|
||||
self._client = DiscordBotClient(
|
||||
self,
|
||||
intents=intents,
|
||||
proxy=self.config.proxy,
|
||||
proxy_auth=proxy_auth,
|
||||
)
|
||||
except Exception:
|
||||
self.logger.exception("Failed to initialize client")
|
||||
self._client = None
|
||||
self._running = False
|
||||
return
|
||||
|
||||
self._running = True
|
||||
self.logger.info("Starting client via discord.py...")
|
||||
|
||||
try:
|
||||
await self._client.start(self.config.token)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception:
|
||||
self.logger.exception("client startup failed")
|
||||
finally:
|
||||
self._running = False
|
||||
await self._reset_runtime_state(close_client=True)
|
||||
|
||||
async def stop(self) -> None:
|
||||
"""Stop the Discord channel."""
|
||||
self._running = False
|
||||
await self._reset_runtime_state(close_client=True)
|
||||
|
||||
async def send(self, msg: OutboundMessage) -> None:
|
||||
"""Send a message through Discord using discord.py."""
|
||||
client = self._client
|
||||
if client is None or not client.is_ready():
|
||||
raise RuntimeError("Discord client is not ready")
|
||||
|
||||
is_progress = isinstance(msg.event, ProgressEvent)
|
||||
|
||||
try:
|
||||
await client.send_outbound(msg)
|
||||
except Exception:
|
||||
self.logger.exception("Error sending message")
|
||||
raise
|
||||
finally:
|
||||
if not is_progress:
|
||||
await self._stop_typing(msg.chat_id)
|
||||
await self._clear_reactions(msg.chat_id)
|
||||
|
||||
async def send_delta(
|
||||
self,
|
||||
chat_id: str,
|
||||
delta: str,
|
||||
metadata: dict[str, Any] | None = None,
|
||||
*,
|
||||
stream_id: str | None = None,
|
||||
stream_end: bool = False,
|
||||
resuming: bool = False,
|
||||
) -> None:
|
||||
"""Progressive Discord delivery: send once, then edit until the stream ends."""
|
||||
client = self._client
|
||||
if client is None or not client.is_ready():
|
||||
self.logger.warning("client not ready; dropping stream delta")
|
||||
return
|
||||
|
||||
if stream_end:
|
||||
buf = self._stream_bufs.get(chat_id)
|
||||
if not buf or buf.message is None or not buf.text:
|
||||
return
|
||||
if stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id:
|
||||
return
|
||||
await self._finalize_stream(chat_id, buf)
|
||||
return
|
||||
|
||||
buf = self._stream_bufs.get(chat_id)
|
||||
if buf is None or (
|
||||
stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id
|
||||
):
|
||||
buf = _StreamBuf(stream_id=stream_id)
|
||||
self._stream_bufs[chat_id] = buf
|
||||
elif buf.stream_id is None:
|
||||
buf.stream_id = stream_id
|
||||
|
||||
buf.text += delta
|
||||
if not buf.text.strip():
|
||||
return
|
||||
|
||||
target = await self._resolve_channel(chat_id)
|
||||
if target is None:
|
||||
self.logger.warning("stream target {} unavailable", chat_id)
|
||||
return
|
||||
|
||||
now = time.monotonic()
|
||||
if buf.message is None:
|
||||
try:
|
||||
buf.message = await target.send(content=buf.text)
|
||||
buf.last_edit = now
|
||||
except Exception as e:
|
||||
self.logger.warning("stream initial send failed: {}", e)
|
||||
raise
|
||||
return
|
||||
|
||||
if (now - buf.last_edit) < self._STREAM_EDIT_INTERVAL:
|
||||
return
|
||||
|
||||
try:
|
||||
await buf.message.edit(content=DiscordBotClient._build_chunks(buf.text, [], False)[0])
|
||||
buf.last_edit = now
|
||||
except Exception as e:
|
||||
self.logger.warning("stream edit failed: {}", e)
|
||||
raise
|
||||
|
||||
async def _handle_discord_message(self, message: discord.Message) -> None:
|
||||
"""Handle incoming Discord messages from discord.py.
|
||||
|
||||
Self-loop guard: only drop messages from this bot's own account. Messages
|
||||
from other bots are allowed through so multi-agent setups (one bot asking
|
||||
another for help, a bot mentioning another by @name, etc.) can work.
|
||||
Bot-from-bot loops are still prevented per-instance because each bot
|
||||
still ignores its own outbound messages. (#3217)
|
||||
"""
|
||||
if self._bot_user_id is not None and str(message.author.id) == self._bot_user_id:
|
||||
return
|
||||
if self._is_system_message(message):
|
||||
return
|
||||
|
||||
sender_id = str(message.author.id)
|
||||
channel_id = self._channel_key(message.channel)
|
||||
self._remember_channel(message.channel)
|
||||
content = message.content or ""
|
||||
|
||||
if not self._should_accept_inbound(message, sender_id, content):
|
||||
return
|
||||
|
||||
media_paths, attachment_markers = await self._download_attachments(message.attachments)
|
||||
full_content = self._compose_inbound_content(content, attachment_markers)
|
||||
metadata = self._build_inbound_metadata(message)
|
||||
parent_channel_id = self._channel_parent_key(message.channel)
|
||||
session_key = None
|
||||
if parent_channel_id is not None:
|
||||
metadata["parent_channel_id"] = parent_channel_id
|
||||
metadata["context_chat_id"] = parent_channel_id
|
||||
metadata["thread_id"] = channel_id
|
||||
session_key = f"{self.name}:{parent_channel_id}:thread:{channel_id}"
|
||||
|
||||
await self._start_typing(message.channel)
|
||||
|
||||
# Add read receipt reaction immediately, working emoji after delay
|
||||
try:
|
||||
await message.add_reaction(self.config.read_receipt_emoji)
|
||||
self._pending_reactions[channel_id] = message
|
||||
except Exception as e:
|
||||
self.logger.debug("Failed to add read receipt reaction: {}", e)
|
||||
|
||||
# Delayed working indicator (cosmetic — not tied to subagent lifecycle)
|
||||
async def _delayed_working_emoji() -> None:
|
||||
await asyncio.sleep(self.config.working_emoji_delay)
|
||||
with suppress(Exception):
|
||||
await message.add_reaction(self.config.working_emoji)
|
||||
|
||||
self._working_emoji_tasks[channel_id] = asyncio.create_task(_delayed_working_emoji())
|
||||
|
||||
try:
|
||||
await self._handle_message(
|
||||
sender_id=sender_id,
|
||||
chat_id=channel_id,
|
||||
content=full_content,
|
||||
media=media_paths,
|
||||
metadata=metadata,
|
||||
session_key=session_key,
|
||||
is_dm=message.guild is None,
|
||||
)
|
||||
except Exception:
|
||||
await self._clear_reactions(channel_id)
|
||||
await self._stop_typing(channel_id)
|
||||
raise
|
||||
|
||||
async def _on_message(self, message: discord.Message) -> None:
|
||||
"""Backward-compatible alias for legacy tests/callers."""
|
||||
await self._handle_discord_message(message)
|
||||
|
||||
async def _resolve_channel(self, chat_id: str) -> Any | None:
|
||||
"""Resolve a Discord channel from cache first, then network fetch."""
|
||||
client = self._client
|
||||
if client is None or not client.is_ready():
|
||||
return None
|
||||
channel = self._known_channels.get(chat_id)
|
||||
if channel is not None:
|
||||
return channel
|
||||
channel_id = int(chat_id)
|
||||
channel = client.get_channel(channel_id)
|
||||
if channel is not None:
|
||||
return channel
|
||||
try:
|
||||
return await client.fetch_channel(channel_id)
|
||||
except Exception as e:
|
||||
self.logger.warning("channel {} unavailable: {}", chat_id, e)
|
||||
return None
|
||||
|
||||
async def _finalize_stream(self, chat_id: str, buf: _StreamBuf) -> None:
|
||||
"""Commit the final streamed content and flush overflow chunks."""
|
||||
chunks = DiscordBotClient._build_chunks(buf.text, [], False)
|
||||
if not chunks:
|
||||
self._stream_bufs.pop(chat_id, None)
|
||||
return
|
||||
|
||||
try:
|
||||
await buf.message.edit(content=chunks[0])
|
||||
except Exception as e:
|
||||
self.logger.warning("final stream edit failed: {}", e)
|
||||
raise
|
||||
|
||||
target = getattr(buf.message, "channel", None) or await self._resolve_channel(chat_id)
|
||||
if target is None:
|
||||
self.logger.warning("stream follow-up target {} unavailable", chat_id)
|
||||
self._stream_bufs.pop(chat_id, None)
|
||||
return
|
||||
|
||||
for extra_chunk in chunks[1:]:
|
||||
await target.send(content=extra_chunk)
|
||||
|
||||
self._stream_bufs.pop(chat_id, None)
|
||||
await self._stop_typing(chat_id)
|
||||
await self._clear_reactions(chat_id)
|
||||
|
||||
def _should_accept_inbound(
|
||||
self,
|
||||
message: discord.Message,
|
||||
sender_id: str,
|
||||
content: str,
|
||||
) -> bool:
|
||||
"""Check if inbound Discord message should be processed."""
|
||||
# Reject unauthorized guild messages before any side effects, but let DMs
|
||||
# reach BaseChannel._handle_message so it can issue a pairing code.
|
||||
if message.guild is not None and not self.is_allowed(sender_id):
|
||||
return False
|
||||
# Channel-based filtering: only respond in allowed channels
|
||||
allow_channels = self.config.allow_channels
|
||||
if allow_channels:
|
||||
channel_ids = self._channel_allow_keys(message.channel)
|
||||
if channel_ids.isdisjoint(allow_channels):
|
||||
return False
|
||||
if message.guild is not None and not self._should_respond_in_group(message, content):
|
||||
return False
|
||||
return True
|
||||
|
||||
async def _download_attachments(
|
||||
self,
|
||||
attachments: list[discord.Attachment],
|
||||
) -> tuple[list[str], list[str]]:
|
||||
"""Download supported attachments and return paths + display markers."""
|
||||
media_paths: list[str] = []
|
||||
markers: list[str] = []
|
||||
media_dir = get_media_dir("discord")
|
||||
|
||||
for attachment in attachments:
|
||||
filename = attachment.filename or "attachment"
|
||||
if attachment.size and attachment.size > MAX_ATTACHMENT_BYTES:
|
||||
markers.append(f"[attachment: {filename} - too large]")
|
||||
continue
|
||||
try:
|
||||
media_dir.mkdir(parents=True, exist_ok=True)
|
||||
safe_name = safe_filename(filename)
|
||||
file_path = media_dir / f"{attachment.id}_{safe_name}"
|
||||
await attachment.save(file_path)
|
||||
media_paths.append(str(file_path))
|
||||
markers.append(f"[attachment: {file_path.name}]")
|
||||
except Exception as e:
|
||||
self.logger.warning("Failed to download attachment: {}", e)
|
||||
markers.append(f"[attachment: {filename} - download failed]")
|
||||
|
||||
return media_paths, markers
|
||||
|
||||
@staticmethod
|
||||
def _compose_inbound_content(content: str, attachment_markers: list[str]) -> str:
|
||||
"""Combine message text with attachment markers."""
|
||||
content_parts = [content] if content else []
|
||||
content_parts.extend(attachment_markers)
|
||||
return "\n".join(part for part in content_parts if part) or "[empty message]"
|
||||
|
||||
@staticmethod
|
||||
def _is_system_message(message: discord.Message) -> bool:
|
||||
"""Return True for Discord system messages that carry no user prompt."""
|
||||
message_type = getattr(message, "type", discord.MessageType.default)
|
||||
return message_type not in {discord.MessageType.default, discord.MessageType.reply}
|
||||
|
||||
@staticmethod
|
||||
def _build_inbound_metadata(message: discord.Message) -> dict[str, str | None]:
|
||||
"""Build metadata for inbound Discord messages."""
|
||||
reply_to = (
|
||||
str(message.reference.message_id)
|
||||
if message.reference and message.reference.message_id
|
||||
else None
|
||||
)
|
||||
return {
|
||||
"message_id": str(message.id),
|
||||
"guild_id": str(message.guild.id) if message.guild else None,
|
||||
"reply_to": reply_to,
|
||||
}
|
||||
|
||||
def _should_respond_in_group(self, message: discord.Message, content: str) -> bool:
|
||||
"""Check if the bot should respond in a guild channel based on policy."""
|
||||
if self.config.group_policy == "open":
|
||||
return True
|
||||
|
||||
if self.config.group_policy == "mention":
|
||||
bot_user_id = self._bot_user_id
|
||||
if bot_user_id is None and self._client and self._client.user:
|
||||
bot_user_id = str(self._client.user.id)
|
||||
if bot_user_id is None:
|
||||
self.logger.debug(
|
||||
"message in {} ignored (bot identity unavailable)", message.channel.id
|
||||
)
|
||||
return False
|
||||
|
||||
if any(str(user.id) == bot_user_id for user in message.mentions):
|
||||
return True
|
||||
if bot_user_id in {str(user_id) for user_id in getattr(message, "raw_mentions", [])}:
|
||||
return True
|
||||
if f"<@{bot_user_id}>" in content or f"<@!{bot_user_id}>" in content:
|
||||
return True
|
||||
if self._references_bot_message(message, bot_user_id):
|
||||
return True
|
||||
|
||||
self.logger.debug("message in {} ignored (bot not mentioned)", message.channel.id)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def _references_bot_message(message: discord.Message, bot_user_id: str) -> bool:
|
||||
"""Return True when a Discord reply targets a message authored by this bot."""
|
||||
reference = getattr(message, "reference", None)
|
||||
if reference is None:
|
||||
return False
|
||||
referenced_message = getattr(reference, "resolved", None) or getattr(
|
||||
reference, "cached_message", None
|
||||
)
|
||||
author = getattr(referenced_message, "author", None)
|
||||
return str(getattr(author, "id", "")) == bot_user_id
|
||||
|
||||
async def _start_typing(self, channel: Messageable) -> None:
|
||||
"""Start periodic typing indicator for a channel."""
|
||||
channel_id = self._channel_key(channel)
|
||||
await self._stop_typing(channel_id)
|
||||
|
||||
async def typing_loop() -> None:
|
||||
while self._running:
|
||||
try:
|
||||
async with channel.typing():
|
||||
await asyncio.sleep(TYPING_INTERVAL_S)
|
||||
except asyncio.CancelledError:
|
||||
return
|
||||
except Exception as e:
|
||||
self.logger.debug("typing indicator failed for {}: {}", channel_id, e)
|
||||
return
|
||||
|
||||
self._typing_tasks[channel_id] = asyncio.create_task(typing_loop())
|
||||
|
||||
async def _stop_typing(self, channel_id: str) -> None:
|
||||
"""Stop typing indicator for a channel."""
|
||||
task = self._typing_tasks.pop(self._channel_key(channel_id), None)
|
||||
if task is None:
|
||||
return
|
||||
task.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await task
|
||||
|
||||
async def _clear_reactions(self, chat_id: str) -> None:
|
||||
"""Remove all pending reactions after bot replies."""
|
||||
# Cancel delayed working emoji if it hasn't fired yet
|
||||
task = self._working_emoji_tasks.pop(chat_id, None)
|
||||
if task and not task.done():
|
||||
task.cancel()
|
||||
|
||||
msg_obj = self._pending_reactions.pop(chat_id, None)
|
||||
if msg_obj is None:
|
||||
return
|
||||
bot_user = self._client.user if self._client else None
|
||||
for emoji in (self.config.read_receipt_emoji, self.config.working_emoji):
|
||||
with suppress(Exception):
|
||||
await msg_obj.remove_reaction(emoji, bot_user)
|
||||
|
||||
async def _cancel_all_typing(self) -> None:
|
||||
"""Stop all typing tasks."""
|
||||
channel_ids = list(self._typing_tasks)
|
||||
for channel_id in channel_ids:
|
||||
await self._stop_typing(channel_id)
|
||||
|
||||
async def _reset_runtime_state(self, close_client: bool) -> None:
|
||||
"""Reset client and typing state."""
|
||||
await self._cancel_all_typing()
|
||||
self._stream_bufs.clear()
|
||||
self._known_channels.clear()
|
||||
if close_client and self._client is not None and not self._client.is_closed():
|
||||
try:
|
||||
await self._client.close()
|
||||
except Exception as e:
|
||||
self.logger.warning("client close failed: {}", e)
|
||||
self._client = None
|
||||
self._bot_user_id = None
|
||||
@@ -0,0 +1 @@
|
||||
"""Tests for the Discord channel package."""
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,69 @@
|
||||
"""Discord setup validation owned by the channel package."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from nanobot.channels.contracts import ChannelValidationContext
|
||||
from nanobot.channels.validation import (
|
||||
check,
|
||||
http_get,
|
||||
payload,
|
||||
required_checks,
|
||||
status_from_checks,
|
||||
string_value,
|
||||
)
|
||||
|
||||
|
||||
def validate(values: dict[str, Any], _context: ChannelValidationContext) -> dict[str, Any]:
|
||||
checks, missing = required_checks("discord", values)
|
||||
token = string_value(values.get("token"))
|
||||
if token:
|
||||
try:
|
||||
data = http_get(
|
||||
"https://discord.com/api/v10/users/@me",
|
||||
headers={"Authorization": f"Bot {token}"},
|
||||
)
|
||||
bot_id = str(data.get("id") or "")
|
||||
checks.append(check("bot_token", "Bot token", "pass", "Discord accepted the bot token."))
|
||||
identity = {
|
||||
"name": data.get("global_name") or data.get("username"),
|
||||
"account": bot_id,
|
||||
}
|
||||
if bot_id:
|
||||
checks.append(
|
||||
check(
|
||||
"invite",
|
||||
"Server invite",
|
||||
"pass",
|
||||
"Use this generated OAuth URL to invite the bot.",
|
||||
action_url=(
|
||||
"https://discord.com/oauth2/authorize"
|
||||
f"?client_id={bot_id}&scope=bot%20applications.commands"
|
||||
),
|
||||
)
|
||||
)
|
||||
return payload(
|
||||
"discord",
|
||||
"connected",
|
||||
checks,
|
||||
identity=identity,
|
||||
missing_fields=missing,
|
||||
)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
checks.append(
|
||||
check(
|
||||
"bot_token",
|
||||
"Bot token",
|
||||
"fail",
|
||||
f"Discord rejected the token: HTTP {exc.response.status_code}",
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
checks.append(
|
||||
check("bot_token", "Bot token", "warn", f"Could not reach Discord now: {exc}")
|
||||
)
|
||||
return status_from_checks("discord", checks, missing)
|
||||
|
||||
|
||||
__all__ = ["validate"]
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { ChannelUiContribution } from "@/channel-plugins/types";
|
||||
import { chatAppGuideUrl } from "@/components/settings/channels/catalog";
|
||||
|
||||
export default {
|
||||
presentation: {
|
||||
displayName: "Discord",
|
||||
initials: "DC",
|
||||
color: "#5865F2",
|
||||
logoUrl: "https://discord.com/favicon.ico",
|
||||
setup: {
|
||||
mode: "credentials",
|
||||
docsUrl: chatAppGuideUrl("discord"),
|
||||
fields: [
|
||||
{ key: "channels.discord.token" },
|
||||
{ key: "channels.discord.allowChannels" },
|
||||
{ key: "channels.discord.groupPolicy" },
|
||||
],
|
||||
},
|
||||
},
|
||||
} satisfies ChannelUiContribution;
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Use nanobot from Discord servers and DMs.",
|
||||
"requirements": "Discord bot token, permissions, gateway",
|
||||
"setup": {
|
||||
"docsLabel": "Open Discord setup",
|
||||
"officialLabel": "Open Discord portal",
|
||||
"tryIt": "Mention the bot in a server or send it a direct message.",
|
||||
"summary": "Enable turns on Discord support. Discord still needs a bot token and server permissions.",
|
||||
"steps": [
|
||||
"Create a bot in Discord Developer Portal and copy its token.",
|
||||
"Invite the bot to your server with message read/send and slash command permissions.",
|
||||
"Save and enable Discord, then mention the bot or send a direct message."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "Bot token",
|
||||
"placeholder": "Discord bot token",
|
||||
"help": "Create it from the Bot page in Discord Developer Portal."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "Allowed channels",
|
||||
"placeholder": "Channel IDs, comma separated",
|
||||
"help": "Leave empty to allow any channel the bot can read."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "Group behavior",
|
||||
"choices": {
|
||||
"mention": "Mention only",
|
||||
"open": "All messages",
|
||||
"allowlist": "Allowlist"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "Allowed users",
|
||||
"placeholder": "User IDs, comma separated"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Usa nanobot en servidores y mensajes directos de Discord.",
|
||||
"requirements": "Token del bot de Discord, permisos y gateway",
|
||||
"setup": {
|
||||
"docsLabel": "Abrir guía de Discord",
|
||||
"officialLabel": "Abrir portal de Discord",
|
||||
"tryIt": "Menciona al bot en un servidor o envíale un mensaje directo.",
|
||||
"summary": "Activar habilita Discord. Aún necesitas el token del bot y permisos del servidor.",
|
||||
"steps": [
|
||||
"Crea un bot en Discord Developer Portal y copia su token.",
|
||||
"Invítalo al servidor con permisos para leer/enviar mensajes y usar comandos slash.",
|
||||
"Guarda y activa Discord; después menciona al bot o envíale un mensaje directo."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "Token del bot",
|
||||
"placeholder": "Token del bot de Discord",
|
||||
"help": "Créalo desde la página Bot de Discord Developer Portal."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "Canales permitidos",
|
||||
"placeholder": "ID de canal separados por comas",
|
||||
"help": "Déjalo vacío para permitir cualquier canal que el bot pueda leer."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "Comportamiento en grupos",
|
||||
"choices": {
|
||||
"mention": "Solo menciones",
|
||||
"open": "Todos los mensajes",
|
||||
"allowlist": "Lista permitida"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "Usuarios permitidos",
|
||||
"placeholder": "ID de usuario separados por comas"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Utilisez nanobot sur les serveurs Discord et en messages privés.",
|
||||
"requirements": "Jeton du bot Discord, permissions et passerelle",
|
||||
"setup": {
|
||||
"docsLabel": "Ouvrir le guide Discord",
|
||||
"officialLabel": "Ouvrir le portail Discord",
|
||||
"tryIt": "Mentionnez le bot sur un serveur ou envoyez-lui un message privé.",
|
||||
"summary": "L’activation ouvre la prise en charge de Discord. Un jeton de bot et des permissions serveur restent nécessaires.",
|
||||
"steps": [
|
||||
"Créez un bot dans le portail développeur Discord et copiez son jeton.",
|
||||
"Invitez-le sur votre serveur avec les permissions de lecture, d’envoi et de commandes slash.",
|
||||
"Enregistrez et activez Discord, puis mentionnez le bot ou envoyez-lui un message privé."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "Jeton du bot",
|
||||
"placeholder": "Jeton du bot Discord",
|
||||
"help": "Créez-le depuis la page Bot du portail développeur Discord."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "Salons autorisés",
|
||||
"placeholder": "ID de salon séparés par des virgules",
|
||||
"help": "Laissez vide pour autoriser tous les salons lisibles par le bot."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "Comportement en groupe",
|
||||
"choices": {
|
||||
"mention": "Mentions uniquement",
|
||||
"open": "Tous les messages",
|
||||
"allowlist": "Liste d’autorisation"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "Utilisateurs autorisés",
|
||||
"placeholder": "ID utilisateur séparés par des virgules"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Gunakan nanobot dari server dan DM Discord.",
|
||||
"requirements": "Token bot Discord, izin, dan gateway",
|
||||
"setup": {
|
||||
"docsLabel": "Buka panduan Discord",
|
||||
"officialLabel": "Buka portal Discord",
|
||||
"tryIt": "Sebut bot di server atau kirim pesan langsung.",
|
||||
"summary": "Mengaktifkan akan menyalakan dukungan Discord. Token bot dan izin server tetap diperlukan.",
|
||||
"steps": [
|
||||
"Buat bot di Discord Developer Portal dan salin tokennya.",
|
||||
"Undang bot ke server dengan izin baca/kirim pesan dan perintah slash.",
|
||||
"Simpan dan aktifkan Discord, lalu sebut bot atau kirim DM."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "Token bot",
|
||||
"placeholder": "Token bot Discord",
|
||||
"help": "Buat dari halaman Bot di Discord Developer Portal."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "Channel yang diizinkan",
|
||||
"placeholder": "ID channel, dipisahkan koma",
|
||||
"help": "Kosongkan untuk mengizinkan semua channel yang dapat dibaca bot."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "Perilaku grup",
|
||||
"choices": {
|
||||
"mention": "Hanya sebutan",
|
||||
"open": "Semua pesan",
|
||||
"allowlist": "Daftar izin"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "Pengguna yang diizinkan",
|
||||
"placeholder": "ID pengguna, dipisahkan koma"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Discord サーバーと DM から nanobot を利用します。",
|
||||
"requirements": "Discord ボットトークン、権限、ゲートウェイ",
|
||||
"setup": {
|
||||
"docsLabel": "Discord 設定ガイドを開く",
|
||||
"officialLabel": "Discord ポータルを開く",
|
||||
"tryIt": "サーバーでボットをメンションするか、DM を送信します。",
|
||||
"summary": "有効化すると Discord 対応がオンになります。ボットトークンとサーバー権限が必要です。",
|
||||
"steps": [
|
||||
"Discord Developer Portal でボットを作成し、トークンをコピーします。",
|
||||
"メッセージの読み書きとスラッシュコマンド権限を付けてサーバーに招待します。",
|
||||
"保存して Discord を有効にし、メンションまたは DM を送信します。"
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "ボットトークン",
|
||||
"placeholder": "Discord ボットトークン",
|
||||
"help": "Discord Developer Portal の Bot ページで作成します。"
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "許可するチャンネル",
|
||||
"placeholder": "チャンネル ID(カンマ区切り)",
|
||||
"help": "空欄の場合、ボットが読めるすべてのチャンネルを許可します。"
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "グループでの動作",
|
||||
"choices": {
|
||||
"mention": "メンションのみ",
|
||||
"open": "すべてのメッセージ",
|
||||
"allowlist": "許可リスト"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "許可するユーザー",
|
||||
"placeholder": "ユーザー ID(カンマ区切り)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Discord 서버와 DM에서 nanobot을 사용합니다.",
|
||||
"requirements": "Discord 봇 토큰, 권한 및 게이트웨이",
|
||||
"setup": {
|
||||
"docsLabel": "Discord 설정 가이드 열기",
|
||||
"officialLabel": "Discord 포털 열기",
|
||||
"tryIt": "서버에서 봇을 멘션하거나 DM을 보내세요.",
|
||||
"summary": "활성화하면 Discord 지원이 켜집니다. 봇 토큰과 서버 권한이 필요합니다.",
|
||||
"steps": [
|
||||
"Discord Developer Portal에서 봇을 만들고 토큰을 복사하세요.",
|
||||
"메시지 읽기/보내기 및 슬래시 명령 권한으로 서버에 초대하세요.",
|
||||
"저장하고 Discord를 활성화한 다음 봇을 멘션하거나 DM을 보내세요."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "봇 토큰",
|
||||
"placeholder": "Discord 봇 토큰",
|
||||
"help": "Discord Developer Portal의 Bot 페이지에서 생성하세요."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "허용된 채널",
|
||||
"placeholder": "채널 ID, 쉼표로 구분",
|
||||
"help": "비워 두면 봇이 읽을 수 있는 모든 채널을 허용합니다."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "그룹 동작",
|
||||
"choices": {
|
||||
"mention": "멘션만",
|
||||
"open": "모든 메시지",
|
||||
"allowlist": "허용 목록"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "허용된 사용자",
|
||||
"placeholder": "사용자 ID, 쉼표로 구분"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Use o nanobot em servidores e DMs do Discord.",
|
||||
"requirements": "Token do bot Discord, permissões e gateway",
|
||||
"setup": {
|
||||
"docsLabel": "Abrir guia do Discord",
|
||||
"officialLabel": "Abrir portal do Discord",
|
||||
"tryIt": "Mencione o bot em um servidor ou envie uma mensagem direta.",
|
||||
"summary": "Ativar liga o suporte ao Discord. O token do bot e as permissões do servidor ainda são necessários.",
|
||||
"steps": [
|
||||
"Crie um bot no Discord Developer Portal e copie o token.",
|
||||
"Convide-o para o servidor com permissões de leitura/envio e comandos slash.",
|
||||
"Salve e ative o Discord; depois, mencione o bot ou envie uma DM."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "Token do bot",
|
||||
"placeholder": "Token do bot Discord",
|
||||
"help": "Crie-o na página Bot do Discord Developer Portal."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "Canais permitidos",
|
||||
"placeholder": "IDs de canal separados por vírgulas",
|
||||
"help": "Deixe vazio para permitir qualquer canal que o bot consiga ler."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "Comportamento em grupos",
|
||||
"choices": {
|
||||
"mention": "Somente menções",
|
||||
"open": "Todas as mensagens",
|
||||
"allowlist": "Lista de permissão"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "Usuários permitidos",
|
||||
"placeholder": "IDs de usuário separados por vírgulas"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Sử dụng nanobot trong máy chủ và tin nhắn riêng Discord.",
|
||||
"requirements": "Token bot Discord, quyền và gateway",
|
||||
"setup": {
|
||||
"docsLabel": "Mở hướng dẫn Discord",
|
||||
"officialLabel": "Mở cổng Discord",
|
||||
"tryIt": "Nhắc bot trong máy chủ hoặc gửi tin nhắn riêng.",
|
||||
"summary": "Bật sẽ kích hoạt hỗ trợ Discord. Bạn vẫn cần token bot và quyền trên máy chủ.",
|
||||
"steps": [
|
||||
"Tạo bot trong Discord Developer Portal và sao chép token.",
|
||||
"Mời bot vào máy chủ với quyền đọc/gửi tin nhắn và lệnh slash.",
|
||||
"Lưu và bật Discord, sau đó nhắc bot hoặc gửi tin nhắn riêng."
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "Token bot",
|
||||
"placeholder": "Token bot Discord",
|
||||
"help": "Tạo từ trang Bot trong Discord Developer Portal."
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "Kênh được phép",
|
||||
"placeholder": "ID kênh, phân tách bằng dấu phẩy",
|
||||
"help": "Để trống để cho phép mọi kênh bot có thể đọc."
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "Hành vi trong nhóm",
|
||||
"choices": {
|
||||
"mention": "Chỉ khi được nhắc",
|
||||
"open": "Mọi tin nhắn",
|
||||
"allowlist": "Danh sách cho phép"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "Người dùng được phép",
|
||||
"placeholder": "ID người dùng, phân tách bằng dấu phẩy"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "在 Discord 服务器和私信中使用 nanobot。",
|
||||
"requirements": "Discord 机器人令牌、权限和网关",
|
||||
"setup": {
|
||||
"docsLabel": "打开 Discord 配置指南",
|
||||
"officialLabel": "打开 Discord 开发者后台",
|
||||
"tryIt": "在服务器中提及机器人,或向它发送私信。",
|
||||
"summary": "启用只会打开 Discord 支持;还需要机器人令牌和服务器权限。",
|
||||
"steps": [
|
||||
"在 Discord Developer Portal 中创建机器人并复制令牌。",
|
||||
"将机器人邀请到服务器,并授予读取/发送消息及斜杠命令权限。",
|
||||
"保存并启用 Discord,然后提及机器人或发送私信。"
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "机器人令牌",
|
||||
"placeholder": "Discord 机器人令牌",
|
||||
"help": "从 Discord Developer Portal 的 Bot 页面创建。"
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "允许的频道",
|
||||
"placeholder": "频道 ID,用逗号分隔",
|
||||
"help": "留空则允许机器人可读取的所有频道。"
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "群组行为",
|
||||
"choices": {
|
||||
"mention": "仅提及时",
|
||||
"open": "所有消息",
|
||||
"allowlist": "白名单"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "允许的用户",
|
||||
"placeholder": "用户 ID,用逗号分隔"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "在 Discord 伺服器和私訊中使用 nanobot。",
|
||||
"requirements": "Discord 機器人權杖、權限和閘道",
|
||||
"setup": {
|
||||
"docsLabel": "開啟 Discord 設定指南",
|
||||
"officialLabel": "開啟 Discord 開發者後台",
|
||||
"tryIt": "在伺服器中提及機器人,或向它傳送私訊。",
|
||||
"summary": "啟用只會開啟 Discord 支援;還需要機器人權杖和伺服器權限。",
|
||||
"steps": [
|
||||
"在 Discord Developer Portal 中建立機器人並複製權杖。",
|
||||
"將機器人邀請到伺服器,並授予讀取/傳送訊息及斜線指令權限。",
|
||||
"儲存並啟用 Discord,然後提及機器人或傳送私訊。"
|
||||
],
|
||||
"fields": {
|
||||
"token": {
|
||||
"label": "機器人權杖",
|
||||
"placeholder": "Discord 機器人權杖",
|
||||
"help": "從 Discord Developer Portal 的 Bot 頁面建立。"
|
||||
},
|
||||
"allowChannels": {
|
||||
"label": "允許的頻道",
|
||||
"placeholder": "頻道 ID,以逗號分隔",
|
||||
"help": "留空則允許機器人可讀取的所有頻道。"
|
||||
},
|
||||
"groupPolicy": {
|
||||
"label": "群組行為",
|
||||
"choices": {
|
||||
"mention": "僅提及時",
|
||||
"open": "所有訊息",
|
||||
"allowlist": "允許清單"
|
||||
}
|
||||
},
|
||||
"allowFrom": {
|
||||
"label": "允許的使用者",
|
||||
"placeholder": "使用者 ID,以逗號分隔"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user