Исправление: обрезка embed-полей до лимитов Discord (description 4096, field 1024)
This commit is contained in:
parent
5a2f1630f6
commit
07af928654
@ -1,7 +1,13 @@
|
|||||||
import logging
|
import logging
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -36,7 +42,7 @@ class News(commands.Cog):
|
|||||||
|
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Статьи",
|
name="Статьи",
|
||||||
value="\n".join(articles_text),
|
value=truncate_embed_field("\n".join(articles_text)),
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,7 +59,7 @@ class News(commands.Cog):
|
|||||||
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/")
|
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/")
|
||||||
embed.add_field(
|
embed.add_field(
|
||||||
name="Новости",
|
name="Новости",
|
||||||
value="\n".join(posts_text),
|
value=truncate_embed_field("\n".join(posts_text)),
|
||||||
inline=False,
|
inline=False,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -15,7 +15,13 @@ from utils.pogoda import (
|
|||||||
fetch_weather,
|
fetch_weather,
|
||||||
format_weather_for_embed,
|
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
|
from utils.cat import fetch_cat
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -106,7 +112,8 @@ async def run_morning(bot: "commands.Bot", channel: discord.TextChannel):
|
|||||||
"Проверьте доступность API и повторите попытку позже."
|
"Проверьте доступность API и повторите попытку позже."
|
||||||
]
|
]
|
||||||
|
|
||||||
embed.description = "\n".join(description_lines)
|
description = "\n".join(description_lines)
|
||||||
|
embed.description = truncate_embed_text(description)
|
||||||
await channel.send(embed=embed)
|
await channel.send(embed=embed)
|
||||||
logger.info("✅ Утренний дайджест отправлен в #%s", channel.name)
|
logger.info("✅ Утренний дайджест отправлен в #%s", channel.name)
|
||||||
|
|
||||||
|
|||||||
@ -93,6 +93,20 @@ def truncate_title(title, max_len=60):
|
|||||||
return title
|
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):
|
def format_articles(articles, title, link):
|
||||||
"""Сформировать список строк для вывода статей/постов."""
|
"""Сформировать список строк для вывода статей/постов."""
|
||||||
lines = [f"**{title}**\n<{link}>"]
|
lines = [f"**{title}**\n<{link}>"]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user