AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend/copilot-bot): post intro message when added to a server (#13220)
Bently8dc55d916528
diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.pyindex 10482a8251..ac841d791d 100644--- a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/adapter.py@@ -21,7 +21,7 @@ from ..base import (MessageHistoryEntry,PlatformAdapter,)-from . import commands, config+from . import commands, config, intrologger = logging.getLogger(__name__)THREAD_HISTORY_LIMIT = 20@@ -187,6 +187,23 @@ class DiscordAdapter(PlatformAdapter):except Exception:logger.exception("Failed to sync slash commands")+ @self._client.event+ async def on_guild_join(guild: discord.Guild) -> None:+ channel = intro.pick_intro_channel(guild)+ if channel is None:+ logger.info(+ "No sendable channel in guild %s for intro message", guild.id+ )+ return+ try:+ await channel.send(+ intro.intro_message(),+ tts=False,+ allowed_mentions=discord.AllowedMentions.none(),+ )+ except discord.HTTPException:+ logger.exception("Failed to post intro message in guild %s", guild.id)+@self._client.eventasync def on_message(message: discord.Message) -> None:if self._should_ignore_message(message):diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/intro.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/intro.pynew file mode 100644index 0000000000..8abc6a10f1--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/intro.py@@ -0,0 +1,35 @@+"""Welcome message posted when AutoPilot is added to a Discord server."""++import discord+++def pick_intro_channel(guild: discord.Guild) -> discord.TextChannel | None:+ """Return the best channel to post the welcome message in.++ Prefer the server's system channel; fall back to the first text channel+ we can send in. ``None`` if there's nowhere we can post.+ """+ me = guild.me+ if me is None:+ return None+ system_channel = guild.system_channel+ if system_channel is not None and system_channel.permissions_for(me).send_messages:+ return system_channel+ for channel in guild.text_channels:+ if channel.permissions_for(me).send_messages:+ return channel+ return None+++def intro_message() -> str:+ return (+ "**Hey, I'm AutoPilot — AutoGPT in your Discord.** Thanks for adding me!\n\n"+ "To get started, a server admin runs `/setup` and follows the link "+ "to connect this server to an AutoGPT account.\n\n"+ "Once linked, mention me anywhere I can read messages "+ "(e.g. `@AutoPilot summarise this channel`) and I'll spin up a "+ "thread for our chat.\n\n"+ "You can also **DM me** to chat 1:1 — send any message and I'll "+ "walk you through linking your own DMs.\n\n"+ "Commands: `/help` `/setup` `/new`"+ )diff --git a/autogpt_platform/backend/backend/copilot/bot/adapters/discord/intro_test.py b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/intro_test.pynew file mode 100644index 0000000000..c8d9d48395--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/bot/adapters/discord/intro_test.py@@ -0,0 +1,74 @@+"""Tests for the on-add intro helpers."""++from unittest.mock import MagicMock++import discord++from .intro import intro_message, pick_intro_channel+++def _channel(name: str, sendable: bool) -> MagicMock:+ ch = MagicMock(spec=discord.TextChannel)+ ch.name = name+ perms = MagicMock()+ perms.send_messages = sendable+ ch.permissions_for = MagicMock(return_value=perms)+ return ch+++def _guild(+ *,+ system_channel: MagicMock | None,+ text_channels: list[MagicMock],+ has_me: bool = True,+) -> MagicMock:+ guild = MagicMock()+ guild.me = MagicMock() if has_me else None+ guild.system_channel = system_channel+ guild.text_channels = text_channels+ return guild+++def test_prefers_system_channel_when_sendable():+ sys_ch = _channel("system", sendable=True)+ other = _channel("other", sendable=True)+ guild = _guild(system_channel=sys_ch, text_channels=[other])++ assert pick_intro_channel(guild) is sys_ch+++def test_falls_back_to_first_sendable_text_channel():+ sys_ch = _channel("system", sendable=False)+ a = _channel("a", sendable=False)+ b = _channel("b", sendable=True)+ c = _channel("c", sendable=True)+ guild = _guild(system_channel=sys_ch, text_channels=[a, b, c])++ assert pick_intro_channel(guild) is b+++def test_returns_none_when_nothing_sendable():+ sys_ch = _channel("system", sendable=False)+ a = _channel("a", sendable=False)+ guild = _guild(system_channel=sys_ch, text_channels=[a])++ assert pick_intro_channel(guild) is None+++def test_no_system_channel_uses_first_sendable_text_channel():+ a = _channel("a", sendable=True)+ guild = _guild(system_channel=None, text_channels=[a])++ assert pick_intro_channel(guild) is a+++def test_no_member_returns_none():+ guild = _guild(system_channel=None, text_channels=[], has_me=False)++ assert pick_intro_channel(guild) is None+++def test_intro_message_mentions_setup():+ msg = intro_message()+ assert "/setup" in msg+ assert "AutoPilot" in msg