commit a63d8d058c4fcd4ff82c190739e6d93b0cbf624c Author: Paul Glaß Date: Mon Dec 2 21:09:10 2024 +0100 Birthday diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b19a8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.venv +**/fregbot-birthday.log +.idea/** \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a7314a0 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[build-system] +requires = ["setuptools>=61.0", "setuptools-scm"] +build-backend = "setuptools.build_meta" + +[project] +name = "fregbot" +dynamic = ["version"] +dependencies = [ + "discord.py", + "pydantic", +] + +[project.scripts] +fregbot-birthday = "birthday:main" \ No newline at end of file diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..5a25821 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +*.egg-info +__pycache__ \ No newline at end of file diff --git a/src/birthday.py b/src/birthday.py new file mode 100644 index 0000000..00cb642 --- /dev/null +++ b/src/birthday.py @@ -0,0 +1,83 @@ +import datetime +import json +import logging +import os +from pathlib import Path + +import discord +from discord import Client, Member, TextChannel +from pydantic import BaseModel + + +class ChannelConfig(BaseModel): + server_id: int + channel_id: int + + +class BirthdayHaver(BaseModel): + name: str = None + user_id: int + date: str + + +class BirthdayConfig(BaseModel): + channel: ChannelConfig + birthdays: list[BirthdayHaver] + + +class BirthdayClient(Client): + birthdays: dict[tuple[Member, str], datetime.date] = {} + config_file: Path = None + channel: TextChannel = None + + async def on_ready(self): + logging.info(f'Logged in as {self.user}') + try: + self.channel = self.parse_birthdays() + logging.info(f'Birthdays loaded') + for (member, name), birthday in self.birthdays.items(): + if datetime.date.today() == birthday: + await self.tell_birthday(member, name) + except Exception as e: + logging.error(f'Error: {e}') + finally: + await self.close() + + async def tell_birthday(self, member: Member, name: str) -> None: + logging.info(f'Gratuliere {member.display_name}') + await self.channel.send(f'Heute hat {name} ({member.mention}) Geburtstag! Seid nett :birthday: :tada:') + + def parse_birthdays(self) -> TextChannel: + with open(self.config_file) as fh: + config = BirthdayConfig.model_validate_json(fh.read()) + guild = self.get_guild(config.channel.server_id) + channel = discord.utils.get(guild.channels, id=config.channel.channel_id) + for birthday_haver in config.birthdays: + member = discord.utils.get(guild.members, id=birthday_haver.user_id) + birthday = datetime.datetime.strptime(birthday_haver.date, '%d.%m.') + self.birthdays[member, birthday_haver.name] = datetime.date(datetime.date.today().year, birthday.month, + birthday.day) + return channel + + +def main() -> None: + intents = discord.Intents.default() + intents.message_content = intents.members = intents.messages = True + + log_handler = logging.FileHandler(filename='fregbot-birthday.log', encoding='utf-8', mode='w') + + client = BirthdayClient(intents=intents) + client.config_file = Path(os.getenv('FREG_BIRTHDAY_CONFIG') or '') + if not client.config_file.is_file(): + logging.error(f'{client.config_file} ist leider keine Datei :s, setz mal FREG_BIRTHDAY_CONFIG') + exit(2) + + token = os.getenv('FREG_TOKEN') + if not token: + logging.error('Kein Token gefunden. Kann nicht starten. Umgebungsvariable FREG_TOKEN muss gesetzt sein') + exit(1) + client.run(token, log_handler=log_handler) + + +if __name__ == '__main__': + main() diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..305c3f5 --- /dev/null +++ b/src/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Umschalt+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Strg+8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/