Skip to content

Instantly share code, notes, and snippets.

@Kanin
Created January 15, 2022 12:21
Show Gist options
  • Save Kanin/f4b493bae225e8bad481cee972357e0b to your computer and use it in GitHub Desktop.
Save Kanin/f4b493bae225e8bad481cee972357e0b to your computer and use it in GitHub Desktop.
@tasks.loop(hours=1)
async def birthday_handler(self): # sourcery no-metrics
now = datetime.utcnow()
if self.day != now.day:
self.day = now.day
self.first = True
data = await self.bot.pool.fetch(
"SELECT * FROM birthdays WHERE extract(month from birthday) = $1 AND extract(day from birthday) = $2",
now.month,
now.day
)
if not data:
if (now.year % 400 == 0) or (now.year % 100 != 0) and (now.year % 4 == 0): # Check if it's a leap year
return
if now.day == 28 and now.month == 2: # We're going to celebrate on the 28th
data = await self.bot.pool.fetch(
"SELECT * FROM birthdays WHERE extract(month from birthday) = 2 AND extract(day from birthday) = 29"
)
if not data:
return
guild: discord.Guild = self.bot.get_guild(694641646780022818)
channel: discord.TextChannel = guild.get_channel(931864362682024006)
age18_20: discord.Role = guild.get_role(929838790506332181)
age21_25: discord.Role = guild.get_role(929838791336792154)
age26_30: discord.Role = guild.get_role(929838792188239892)
age31_40: discord.Role = guild.get_role(929838793056481330)
age41: discord.Role = guild.get_role(929838793895321610)
announcements = []
unbans = 0
left_birthdays = 0
for birthday in data:
age = int((datetime.now().replace(tzinfo=timezone.utc) - birthday["birthday"]).days / 365.25)
if age == birthday["age"]:
continue
if age < 18:
return
if birthday["age"] < 18:
unbans += 1
user: discord.User = self.bot.get_user(birthday["user_id"])
await guild.unban(user, reason="They are now 18!")
member: discord.Member = guild.get_member(birthday["user_id"])
if member:
if 21 <= age <= 25 and age21_25 not in member.roles:
await member.remove_roles(age18_20)
await member.add_roles(age21_25)
elif 26 <= age <= 30 and age26_30 not in member.roles:
await member.remove_roles(age21_25)
await member.add_roles(age26_30)
elif 31 <= age <= 40 and age31_40 not in member.roles:
await member.remove_roles(age26_30)
await member.add_roles(age31_40)
elif age >= 41 and age41 not in member.roles:
await member.remove_roles(age31_40)
await member.add_roles(age41)
if birthday["announce"] is True:
if self.first is True:
announcements.append(member)
else:
em = discord.Embed(colour=0x912252)
em.set_author(name="Another birthday!")
em.description = f"Everyone wish {member.mention} a happy birthday!"
await channel.send(embed=em)
else:
left_birthdays += 1
await self.bot.pool.execute("UPDATE birthdays SET age=$1 WHERE user_id=$2", age, birthday["user_id"])
if announcements:
em = discord.Embed(colour=0x912252)
if len(announcements) == 1:
em.set_author(name="We have a birthday today!")
else:
em.set_author(name="We have some birthdays today!")
em.set_footer(text="Everyone wish them a happy birthday!")
em.description = "\n".join(x.mention for x in announcements)
em.description += f"\n\nIn addition I have unbanned {unbans} people for turning 18 today" \
f" and {left_birthdays} people that have left are celebrating birthdays today!"
await channel.send(embed=em)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment