Birthday
This commit is contained in:
commit
a63d8d058c
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.env
|
||||||
|
.venv
|
||||||
|
**/fregbot-birthday.log
|
||||||
|
.idea/**
|
||||||
14
pyproject.toml
Normal file
14
pyproject.toml
Normal file
@ -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"
|
||||||
2
src/.gitignore
vendored
Normal file
2
src/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.egg-info
|
||||||
|
__pycache__
|
||||||
83
src/birthday.py
Normal file
83
src/birthday.py
Normal file
@ -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()
|
||||||
16
src/main.py
Normal file
16
src/main.py
Normal file
@ -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/
|
||||||
Loading…
Reference in New Issue
Block a user