1
0
Fork 0
mirror of synced 2024-05-19 20:12:30 +12:00

Update to conform to new pendulum versions

This commit is contained in:
phxntxm 2018-10-04 12:38:38 -05:00
parent 757b49a3eb
commit 7f05307b78

View file

@ -2,6 +2,7 @@ import discord
import pendulum import pendulum
import asyncio import asyncio
import traceback import traceback
import re
from pendulum.parsing.exceptions import ParserError from pendulum.parsing.exceptions import ParserError
from discord.ext import commands from discord.ext import commands
@ -36,6 +37,48 @@ def sort_birthdays(bds):
return later_bds + previous_bds return later_bds + previous_bds
def parse_string(date):
year = pendulum.now().year
month = None
day = None
month_map = {
"january": 1,
"jan": 1,
"february": 2,
"feb": 2,
"march": 3,
"mar": 3,
"april": 4,
"apr": 4,
"may": 5,
"june": 6,
"jun": 6,
"july": 7,
"jul": 7,
"august": 8,
"aug": 8,
"september": 9,
"sep": 9,
"october": 10,
"oct": 10,
"november": 11,
"nov": 11,
"december": 12,
"dec": 12,
}
num_re = re.compile("^(\d+)[a-z]*$")
for part in [x.lower() for x in date.split()]:
match = num_re.match(part)
if match:
day = int(match.group(1))
elif part in month_map:
month = month_map.get(part)
if month and day:
return pendulum.date(year, month, day)
class Birthday: class Birthday:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@ -144,20 +187,22 @@ class Birthday:
EXAMPLE: !birthday add December 1st EXAMPLE: !birthday add December 1st
RESULT: I now know your birthday is December 1st""" RESULT: I now know your birthday is December 1st"""
try: if len(date.split()) != 2:
# Try parsing the date from what was given
date = pendulum.parse(date)
# We'll save in a specific way so that it can be parsed how we want, so do this
date = date.format("%B %-d")
except (ValueError, ParserError):
await ctx.send("Please provide date in a valid format, such as December 1st!") await ctx.send("Please provide date in a valid format, such as December 1st!")
else: return
entry = {
'member_id': str(ctx.message.author.id), date = parse_string(date)
'birthday': date if date is None:
} await ctx.send("Please provide date in a valid format, such as December 1st!")
await self.bot.db.save('birthdays', entry) return
await ctx.send("I have just saved your birthday as {}".format(date))
date = date.strftime("%B %-d")
entry = {
'member_id': str(ctx.message.author.id),
'birthday': date
}
await self.bot.db.save('birthdays', entry)
await ctx.send("I have just saved your birthday as {}".format(date))
@birthday.command(name='remove') @birthday.command(name='remove')
@utils.can_run(send_messages=True) @utils.can_run(send_messages=True)