Skip to content

Commit

Permalink
Added Birthday feature
Browse files Browse the repository at this point in the history
  • Loading branch information
LuminatiHD committed Jun 21, 2022
1 parent 32d2891 commit 12b77d0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions Items/searchItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Buttons
from Items.editItem import editItem
import FuncLibrary
import birthdays as bday

Itemfile = "ItemFiles.db"
Alltables = "items"
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)

Expand Down
32 changes: 32 additions & 0 deletions Items/specialcmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion Mensa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
73 changes: 73 additions & 0 deletions birthdays.py
Original file line number Diff line number Diff line change
@@ -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 ""
14 changes: 14 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 12b77d0

Please sign in to comment.