2020-06-27 19:30:45 +12:00
|
|
|
from redbot.core import commands, Config, checks
|
|
|
|
from redbot.core.commands import Context, Cog
|
|
|
|
from redbot.core.utils.chat_formatting import *
|
|
|
|
from redbot.core.utils.predicates import MessagePredicate
|
|
|
|
from redbot.core.utils.menus import menu, DEFAULT_CONTROLS
|
|
|
|
|
2020-02-12 19:34:06 +13:00
|
|
|
from dateutil import parser
|
2020-06-27 19:30:45 +12:00
|
|
|
import asyncio
|
2020-01-21 14:11:51 +13:00
|
|
|
import datetime
|
|
|
|
import discord
|
2021-02-06 21:40:00 +13:00
|
|
|
from typing import Literal
|
2020-06-27 19:31:16 +12:00
|
|
|
|
2021-02-06 21:42:47 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
class Birthday(commands.Cog):
|
|
|
|
"""Track birthdays, add birthday role, and annouce birthdays for users."""
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2020-02-08 11:20:10 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
self.config = Config.get_conf(self, identifier=1561656787974966131, force_registration=True)
|
2020-02-08 11:20:10 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
default_guild = {
|
|
|
|
"channel": None,
|
|
|
|
"role": None,
|
2020-06-27 19:31:16 +12:00
|
|
|
"dm_message": ":tada: Aurelia wishes you a very happy birthday! :tada:",
|
2020-06-27 19:30:45 +12:00
|
|
|
}
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:31:16 +12:00
|
|
|
default_member = {"birthday": None, "birthday_handeled": False}
|
2020-02-08 11:20:10 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
self.config.register_guild(**default_guild)
|
|
|
|
self.config.register_member(**default_member)
|
2020-02-08 11:20:10 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
self.bday_task = asyncio.create_task(self.initialise())
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
@staticmethod
|
|
|
|
def parse_date(date: str):
|
|
|
|
return parser.parse(date)
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
@staticmethod
|
|
|
|
def get_date_and_age(date: datetime):
|
|
|
|
today = datetime.datetime.utcnow()
|
|
|
|
if date.year != today.year:
|
|
|
|
age = today.year - date.year
|
|
|
|
date = date.strftime("%b %d, %Y")
|
|
|
|
else:
|
|
|
|
date = date.strftime("%b %d")
|
|
|
|
age = None
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
return date, age
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
def cog_unload(self):
|
|
|
|
self.bday_task.cancel()
|
2020-01-21 14:11:51 +13:00
|
|
|
|
|
|
|
async def initialise(self):
|
|
|
|
await self.bot.wait_until_ready()
|
2020-06-27 19:30:45 +12:00
|
|
|
while True:
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
tomorrow = (now + datetime.timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
# check bdays once a day, at utc midnight
|
|
|
|
await self.check_bdays()
|
|
|
|
await asyncio.sleep((tomorrow - now).total_seconds())
|
|
|
|
### TESTING:
|
|
|
|
# await asyncio.sleep(30)
|
|
|
|
|
|
|
|
async def check_bdays(self):
|
|
|
|
for guild in self.bot.guilds:
|
2020-10-29 21:36:43 +13:00
|
|
|
if await self.bot.cog_disabled_in_guild(self, guild):
|
|
|
|
continue
|
2020-06-27 19:30:45 +12:00
|
|
|
for member in guild.members:
|
|
|
|
await self.check_member_bday(member)
|
|
|
|
|
|
|
|
async def check_member_bday(self, member: discord.Member):
|
|
|
|
today = datetime.datetime.utcnow().date()
|
|
|
|
bday = await self.config.member(member).birthday()
|
|
|
|
try:
|
|
|
|
bday = self.parse_date(bday).date()
|
|
|
|
except:
|
|
|
|
# no bday for user
|
|
|
|
return
|
|
|
|
year = bday.year
|
|
|
|
bday = bday.replace(year=today.year)
|
|
|
|
|
|
|
|
handled = await self.config.member(member).birthday_handeled()
|
|
|
|
if bday == today:
|
|
|
|
if not handled:
|
|
|
|
# dm user
|
|
|
|
dm = await self.config.guild(member.guild).dm_message()
|
|
|
|
try:
|
|
|
|
await member.send(dm)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# send bday in channel
|
|
|
|
channel = await self.config.guild(member.guild).channel()
|
|
|
|
channel = self.bot.get_channel(channel)
|
|
|
|
if channel:
|
|
|
|
embed = discord.Embed(color=discord.Colour.gold())
|
|
|
|
if year != today.year:
|
|
|
|
age = today.year - year
|
|
|
|
embed.description = f"{member.mention} is now **{age} years old!**"
|
|
|
|
else:
|
|
|
|
embed.description = f"Happy Birthday to {member.mention}!"
|
|
|
|
try:
|
2020-12-28 18:44:17 +13:00
|
|
|
await channel.send(embed=embed, allowed_mentions=discord.AllowedMentions.all())
|
2020-06-27 19:30:45 +12:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# add role, if available
|
|
|
|
role = await self.config.guild(member.guild).role()
|
|
|
|
role = member.guild.get_role(role)
|
|
|
|
if role:
|
|
|
|
try:
|
|
|
|
await member.add_roles(role, reason="Birthday cog")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
await self.config.member(member).birthday_handeled.set(True)
|
|
|
|
else:
|
|
|
|
if handled:
|
|
|
|
# remove bday role
|
|
|
|
role = await self.config.guild(member.guild).role()
|
|
|
|
role = member.guild.get_role(role)
|
|
|
|
if role:
|
|
|
|
try:
|
|
|
|
await member.remove_roles(role, reason="Birthday cog")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# unhandled their birthday, cya next year!
|
|
|
|
await self.config.member(member).birthday_handeled.set(False)
|
|
|
|
|
2020-08-26 15:38:05 +12:00
|
|
|
# @commands.command()
|
|
|
|
# async def test(self, ctx, *, member: discord.Member):
|
2020-08-25 14:45:28 +12:00
|
|
|
# await self.check_bdays()
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
@commands.group(name="bdayset")
|
2020-01-21 14:11:51 +13:00
|
|
|
@commands.guild_only()
|
2020-06-27 19:30:45 +12:00
|
|
|
@checks.admin_or_permissions(administrator=True)
|
|
|
|
async def bdayset(self, ctx):
|
|
|
|
""" Manage birthday cog settings """
|
2020-01-21 14:11:51 +13:00
|
|
|
pass
|
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
@bdayset.command(name="dmmessage")
|
|
|
|
async def bdayset_dm_message(self, ctx, *, message: str = None):
|
2020-09-07 10:23:33 +12:00
|
|
|
"""Set message DMed to users when its their birthday!
|
|
|
|
Leave empty to get/clear current message
|
2020-02-18 18:35:31 +13:00
|
|
|
"""
|
2020-06-27 19:30:45 +12:00
|
|
|
if not message:
|
|
|
|
current = await self.config.guild(ctx.guild).dm_message()
|
|
|
|
await ctx.send(f"Current message is `{current}`\nDo you want to reset it to default?")
|
|
|
|
pred = MessagePredicate.yes_or_no(ctx)
|
|
|
|
try:
|
|
|
|
await self.bot.wait_for("message", check=pred, timeout=30)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
await ctx.send("Took too long.")
|
2020-02-17 15:58:23 +13:00
|
|
|
return
|
2020-06-27 19:30:45 +12:00
|
|
|
if pred.result:
|
|
|
|
await self.config.guild(ctx.guild).dm_message.clear()
|
|
|
|
await ctx.send("DM message reset to default.")
|
|
|
|
else:
|
|
|
|
await ctx.send("Nothing changed.")
|
2020-02-17 15:58:23 +13:00
|
|
|
return
|
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
await self.config.guild(ctx.guild).dm_message.set(message)
|
|
|
|
await ctx.tick()
|
2020-02-18 18:35:31 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
@bdayset.command(name="channel")
|
|
|
|
async def bdayset_channel(self, ctx, *, channel: discord.TextChannel = None):
|
|
|
|
""" Set channel to send birthday annoucements """
|
|
|
|
if not channel:
|
|
|
|
await self.config.guild(ctx.guild).channel.clear()
|
|
|
|
else:
|
|
|
|
await self.config.guild(ctx.guild).channel.set(channel.id)
|
|
|
|
|
|
|
|
await ctx.tick()
|
2020-02-18 18:35:31 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
@bdayset.command(name="role")
|
|
|
|
@checks.bot_has_permissions(manage_roles=True)
|
|
|
|
async def bdayset_role(self, ctx, *, role: discord.Role = None):
|
|
|
|
""" Set role to give users on their birthday """
|
|
|
|
if not role:
|
|
|
|
await self.config.guild(ctx.guild).role.clear()
|
2020-02-17 14:31:30 +13:00
|
|
|
else:
|
2020-06-27 19:30:45 +12:00
|
|
|
await self.config.guild(ctx.guild).role.set(role.id)
|
|
|
|
|
|
|
|
await ctx.tick()
|
|
|
|
|
|
|
|
@commands.group(name="bday")
|
|
|
|
@commands.guild_only()
|
|
|
|
async def bday(self, ctx):
|
|
|
|
""" Manage your birthday """
|
|
|
|
pass
|
|
|
|
|
|
|
|
@bday.command(name="set")
|
|
|
|
async def bday_set(self, ctx, *, date: str = None):
|
2020-09-07 10:23:33 +12:00
|
|
|
"""Set your birthday. Year not required.
|
2020-06-27 19:30:45 +12:00
|
|
|
Date can be any valid date format, like:
|
|
|
|
05/20/99
|
|
|
|
05-20-99
|
|
|
|
May 5, 1999
|
|
|
|
20/05/99
|
|
|
|
05-20
|
|
|
|
etc..
|
|
|
|
"""
|
2020-06-27 19:40:31 +12:00
|
|
|
current = await self.config.member(ctx.author).birthday()
|
|
|
|
if not date and not current:
|
|
|
|
await self.bot.send_help_for(ctx, "bday set")
|
|
|
|
return
|
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
if not date:
|
|
|
|
await ctx.send("Would you like to remove your birthday?")
|
|
|
|
pred = MessagePredicate.yes_or_no(ctx)
|
|
|
|
try:
|
|
|
|
await self.bot.wait_for("message", check=pred, timeout=30)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
await ctx.send("Took too long.")
|
|
|
|
return
|
|
|
|
if pred.result:
|
|
|
|
await self.config.member(ctx.author).birthday.clear()
|
|
|
|
await ctx.tick()
|
2020-01-21 14:11:51 +13:00
|
|
|
else:
|
2020-06-27 19:30:45 +12:00
|
|
|
await ctx.send("Nothing Changed.")
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
return
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
today = datetime.datetime.utcnow()
|
|
|
|
try:
|
|
|
|
date = self.parse_date(date).date()
|
|
|
|
if date.year > today.year:
|
|
|
|
await ctx.send(error("Year is in the future!"))
|
|
|
|
return
|
|
|
|
elif date.year < (today.year - 110):
|
|
|
|
await ctx.send(error("You can't be that old..."))
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
await ctx.send(error("Invalid Date!"))
|
|
|
|
return
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
if date.year == today.year:
|
|
|
|
date = date.strftime("%m/%d")
|
|
|
|
else:
|
|
|
|
date = date.strftime("%m/%d/%Y")
|
2020-01-21 14:11:51 +13:00
|
|
|
|
2020-06-27 19:30:45 +12:00
|
|
|
await self.config.member(ctx.author).birthday.set(date)
|
|
|
|
await ctx.tick()
|
|
|
|
|
|
|
|
@bday.command(name="list")
|
|
|
|
async def bday_list(self, ctx):
|
|
|
|
""" List birthdays in the server """
|
|
|
|
embeds = []
|
|
|
|
for member in ctx.guild.members:
|
|
|
|
bday = await self.config.member(member).birthday()
|
|
|
|
if bday:
|
|
|
|
embed = discord.Embed(title=f"{member.display_name}", colour=ctx.guild.me.colour)
|
2020-08-26 15:38:05 +12:00
|
|
|
bday_datetime = self.parse_date(bday)
|
|
|
|
bday, age = self.get_date_and_age(bday_datetime)
|
2020-06-27 19:30:45 +12:00
|
|
|
embed.add_field(name="Birthday", value=bday)
|
|
|
|
if age:
|
2020-08-26 15:38:05 +12:00
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
bday_datetime = bday_datetime.replace(year=now.year)
|
|
|
|
if now > bday_datetime:
|
|
|
|
embed.add_field(name="Turned", value=age)
|
|
|
|
else:
|
|
|
|
embed.add_field(name="Turning", value=age)
|
2020-06-27 19:30:45 +12:00
|
|
|
embeds.append(embed)
|
|
|
|
|
|
|
|
for i, embed in enumerate(embeds):
|
|
|
|
embed.set_footer(text=f"Page {i+1} of {len(embeds)}")
|
|
|
|
|
|
|
|
if not embeds:
|
|
|
|
await ctx.send("No one has their birthday set in your server!")
|
|
|
|
else:
|
|
|
|
await menu(ctx, embeds, DEFAULT_CONTROLS)
|
2021-02-06 21:40:00 +13:00
|
|
|
|
|
|
|
async def red_delete_data_for_user(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
requester: Literal["discord_deleted_user", "owner", "user", "user_strict"],
|
|
|
|
user_id: int,
|
|
|
|
):
|
|
|
|
pass
|