Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
raccube committed Aug 16, 2024
1 parent 9dfa4cc commit b90a267
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ python-dotenv
discord.py
feedparser
beautifulsoup4
types-beautifulsoup4
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ sgmllib3k==1.0.0
# via feedparser
soupsieve==2.5
# via beautifulsoup4
types-beautifulsoup4==4.12.0.20240511
# via -r requirements.in
types-html5lib==1.1.11.20240806
# via types-beautifulsoup4
yarl==1.9.4
# via aiohttp
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ strict_equality = True

scripts_are_modules = True
warn_unused_configs = True

[mypy-feedparser.*]
ignore_missing_imports = True
24 changes: 12 additions & 12 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

import discord
from discord import app_commands
from discord.ext import tasks

from src.rss import check_posts
from src.constants import (
SPECIAL_ROLE,
VERIFIED_ROLE,
CHANNEL_PREFIX,
VOLUNTEER_ROLE,
FEED_CHANNEL_NAME,
FEED_CHECK_INTERVAL,
ANNOUNCE_CHANNEL_NAME,
WELCOME_CATEGORY_NAME,
PASSWORDS_CHANNEL_NAME,
FEED_CHECK_INTERVAL,
)
from src.commands.join import join
from src.commands.team import (
Expand All @@ -27,10 +30,6 @@
create_team_channel,
)

from discord.ext import tasks

from src.rss import check_posts


class BotClient(discord.Client):
logger: logging.Logger
Expand All @@ -41,6 +40,7 @@ class BotClient(discord.Client):
welcome_category: discord.CategoryChannel
announce_channel: discord.TextChannel
passwords_channel: discord.TextChannel
feed_channel: discord.TextChannel

def __init__(
self,
Expand All @@ -54,7 +54,7 @@ def __init__(
self.tree = app_commands.CommandTree(self)
guild_id = os.getenv('DISCORD_GUILD_ID')
if guild_id is None or not guild_id.isnumeric():
logger.error("Invalid guild ID")
self.logger.error("Invalid guild ID")
exit(1)
self.guild = discord.Object(id=int(guild_id))
team = Team()
Expand All @@ -70,8 +70,6 @@ async def setup_hook(self) -> None:
# This copies the global commands over to your guild.
self.tree.copy_global_to(guild=self.guild)
await self.tree.sync(guild=self.guild)

async def setup_hook(self) -> None:
self.check_for_new_blog_posts.start()

async def on_ready(self) -> None:
Expand All @@ -88,6 +86,7 @@ async def on_ready(self) -> None:
welcome_category = discord.utils.get(guild.categories, name=WELCOME_CATEGORY_NAME)
announce_channel = discord.utils.get(guild.text_channels, name=ANNOUNCE_CHANNEL_NAME)
passwords_channel = discord.utils.get(guild.text_channels, name=PASSWORDS_CHANNEL_NAME)
feed_channel = discord.utils.get(guild.text_channels, name=FEED_CHANNEL_NAME)

if (
verified_role is None
Expand All @@ -96,6 +95,7 @@ async def on_ready(self) -> None:
or welcome_category is None
or announce_channel is None
or passwords_channel is None
or feed_channel is None
):
logging.error("Roles and channels are not set up")
exit(1)
Expand All @@ -106,6 +106,7 @@ async def on_ready(self) -> None:
self.welcome_category = welcome_category
self.announce_channel = announce_channel
self.passwords_channel = passwords_channel
self.feed_channel = feed_channel

async def on_member_join(self, member: discord.Member) -> None:
name = member.display_name
Expand Down Expand Up @@ -143,15 +144,14 @@ async def on_member_remove(self, member: discord.Member) -> None:
self.logger.info(f"Deleted channel '{channel.name}', because it has no users.")

@tasks.loop(seconds=FEED_CHECK_INTERVAL)
async def check_for_new_blog_posts(self):
async def check_for_new_blog_posts(self) -> None:
self.logger.info("Checking for new blog posts")
await check_posts(self.get_guild(int(os.getenv('DISCORD_GUILD_ID'))))
await check_posts(self.feed_channel)

@check_for_new_blog_posts.before_loop
async def before_check_for_new_blog_posts(self):
async def before_check_for_new_blog_posts(self) -> None:
await self.wait_until_ready()


async def load_passwords(self) -> AsyncGenerator[Tuple[str, str], None]:
"""
Returns a mapping from role name to the password for that role.
Expand Down
11 changes: 7 additions & 4 deletions src/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bs4 import BeautifulSoup
from feedparser import FeedParserDict

from src.constants import FEED_URL, FEED_CHANNEL_NAME
from src.constants import FEED_URL


def get_seen_posts() -> List[str]:
Expand All @@ -22,9 +22,8 @@ def add_seen_post(post_id: str) -> None:
f.write(post_id + '\n')


async def check_posts(guild: discord.Guild) -> None:
async def check_posts(channel: discord.TextChannel) -> None:
feed = feedparser.parse(FEED_URL)
channel = discord.utils.get(guild.channels, name=FEED_CHANNEL_NAME)
post = feed.entries[0]

if post.id + "\n" not in get_seen_posts():
Expand All @@ -34,12 +33,16 @@ async def check_posts(guild: discord.Guild) -> None:

def create_embed(post: FeedParserDict) -> discord.Embed:
soup = BeautifulSoup(post.content[0].value, 'html.parser')
text = ""

if soup.p:
text = soup.p.text

embed = discord.Embed(
title=post.title,
type="article",
url=post.link,
description=soup.p.text,
description=text,
)

if len(post.media_thumbnail) > 0:
Expand Down

0 comments on commit b90a267

Please sign in to comment.