Compare commits
No commits in common. "99572fd97a5719a9a6a58c5da215502801e81a37" and "c71cb1b4ab1edd2b67b59ad671e00be141c148b2" have entirely different histories.
99572fd97a
...
c71cb1b4ab
5 changed files with 1 additions and 106 deletions
|
@ -23,7 +23,6 @@ cogs_list = [
|
||||||
'roles',
|
'roles',
|
||||||
'messages',
|
'messages',
|
||||||
'ksp',
|
'ksp',
|
||||||
'news',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
for cog in cogs_list:
|
for cog in cogs_list:
|
||||||
|
|
|
@ -1,96 +0,0 @@
|
||||||
from datetime import datetime
|
|
||||||
import discord
|
|
||||||
from discord.ext import commands
|
|
||||||
from discord.utils import get
|
|
||||||
from markdownify import markdownify
|
|
||||||
import re
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
|
|
||||||
from hrochobot.utils.ksp_utils import ksp_feed
|
|
||||||
import hrochobot.utils.data as data
|
|
||||||
|
|
||||||
NEWS_JSON = "news"
|
|
||||||
|
|
||||||
def guess_color(title):
|
|
||||||
"""
|
|
||||||
Automagically guess color of given post.
|
|
||||||
Not always reliable as all things automagic.
|
|
||||||
"""
|
|
||||||
def contains(*regexes):
|
|
||||||
return any(re.search(regex, title) for regex in regexes)
|
|
||||||
|
|
||||||
if contains(r"(\d+)-Z(\d+)", "začátečnic", "KSP-Z"):
|
|
||||||
return discord.Color.green()
|
|
||||||
elif contains(r"(\d+)-(\d+)", "seriál", "série", "KSP-H"):
|
|
||||||
return discord.Color.blue()
|
|
||||||
else:
|
|
||||||
return discord.Color.dark_purple()
|
|
||||||
|
|
||||||
def format_entry(entry, author=None):
|
|
||||||
content = "\n\n".join(map(lambda x: x.replace('\n', ' '), entry.summary.split("\n\n")))
|
|
||||||
embed = discord.Embed(
|
|
||||||
title=entry.title,
|
|
||||||
url=entry.link,
|
|
||||||
description=markdownify(content, strip=["img"]),
|
|
||||||
color=guess_color(entry.title),
|
|
||||||
)
|
|
||||||
|
|
||||||
soup = BeautifulSoup(content, 'html.parser')
|
|
||||||
img = soup.find('img')
|
|
||||||
if img:
|
|
||||||
embed.set_image(url=img['src'])
|
|
||||||
|
|
||||||
if author:
|
|
||||||
embed.set_author(name=author)
|
|
||||||
|
|
||||||
embed.set_thumbnail(url="https://ksp.mff.cuni.cz/img/hippo_head.png")
|
|
||||||
|
|
||||||
date = datetime.fromisoformat(entry.published)
|
|
||||||
embed.set_footer(text=date.strftime("%-d. %-m. %Y"))
|
|
||||||
|
|
||||||
return embed
|
|
||||||
|
|
||||||
async def post_news(bot, guild, entry_id):
|
|
||||||
news_json = data.load_guild_data(guild.id, NEWS_JSON)
|
|
||||||
if "news_channel" not in news_json:
|
|
||||||
return "News channel not set."
|
|
||||||
|
|
||||||
channel = get(guild.channels, id=news_json["news_channel"])
|
|
||||||
feed = await ksp_feed()
|
|
||||||
entries_with_id = list(filter(lambda e: e.id == f"https://ksp.mff.cuni.cz/news_{entry_id}", feed.entries))
|
|
||||||
if len(entries_with_id) == 0:
|
|
||||||
return f"Entry with id ``{entry_id}`` not found."
|
|
||||||
|
|
||||||
await channel.send(embed=format_entry(entries_with_id[0], author=feed.feed.author))
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class News(commands.Cog):
|
|
||||||
def __init__(self, bot):
|
|
||||||
self.bot = bot
|
|
||||||
news = discord.SlashCommandGroup(
|
|
||||||
"news",
|
|
||||||
"Commands for management of ksp news.",
|
|
||||||
guild_only=True,
|
|
||||||
checks=[commands.has_permissions(manage_guild=True)]
|
|
||||||
)
|
|
||||||
|
|
||||||
@news.command(description="Adds a new secret role.")
|
|
||||||
@discord.option("channel", discord.TextChannel, description="Channel for sending news.")
|
|
||||||
async def set_channel(self, ctx, channel: discord.TextChannel):
|
|
||||||
news_json = data.load_guild_data(ctx.guild.id, NEWS_JSON)
|
|
||||||
news_json["news_channel"] = channel.id
|
|
||||||
data.dump_guild_data(ctx.guild.id, NEWS_JSON, news_json)
|
|
||||||
return await ctx.respond(f"News channel set to {channel.mention}.", ephemeral=True)
|
|
||||||
|
|
||||||
@news.command(description="Synchronize news feed.")
|
|
||||||
@discord.option("id", str, description="Id of entry to send.")
|
|
||||||
async def post_news(self, ctx, id: int):
|
|
||||||
err = await post_news(self.bot, ctx.guild, id)
|
|
||||||
if err:
|
|
||||||
return await ctx.respond(err, ephemeral=True)
|
|
||||||
return await ctx.respond(f"News posted.", ephemeral=True)
|
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
|
||||||
bot.add_cog(News(bot))
|
|
|
@ -1,5 +1,4 @@
|
||||||
TEMPLATES = {
|
TEMPLATES = {
|
||||||
'roles.json' : '{"secret_roles": {}}',
|
'roles.json' : '{"secret_roles": {}}',
|
||||||
'messages.json' : '{}',
|
'messages.json' : '{}',
|
||||||
'news.json' : '{}',
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import feedparser
|
|
||||||
from typing import List, Tuple, Optional
|
from typing import List, Tuple, Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import requests
|
import requests
|
||||||
|
@ -34,6 +33,3 @@ def active_deadlines() -> List[Tuple[str, datetime]]:
|
||||||
break
|
break
|
||||||
|
|
||||||
return deadlines
|
return deadlines
|
||||||
|
|
||||||
async def ksp_feed() -> feedparser.util.FeedParserDict:
|
|
||||||
return feedparser.parse(f"{KSP_URL}/ksp.feed")
|
|
||||||
|
|
|
@ -1,5 +1,2 @@
|
||||||
py-cord
|
py-cord
|
||||||
requests
|
requests
|
||||||
feedparser
|
|
||||||
markdownify
|
|
||||||
beautifulsoup4
|
|
Loading…
Reference in a new issue