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