Implement same day birthday checking, along with custom dm message

This commit is contained in:
Sydney 2020-02-16 21:58:23 -05:00
parent f454fbc37e
commit c2e2606587

View file

@ -54,7 +54,6 @@ class Birthdays(Cog):
"The channel for announcing birthdays on **{g}** has been set to: **{c}**." "The channel for announcing birthdays on **{g}** has been set to: **{c}**."
) )
BDAY_REMOVED = _(":put_litter_in_its_place: Your birthday has been removed.") BDAY_REMOVED = _(":put_litter_in_its_place: Your birthday has been removed.")
BDAY_DM_DEFAULT = _(":tada: Aurelia wishes you a very happy birthday! :tada:")
def __init__(self, bot): def __init__(self, bot):
super().__init__() super().__init__()
@ -64,7 +63,9 @@ class Birthdays(Cog):
self.config = Config.get_conf(self, identifier=unique_id) self.config = Config.get_conf(self, identifier=unique_id)
self.config.init_custom(self.DATE_GROUP, 1) self.config.init_custom(self.DATE_GROUP, 1)
self.config.init_custom(self.GUILD_DATE_GROUP, 2) self.config.init_custom(self.GUILD_DATE_GROUP, 2)
self.config.register_guild(channel=None, role=None, yesterdays=[]) self.config.register_guild(
channel=None, role=None, dmmessage=":tada: Aurelia wishes you a very happy birthday! :tada:", yesterdays=[]
)
self.bday_loop = asyncio.create_task(self.initialise()) self.bday_loop = asyncio.create_task(self.initialise())
asyncio.create_task(self.check_breaking_change()) asyncio.create_task(self.check_breaking_change())
@ -112,47 +113,47 @@ class Birthdays(Cog):
year = None year = None
birthday = self.parse_date(date) birthday = self.parse_date(date)
today = datetime.datetime.utcnow().date() today = datetime.datetime.utcnow().date()
current_birthday = await self.get_birthday(ctx.guild.id, author.id)
# An Invalid date was entered. # An Invalid date was entered.
if birthday is None: if birthday is None:
print(self.BDAY_INVALID()) print(self.BDAY_INVALID())
await ctx.send(self.BDAY_INVALID()) await ctx.send(self.BDAY_INVALID())
# TODO: Properly implement a check to read the config to see if today's date is the date already set. return
# else if birthday.toordinal() == today.toordinal(): if today.year != birthday.year:
# await ctx.send("Your birthday is already set to {g} {c}!".format(birthday.strftime("%B"), birthday.strftime("%d").lstrip("0")))) if birthday.year > today.year:
# return await ctx.send("You weren't born in the future, silly!")
else: return
if today.year != birthday.year: if birthday.year < (today.year - 100):
if birthday.year > today.year: await ctx.send("No way you're that old, silly!")
await ctx.send("You weren't born in the future, silly!") return
return year = birthday.year
if birthday.year < (today.year - 100): birthday = datetime.date(1, birthday.month, birthday.day)
await ctx.send("No way you're that old, silly!") if current_birthday != None and birthday.toordinal() == current_birthday.toordinal():
return await ctx.send("Your birthday is already set to {}!".format(self.get_human_birthday(birthday)))
year = birthday.year return
birthday = datetime.date(1, birthday.month, birthday.day) await self.remove_user_bday(message.guild.id, author.id)
await self.remove_user_bday(message.guild.id, author.id) await self.get_date_config(message.guild.id, birthday.toordinal()).get_attr(author.id).set(year)
await self.get_date_config(message.guild.id, birthday.toordinal()).get_attr(author.id).set(year) await ctx.send(self.BDAY_SET(self.get_human_birthday(birthday)))
bday_month_str = birthday.strftime("%B") # Check if today is their birthday
bday_day_str = birthday.strftime("%d").lstrip("0") today_ordinal = today.replace(year=1).toordinal()
await ctx.send(self.BDAY_SET(bday_month_str + " " + bday_day_str)) birthday_ordinal = birthday.replace(year=1).toordinal()
# Check if today is their birthday if today_ordinal == birthday_ordinal:
if today.replace(year=1).toordinal() == birthday.replace(year=1).toordinal(): await self.handle_bday(author.id, year)
await self.handle_bday(self, author.id, year)
@bday_set.command(name="message") @bday_set.command(name="dmmessage")
async def bday_set_message(self, ctx: Context, *, bday_message: str = ""): @checks.mod_or_permissions(manage_roles=True)
"""Sets your birthday message. async def bday_set_dmmessage(self, ctx: Context, *, bday_message: str = ""):
"""Sets the birthday message for DMs."""
It can be any message that you want! This message will be sent
via Direct Message. Set to nothing to clear. Emotes from other
servers are not supported!"""
message = ctx.message message = ctx.message
author = message.author author = message.author
# TODO: implement saving to config.
if bday_message == "": if bday_message == "":
await ctx.send("Your birthday message is now set to the default message.") await self.config.guild(ctx.guild).dmmessage.set(":tada: Aurelia wishes you a very happy birthday! :tada:")
await ctx.send(
"Birthday DM message set to (default): :tada: Aurelia wishes you a very happy birthday! :tada:"
)
else: else:
await ctx.send("Birthday message set to: " + str(bday_message)) await self.config.guild(ctx.guild).dmmessage.set(bday_message)
await ctx.send("Birthday DM message set to: " + str(bday_message))
@bday_set.command(name="channel") @bday_set.command(name="channel")
@checks.mod_or_permissions(manage_roles=True) @checks.mod_or_permissions(manage_roles=True)
@ -239,12 +240,8 @@ class Birthdays(Cog):
channel = guild.get_channel(guild_config.get("channel")) channel = guild.get_channel(guild_config.get("channel"))
if channel is not None: if channel is not None:
await channel.send(embed=embed) await channel.send(embed=embed)
# TODO: Actually get the custom message from config. message = guild_config.get("dmmessage")
custom_message = None await member.send(message)
if custom_message is None:
await member.send(self.BDAY_DM())
else:
await member.send(custom_message)
async def clean_bdays(self): async def clean_bdays(self):
birthdays = await self.get_all_date_configs() birthdays = await self.get_all_date_configs()
@ -293,6 +290,17 @@ class Birthdays(Cog):
pass pass
return result return result
def get_human_birthday(self, birthday: datetime):
return str(birthday.strftime("%B")) + " " + str(birthday.strftime("%d").lstrip("0"))
async def get_birthday(self, guild_id: int, user_id: int):
birthdays = await self.get_guild_date_configs(guild_id)
for bday_ordinal, bdays in birthdays.items():
for user_id_config, year in bdays.items():
if int(user_id_config) == user_id:
return datetime.datetime.fromordinal(int(bday_ordinal))
return None
async def check_breaking_change(self): async def check_breaking_change(self):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
previous = await self.config.custom(self.DATE_GROUP).all() previous = await self.config.custom(self.DATE_GROUP).all()