diff --git a/Buttons.py b/Buttons.py index 5bff51a..b8e5e10 100644 --- a/Buttons.py +++ b/Buttons.py @@ -514,7 +514,8 @@ async def callback(self, interaction: nextcord.Interaction): class ChooseDatum(nextcord.ui.View): - def __init__(self, ctx, day=None, month=None, year=None): + def __init__(self, ctx, day=None, month=None, year=None, + year_bounds:tuple[int, int] = (datetime.now().year, datetime.now().year+3)): self.exit = False self.ctx = ctx self.update = True @@ -537,7 +538,7 @@ def __init__(self, ctx, day=None, month=None, year=None): yearselect = Dropdown() yearselect.placeholder = "Jahr:" - yearselect.options = [nextcord.SelectOption(label=str(i)) for i in range(today.year, today.year+3)] + yearselect.options = [nextcord.SelectOption(label=str(i)) for i in range(*year_bounds)] yearselect.custom_id = "year" self.add_item(dayselect) diff --git a/Items/searchItem.py b/Items/searchItem.py index 9e26510..0b02f36 100644 --- a/Items/searchItem.py +++ b/Items/searchItem.py @@ -7,6 +7,7 @@ import Buttons from Items.editItem import editItem import FuncLibrary +import birthdays as bday Itemfile = "ItemFiles.db" Alltables = "items" @@ -44,9 +45,10 @@ async def todo(self, ctx: Context): # =========================================== ERSTE SEITE =========================================== if results: # aaschiinend giut ä lääri lischte aus ä boolean, ka bro buttons = Buttons.PageButtons(results, 0, ctx) - outputmsg = await ctx.channel.send(embed=FuncLibrary.layout(selection, - footer=f"Seite {1}/{int(len(results) / 5) + (len(results) % 5 > 0)}"), - view=buttons) + outputmsg = await ctx.channel.send(content = bday.notifs(ctx.author.id), + embed=FuncLibrary.layout(selection, + footer=f"Seite {1}/{int(len(results) / 5) + (len(results) % 5 > 0)}"), + view=buttons) while datetime.datetime.now() < begin + datetime.timedelta(minutes=2): results = await get_db_items(ctx, timeset) @@ -56,9 +58,10 @@ async def todo(self, ctx: Context): buttons = Buttons.PageButtons(results, currentpage, ctx) - await outputmsg.edit(embed=FuncLibrary.layout(selection, - footer=f"Seite {currentpage+1}/{int(len(results) / 5) + (len(results) % 5 > 0)}"), - view=buttons) + await outputmsg.edit(content = bday.notifs(ctx.author.id), + embed=FuncLibrary.layout(selection, + footer=f"Seite {currentpage+1}/{int(len(results) / 5) + (len(results) % 5 > 0)}"), + view=buttons) # ds wartet druf das öppis drücket wird. ds geit bim Button mitem self.stop(). Problem isch aber, # dass me dr button när nümme cha bruuche, auso muesme ä neue generiere. await buttons.wait() @@ -71,7 +74,8 @@ async def todo(self, ctx: Context): buttons = Buttons.PageButtons(results, currentpage, ctx) # es isch übersichtlecher, d message ds editiere aus se neu d schicke. - await outputmsg.edit(embed=FuncLibrary.layout(selection, + await outputmsg.edit(content = bday.notifs(ctx.author.id), + embed=FuncLibrary.layout(selection, footer=f"Seite {currentpage + 1}/{int(len(results) / 5) + (len(results) % 5 > 0)}"), view=buttons) diff --git a/Items/specialcmds.py b/Items/specialcmds.py index 9b701c0..a20fd95 100644 --- a/Items/specialcmds.py +++ b/Items/specialcmds.py @@ -110,6 +110,38 @@ async def poll(self, ctx:Context): async def despair_time(self, ctx:Context): await FuncLibrary.covid_embed(ctx.channel, 3600*24*2) + @commands.command(name="bugreport") + async def bugreport(self, ctx:Context): + github_token = "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" + user = "LuminatiHD" + pswd = "hvYqml007mI1" + repo = "Hausaufgabenbot" + + ses = requests.Session() + ses.auth = (user, pswd) + + issue = { + "title": "Test", + "body": "ignore pls", + "assignee": user, + "milestone": None, + "labels": ["bug"] + } + + issue_url = "https://api.github.com/repos/" + user + "/" + repo + "/issues" + + r = ses.post(issue_url, data=issue) + print(r) + + @commands.command(name="birthday") + async def birthday(self, ctx:Context): + await birthdays.add_birthday(self.bot, ctx) + + @commands.command(name="end_notifs") + async def end_notifs(self, ctx:Context): + birthdays.notification_off(ctx.author.id) + await ctx.message.add_reaction("✅") + @commands.command(name="!stcol") async def stcol(self, ctx:Context): liste = list(FuncLibrary.StP_colors.keys()) diff --git a/Mensa/main.py b/Mensa/main.py index 5fd280f..3dea254 100644 --- a/Mensa/main.py +++ b/Mensa/main.py @@ -3,6 +3,8 @@ from nextcord.ext import commands from nextcord.ext.commands.context import Context from datetime import date, timedelta, datetime, time +import birthdays as bday + wochentage = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"] embedcol = [0xba2929, 0xeb7c28, 0xd6aa18, 0x237a06, 0x086fcf] @@ -36,7 +38,7 @@ async def menu(self, ctx:Context): title +=f" ({item['label'].upper()})" menu.add_field(name=title, value=item["desc"]) - await ctx.channel.send(embed=menu) + await ctx.channel.send(content = bday.notifs(ctx.author.id), embed=menu) await output.delete() except IndexError: diff --git a/birthdays.py b/birthdays.py new file mode 100644 index 0000000..5069b45 --- /dev/null +++ b/birthdays.py @@ -0,0 +1,73 @@ +import nextcord +from nextcord.ext import commands +from nextcord.ext.commands.context import Context +import sqlite3 +from Buttons import ChooseDatum +from datetime import datetime, timedelta, date + + +async def add_birthday(client:nextcord.Client, ctx:Context) -> None: + db = sqlite3.connect("ItemFiles.db") + cs = db.cursor() + + # in loop cuz if the day is above 25, then the program has to recreate the view-object + # so if you choose "above 25", then the view will reset, + # but the day-dropdown will show the choices above 25, as well as an option to go back down. + bounds = (2003, 2006) + msg_view = ChooseDatum(ctx, year_bounds=bounds) + msg = await ctx.channel.send("Wann ist dein Geburtstag?", view=msg_view) + while not msg_view.over: + if msg_view.exit: + break + else: + msg_view = ChooseDatum(ctx, day=msg_view.day, month=msg_view.month, + year=msg_view.year, year_bounds=bounds) + await msg.edit(view=msg_view) + await msg_view.wait() + + # cleanup + await msg.delete() + await ctx.message.add_reaction("✅") + + # If the user did not break, then he is added to the table, as well as his next birthday + if not msg_view.exit: + # if the user has already an entry (f.e. he turned the notifications of), + # then their instance in the db gets deleted and then re-added. + if cs.execute(f"SELECT * FROM birthdays WHERE id={ctx.author.id}").fetchone(): + cs.execute(f"DELETE FROM birthdays WHERE id={ctx.author.id}") + + # the birthday of the user is added to the db + cs.execute(f"INSERT INTO birthdays VALUES ({ctx.author.id}, {int(msg_view.day)}, {int(msg_view.month)}, " + f"{int(msg_view.year)}, true)") + + db.commit() + # their subsequent birthday gets added as well + now = (datetime.utcnow() + timedelta(hours=2)).date() + b_date = date(now.year, msg_view.month, msg_view.day) + if now > b_date: + b_date.replace(year=b_date.year+1) + + fach = client.get_channel(912264818516430849).name + + cs.execute(f"INSERT INTO items VALUES ({str(b_date)}, '', ?, ?, all)", + (fach, f'Geburtstag {ctx.author.name}')) + db.commit() + db.close() + + +def notification_off(userid:int): + db = sqlite3.connect("ItemFiles.db") + cs = db.cursor() + cs.execute(f"INSERT INTO birthdays VALUES ({userid}, 0, 0, 0, false)") + db.commit() + db.close() + + +def notifs(userid:int) -> str: + db = sqlite3.connect("ItemFiles.db") + cs = db.cursor() + if not cs.execute(f"SELECT * FROM birthdays WHERE id = {userid}").fetchone(): + return "**Tipp**: gib '!birthday' ein, um deinen Geburtstag einzutragen\n" \ + "*(du kannst diese notification mit '!end_notifs' abstellen.*" + else: + return "" \ No newline at end of file diff --git a/main.py b/main.py index f6640ab..cb4226d 100644 --- a/main.py +++ b/main.py @@ -107,6 +107,20 @@ async def remind_task(): await channel.send(embed=embed) +@tasks.loop(hours = 23) +async def next_birthdays(): + for user in cs.execute("SELECT year, month, date, id FROM birthdays WHERE visible"): + if datetime.now().date == date(user[0], user[1], user[2]): + now = (datetime.utcnow() + timedelta(hours=2)).date() + b_date = date(now.year+1, user[1], user[2]) + + fach = client.get_channel(912264818516430849).name + + cs.execute(f"INSERT INTO items VALUES ({str(b_date)}, '', ?, ?, all)", + (fach, f'Geburtstag {client.get_user(user[3]).name}')) + database.commit() + + @tasks.loop(hours=1) async def news(): if (datetime.utcnow()+timedelta(hours=2)).hour == 10: