Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(discord): answer an unenabled channel only when someone addresses the bot (#2793)

Call White5c5f7de2a707
diff --git a/modules/im/discord.py b/modules/im/discord.py
index 5b393c9a9..82d211d9e 100644
--- a/modules/im/discord.py
+++ b/modules/im/discord.py
@@ -1094,6 +1094,8 @@ class DiscordBot(BaseIMClient):
is_dm = isinstance(channel, discord.DMChannel)
referenced_anchor_base = None if is_dm else self._get_reply_anchor_base(channel_id, self._get_reference_message_id(message))
+ mentioned_bot = bool(self.client.user and message.mentions and self.client.user in message.mentions)
+
auth_result = self.check_authorization(
user_id=str(message.author.id),
channel_id=channel_id,
@@ -1102,7 +1104,12 @@ class DiscordBot(BaseIMClient):
settings_manager=self.settings_manager,
)
if not auth_result.allowed:
- await self._send_auth_denial(channel_id, str(message.author.id), auth_result)
+ # Every message in a guild channel reaches this gate, so the denial
+ # answers only someone who addressed the bot -- a mention, a reply to
+ # its message, or a command -- as on Slack, Lark and Telegram. The
+ # rest of an unenabled channel's conversation gets no reply.
+ if is_dm or mentioned_bot or referenced_anchor_base or self.parse_text_command(content):
+ await self._send_auth_denial(channel_id, str(message.author.id), auth_result)
return
# Mention logic for guild channels
@@ -1112,8 +1119,6 @@ class DiscordBot(BaseIMClient):
channel_id, global_default=self.config.require_mention
)
- mentioned_bot = bool(self.client.user and message.mentions and self.client.user in message.mentions)
-
if effective_require_mention and not is_dm:
if isinstance(channel, discord.Thread):
if not mentioned_bot and not self.is_scheduled_thread_active(channel_id, str(channel.id)):
diff --git a/tests/test_discord_reply_anchor.py b/tests/test_discord_reply_anchor.py
index b29d5da30..7bd949e52 100644
--- a/tests/test_discord_reply_anchor.py
+++ b/tests/test_discord_reply_anchor.py
@@ -242,3 +242,56 @@ class DiscordReplyAnchorTests(unittest.IsolatedAsyncioTestCase):
)
interaction.response.defer.assert_awaited_once_with(ephemeral=True)
+
+
+class DiscordUnenabledChannelHintTests(unittest.IsolatedAsyncioTestCase):
+ """Every message in a guild channel reaches auth; the 'not enabled' hint answers only one that addressed the bot."""
+
+ async def _deliver(self, *, content: str, mentions: list, is_dm: bool = False):
+ import modules.im.discord as discord_module
+
+ bot = object.__new__(DiscordBot)
+ bot.config = DiscordConfig(require_mention=True)
+ bot._controller = None
+ bot.client = SimpleNamespace(user=SimpleNamespace(id=42))
+ bot.sessions = None
+ bot.settings_manager = None
+ bot.on_message_callback = AsyncMock()
+ bot._send_auth_denial = AsyncMock()
+ denial = "unbound_dm" if is_dm else "unauthorized_channel"
+ bot.check_authorization = lambda **kwargs: AuthResult(allowed=False, denial=denial, is_dm=is_dm)
+
+ original_dm_channel = discord_module.discord.DMChannel
+
+ class _FakeDMChannel:
+ id = 321
+
+ try:
+ discord_module.discord.DMChannel = _FakeDMChannel
+ message = SimpleNamespace(
+ author=SimpleNamespace(id=456, bot=False),
+ content=content,
+ channel=_FakeDMChannel() if is_dm else SimpleNamespace(id=123),
+ guild=None if is_dm else SimpleNamespace(id=999),
+ attachments=[],
+ mentions=mentions,
+ reference=None,
+ id=888,
+ )
+ await DiscordBot._on_message_event(bot, message)
+ finally:
+ discord_module.discord.DMChannel = original_dm_channel
+
+ bot.on_message_callback.assert_not_awaited()
+ return bot._send_auth_denial.await_count
+
+ async def test_the_rest_of_an_unenabled_channels_conversation_gets_no_reply(self):
+ self.assertEqual(await self._deliver(content="lunch at noon?", mentions=[]), 0)
+ self.assertEqual(await self._deliver(content="ping <@7>", mentions=[SimpleNamespace(id=7)]), 0)
+
+ async def test_a_mention_or_a_command_gets_the_hint(self):
+ self.assertEqual(await self._deliver(content="<@42> help", mentions=[SimpleNamespace(id=42)]), 1)
+ self.assertEqual(await self._deliver(content="/settings", mentions=[]), 1)
+
+ async def test_a_direct_message_still_gets_its_hint(self):
+ self.assertEqual(await self._deliver(content="hello", mentions=[], is_dm=True), 1)