Исправление: обрезка embed-полей до лимитов Discord (description 4096, field 1024)

This commit is contained in:
deadzilla 2026-07-07 22:47:56 +05:00
parent 5a2f1630f6
commit 07af928654
3 changed files with 32 additions and 5 deletions

View File

@ -1,7 +1,13 @@
import logging
import discord
from discord.ext import commands
from utils.news import fetch_rss, format_articles, RSS_URL_ARTICLES, RSS_URL_POSTS
from utils.news import (
fetch_rss,
format_articles,
RSS_URL_ARTICLES,
RSS_URL_POSTS,
truncate_embed_field,
)
logger = logging.getLogger(__name__)
@ -36,7 +42,7 @@ class News(commands.Cog):
embed.add_field(
name="Статьи",
value="\n".join(articles_text),
value=truncate_embed_field("\n".join(articles_text)),
inline=False,
)
@ -53,7 +59,7 @@ class News(commands.Cog):
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/")
embed.add_field(
name="Новости",
value="\n".join(posts_text),
value=truncate_embed_field("\n".join(posts_text)),
inline=False,
)
else:

View File

@ -15,7 +15,13 @@ from utils.pogoda import (
fetch_weather,
format_weather_for_embed,
)
from utils.news import fetch_rss, format_articles, RSS_URL_ARTICLES, RSS_URL_POSTS
from utils.news import (
fetch_rss,
format_articles,
RSS_URL_ARTICLES,
RSS_URL_POSTS,
truncate_embed_text,
)
from utils.cat import fetch_cat
logger = logging.getLogger(__name__)
@ -106,7 +112,8 @@ async def run_morning(bot: "commands.Bot", channel: discord.TextChannel):
"Проверьте доступность API и повторите попытку позже."
]
embed.description = "\n".join(description_lines)
description = "\n".join(description_lines)
embed.description = truncate_embed_text(description)
await channel.send(embed=embed)
logger.info("✅ Утренний дайджест отправлен в #%s", channel.name)

View File

@ -93,6 +93,20 @@ def truncate_title(title, max_len=60):
return title
def truncate_embed_text(text: str, max_len: int = 4096) -> str:
"""Обрезать текст для embed.description (лимит Discord: 4096 символов)."""
if len(text) <= max_len:
return text
return text[:max_len - 3] + "..."
def truncate_embed_field(text: str, max_len: int = 1024) -> str:
"""Обрезать текст для embed field value (лимит Discord: 1024 символа)."""
if len(text) <= max_len:
return text
return text[:max_len - 3] + "..."
def format_articles(articles, title, link):
"""Сформировать список строк для вывода статей/постов."""
lines = [f"**{title}**\n<{link}>"]