refactor(news): Embed → plain text для команды !nw
This commit is contained in:
parent
8d346943da
commit
7cef202c8f
@ -1,12 +1,11 @@
|
||||
import logging
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from utils.news import (
|
||||
fetch_rss,
|
||||
format_articles,
|
||||
RSS_URL_ARTICLES,
|
||||
RSS_URL_POSTS,
|
||||
truncate_embed_field,
|
||||
truncate_message,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -39,46 +38,26 @@ class News(commands.Cog):
|
||||
|
||||
posts = await fetch_rss(RSS_URL_POSTS)
|
||||
|
||||
embed = discord.Embed(
|
||||
title="Новости AI с Habr",
|
||||
colour=discord.Color.orange(),
|
||||
)
|
||||
|
||||
embed.add_field(
|
||||
name="Статьи",
|
||||
value=truncate_embed_field("\n".join(articles_text)),
|
||||
inline=False,
|
||||
)
|
||||
parts = ["\n".join(articles_text)]
|
||||
|
||||
if posts is None:
|
||||
logger.warning(
|
||||
"%s: !nw — не удалось получить посты (API вернул None)", ctx.author
|
||||
)
|
||||
embed.add_field(
|
||||
name="Новости",
|
||||
value="Не удалось получить новости.",
|
||||
inline=False,
|
||||
)
|
||||
parts.append("\nНе удалось получить новости.")
|
||||
elif posts:
|
||||
posts_text = format_articles(
|
||||
posts,
|
||||
"Лучшие новости за сутки / Искусственный интеллект / Хабr",
|
||||
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/",
|
||||
)
|
||||
embed.add_field(
|
||||
name="Новости",
|
||||
value=truncate_embed_field("\n".join(posts_text)),
|
||||
inline=False,
|
||||
)
|
||||
parts.append("\n" + "\n".join(posts_text))
|
||||
else:
|
||||
logger.info("%s: !nw — постов нет в RSS", ctx.author)
|
||||
embed.add_field(
|
||||
name="Новости",
|
||||
value="Новостей пока нет.",
|
||||
inline=False,
|
||||
)
|
||||
parts.append("\nНовостей пока нет.")
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
message = truncate_message("\n".join(parts))
|
||||
await ctx.send(message)
|
||||
logger.info(
|
||||
"%s: !nw выполнена (статей: %d, постов: %d)",
|
||||
ctx.author,
|
||||
|
||||
@ -114,6 +114,13 @@ def truncate_embed_field(text: str, max_len: int = 1024) -> str:
|
||||
return text[: max_len - 3] + "..."
|
||||
|
||||
|
||||
def truncate_message(text: str, max_len: int = 2000) -> str:
|
||||
"""Обрезать текст для plain text сообщения Discord (лимит: 2000 символов)."""
|
||||
if len(text) <= max_len:
|
||||
return text
|
||||
return text[: max_len - 3] + "..."
|
||||
|
||||
|
||||
def format_articles(articles: list[dict], title: str, link: str) -> list[str]:
|
||||
"""Сформировать список строк для вывода статей/постов."""
|
||||
lines = [f"**{title}**\n<{link}>"]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user