fix(discord): auto-convert markdown tables to bullet groups

Discord does not render GFM pipe tables — raw pipe characters display
as garbage text. format_message now rewrites tables into bold-heading +
bullet groups using the shared helpers.

Fixes #21168

Co-authored-by: Yashiel Sookdeo <yashiel@skyner.co.za>
This commit is contained in:
Yashiel Sookdeo 2026-06-13 21:15:54 +02:00 committed by kshitij
parent 70c834a740
commit cf7bf5bdc9
2 changed files with 65 additions and 5 deletions

View file

@ -3411,13 +3411,15 @@ class DiscordAdapter(BasePlatformAdapter):
print(f"[{self.name}] Updated DISCORD_ALLOWED_USERS with {resolved_count} resolved ID(s)")
def format_message(self, content: str) -> str:
"""
Format message for Discord.
"""Format message for Discord.
Discord uses its own markdown variant.
Converts GFM markdown tables to bullet-list groups since Discord
does not render pipe tables natively.
"""
# Discord markdown is fairly standard, no special escaping needed
return content
if not content:
return content
from gateway.platforms.helpers import convert_table_to_bullets
return convert_table_to_bullets(content)
async def _run_simple_slash(
self,

View file

@ -0,0 +1,58 @@
"""Discord format_message: tables converted to bullet groups."""
import types
import sys
def _make_discord_adapter():
"""Construct a DiscordAdapter with discord.py stubbed out."""
fake_discord = types.ModuleType("discord")
fake_discord.Intents = type("Intents", (), {"default": classmethod(lambda cls: cls())})
fake_discord.Message = object
fake_ext = types.ModuleType("discord.ext")
fake_commands = types.ModuleType("discord.ext.commands")
fake_ext.commands = fake_commands
fake_discord.ext = fake_ext
sys.modules.setdefault("discord", fake_discord)
sys.modules.setdefault("discord.ext", fake_ext)
sys.modules.setdefault("discord.ext.commands", fake_commands)
from plugins.platforms.discord.adapter import DiscordAdapter
adapter = object.__new__(DiscordAdapter)
return adapter
class TestDiscordFormatMessage:
def test_table_converted_to_bullets(self):
adapter = _make_discord_adapter()
text = (
"Results:\n\n"
"| Name | Score |\n"
"|------|-------|\n"
"| Alice | 95 |\n"
"| Bob | 80 |\n"
"\nDone."
)
out = adapter.format_message(text)
assert "**Alice**" in out
assert "• Score: 95" in out
assert "**Bob**" in out
assert "• Score: 80" in out
assert out.startswith("Results:")
assert out.rstrip().endswith("Done.")
assert "|---" not in out
def test_plain_text_unchanged(self):
adapter = _make_discord_adapter()
text = "Hello world, no tables here."
assert adapter.format_message(text) == text
def test_code_block_table_unchanged(self):
adapter = _make_discord_adapter()
text = "```\n| a | b |\n|---|---|\n| 1 | 2 |\n```"
assert adapter.format_message(text) == text
def test_empty_string(self):
adapter = _make_discord_adapter()
assert adapter.format_message("") == ""