From 8d8fd230c0f944b02d3f943f2f40cc4a9886b6c3 Mon Sep 17 00:00:00 2001 From: LMzK <73669620+L-MzK@users.noreply.github.com> Date: Mon, 30 Aug 2021 18:49:08 +0200 Subject: [PATCH 1/2] many commits such wow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - cogs, root.py, env.template und README um NEWS ergänzt - env.template rollen und channels alphabetisch geordnet --- .env.template | 16 ++++++++------- README.md | 1 + cogs/news.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ root.py | 3 ++- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 cogs/news.py diff --git a/.env.template b/.env.template index 102f548..5b012a9 100644 --- a/.env.template +++ b/.env.template @@ -4,17 +4,19 @@ DISCORD_GUILD=<ID of Guild, this Bot should be used at> DISCORD_ACTIVITY=<What should be shown, Bot is playing right now> # IDs -DISCORD_MOD_ROLE=<ID of Mod Role> DISCORD_ADMIN_ROLE=<ID of Admin Role> -DISCORD_POLL_SUGG_CHANNEL=<ID of Channel, where poll suggestions are posted> -DISCORD_WELCOME_CHANNEL=<ID of welcome channel> -DISCORD_WELCOME_MSG=<ID of welcome message> +DISCORD_CALMDOWN_ROLE=<ID of calmdown role> +DISCORD_MOD_ROLE=<ID of Mod Role> +DISCORD_FEUNEWS_ROLE=<ID of FEU news role> DISCORD_BOTUEBUNGSPLATZ_CHANNEL=<ID of channel for bot experiments> +DISCORD_NEWS_CHANNEL=<ID of news channel> DISCORD_OFFTOPIC_CHANNEL=<ID of channel for greeting & everydays subjects and questions> -DISCORD_SUPPORT_CHANNEL=<ID of channel where modmail & user news should be forwarded> -DISCORD_CALMDOWN_ROLE=<ID of calmdown role> +DISCORD_POLL_SUGG_CHANNEL=<ID of Channel, where poll suggestions are posted> DISCORD_ROLE_CHANNEL=<ID of channel for attribution of server roles> DISCORD_ROLE_MSG=<ID of role assignment message> +DISCORD_SUPPORT_CHANNEL=<ID of channel where modmail & user news should be forwarded> +DISCORD_WELCOME_CHANNEL=<ID of welcome channel> +DISCORD_WELCOME_MSG=<ID of welcome message> # JSON Files DISCORD_CALMDOWN_FILE=<File name for calmdowns JSON file> @@ -25,4 +27,4 @@ DISCORD_TIMER_FILE=<File name for running timers JSON file> DISCORD_APPOINTMENTS_FILE=<File name for running appointments JSON file> # Misc -DISCORD_DATE_TIME_FORMAT=<Date and time format used for commands like %d.%m.%Y %H:%M> +DISCORD_DATE_TIME_FORMAT=<Date and time format used for commands like %d.%m.%Y %H:%M> \ No newline at end of file diff --git a/README.md b/README.md index 7aa38a1..0953dff 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ * [Kalenderfunktion](https://github.com/FU-Hagen-Discord/root/tree/master/cogs/appointments.py) * [Umfragefunktion](https://github.com/FU-Hagen-Discord/root/tree/master/cogs/polls.py) * Sammlung Nützlicher [Links](https://github.com/FU-Hagen-Discord/root/tree/master/cogs/links.py) in den Channels + * [News der Universität](https://github.com/FU-Hagen-Discord/root-bot/tree/master/cogs/news.py) werden in dem News-Channel weitergeleitet und verlinkt * [Text-Commands](https://github.com/FU-Hagen-Discord/root/tree/master/cogs/text_commands.py) diff --git a/cogs/news.py b/cogs/news.py new file mode 100644 index 0000000..ab53ff1 --- /dev/null +++ b/cogs/news.py @@ -0,0 +1,55 @@ +import json +import os + +from aiohttp import ClientSession +from bs4 import BeautifulSoup +from discord.ext import commands, tasks + + +class News(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.channel_id = int(os.getenv("DISCORD_NEWS_CHANNEL")) + self.news_role = int(os.getenv("DISCORD_FEUNEWS_ROLE")) + self.url = "https://www.fernuni-hagen.de/universitaet/aktuelles/index.shtml" + self.news = {} + self.load_news() + self.news_loop.start() + + def load_news(self): + news_file = open("data/news.json", mode="r") + self.news = json.load(news_file) + + def save_news(self): + news_file = open("data/news.json", mode="w") + json.dump(self.news, news_file) + + @tasks.loop(hours=1) + async def news_loop(self): + async with ClientSession() as session: + async with session.get(self.url) as r: + if r.status == 200: + content = await r.read() + soup = BeautifulSoup(content, "html.parser") + channel = await self.bot.fetch_channel(self.channel_id) + + for news in soup.find("ul", attrs={"class": "fu-link-list"}).find_all("li"): + date = news.span.text + title = str(news.a.text) + link = news.a['href'] + + if link[0] == "/": + link = f"https://www.fernuni-hagen.de" + link + + if not self.news.get(link): + await channel.send( + f":loudspeaker: <@&{self.news_role}> Meldung aus der FernUni vom {date} :loudspeaker: \n{title} \n{link}") + self.news[link] = date + else: + prev_date = self.news[link] + if date != prev_date: + await channel.send( + f":loudspeaker: <@&{self.news_role}> Meldung aus der FernUni vom {date} :loudspeaker: \n{title} \n{link}") + self.news[link] = date + + self.save_news() \ No newline at end of file diff --git a/root.py b/root.py index d9c5cc1..164051d 100644 --- a/root.py +++ b/root.py @@ -5,7 +5,7 @@ from discord.ext import commands from dislash import * from dotenv import load_dotenv -from cogs import appointments, calmdown, help, links, polls, roles, support, text_commands, timer, welcome +from cogs import appointments, calmdown, help, links, news, polls, roles, support, text_commands, timer, welcome # .env file is necessary in the same directory, that contains several strings. load_dotenv() @@ -21,6 +21,7 @@ bot.add_cog(appointments.Appointments(bot)) bot.add_cog(calmdown.Calmdown(bot)) bot.add_cog(help.Help(bot)) bot.add_cog(links.Links(bot)) +bot.add_cog(news.News(bot)) bot.add_cog(polls.Polls(bot)) bot.add_cog(roles.Roles(bot)) bot.add_cog(support.Support(bot)) -- GitLab From 58bf36f15fa16369edd91b50641a2ec348ed3df7 Mon Sep 17 00:00:00 2001 From: L-MzK <73669620+L-MzK@users.noreply.github.com> Date: Tue, 31 Aug 2021 15:39:26 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Greeting=20channel=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sonst Konflikt mit Master und das wollen wir nicht. --- .env.template | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.env.template b/.env.template index 5b012a9..20559ac 100644 --- a/.env.template +++ b/.env.template @@ -6,9 +6,10 @@ DISCORD_ACTIVITY=<What should be shown, Bot is playing right now> # IDs DISCORD_ADMIN_ROLE=<ID of Admin Role> DISCORD_CALMDOWN_ROLE=<ID of calmdown role> -DISCORD_MOD_ROLE=<ID of Mod Role> DISCORD_FEUNEWS_ROLE=<ID of FEU news role> +DISCORD_MOD_ROLE=<ID of Mod Role> DISCORD_BOTUEBUNGSPLATZ_CHANNEL=<ID of channel for bot experiments> +DISCORD_GREETING_CHANNEL=<ID of channel where users will be greeted when they join> DISCORD_NEWS_CHANNEL=<ID of news channel> DISCORD_OFFTOPIC_CHANNEL=<ID of channel for greeting & everydays subjects and questions> DISCORD_POLL_SUGG_CHANNEL=<ID of Channel, where poll suggestions are posted> @@ -27,4 +28,4 @@ DISCORD_TIMER_FILE=<File name for running timers JSON file> DISCORD_APPOINTMENTS_FILE=<File name for running appointments JSON file> # Misc -DISCORD_DATE_TIME_FORMAT=<Date and time format used for commands like %d.%m.%Y %H:%M> \ No newline at end of file +DISCORD_DATE_TIME_FORMAT=<Date and time format used for commands like %d.%m.%Y %H:%M> -- GitLab