From 7ef50546a8938612ca8f3e208cabf6d9eac8017c Mon Sep 17 00:00:00 2001 From: 0ceanlight Date: Sun, 3 Jan 2021 17:33:27 -0600 Subject: added fair day && streak tracking, function to set timezone --- cogs/utils.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- fair.json | 14 +++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 fair.json diff --git a/cogs/utils.py b/cogs/utils.py index 2cb9105..5d347d5 100755 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -4,6 +4,7 @@ import functools import json from collections import namedtuple from datetime import timedelta +from pytz import timezone, exceptions # forgot to import this and ended up looking mentally unstable # troll literally pointed out atleast 4 things I did wrong in 3 lines of code from random import choice, randint @@ -380,12 +381,104 @@ class Utils(commands.Cog): for word in badWords: if word in message.content.lower().replace(" ", ""): + # get fair object + with open("fair.json", "r") as f: + fair = json.load(f) + + + # if fairer's ID in fair.json + if (userId := str(message.author.id)) in fair: + # TODO: use timezones (get this time based on timezones to fair.json - default to GMT) + tz = fair[userId]["timezone"] + today = str(datetime.datetime.now(timezone(tz)).date()) + yesterday = str(datetime.datetime.now(timezone(tz)).date() - timedelta(1)) + + # if date in json != current date + if (date := fair[userId]["date"]) != today: + # increment fair day + fairDay = fair[userId]["day"] + 1 + + fairStreak = fair[userId]["streak"] + # if the user faired yesterday + if yesterday == date: + fairStreak = fair[userId]["streak"] + 1 + else: + fairStreak = 1 + await message.channel.send("streak lost. :sad:") + + # only send && update if user is fairing for the first time today + fair[userId] = {"day": fairDay, "streak": fairStreak, "date": today, "timezone": tz} + + fairInfo = f"day {fair[userId]['day']}, streak {fair[userId]['streak']}" + await message.channel.send(fairInfo) + + # new user - not in fair.json + else: + # default to GMT + tz = "Europe/London" + today = str(datetime.datetime.now(timezone(tz)).date()) + fair[userId] = {"day": 1, "streak": 1, "date": today, "timezone": tz} + + fairInfo = f"day {fair[userId]['day']}, streak {fair[userId]['streak']}" + await message.channel.send(fairInfo) + + # overwrite with new fair object + with open("fair.json", "w") as f: + json.dump(fair, f, indent = 4) + count += 1 - fair = "Fair " * count + fairMsg = "Fair " * count try: - await message.channel.send(fair) + await message.channel.send(fairMsg) except UnboundLocalError: pass + + + # 4 minute cooldown + # should probably be longer - we can't have these kids cheating! + @commands.cooldown(1, 24, commands.BucketType.guild) + @commands.command() + async def timezone(self, ctx, timeZone): + """set timezone for fair days/streaks""" + + # get fair object + with open("fair.json", "r") as f: + fair = json.load(f) + + # if this user has faired before + if (userId := str(ctx.author.id)) not in fair: + # new user + await ctx.send("try saying 'fair' first") + return + + try: + # let users timezone = input timezone (string version so as to please json) + # use timezone() simply to see if it's valid + tz = str(timezone(timeZone)) + + except exceptions.UnknownTimeZoneError: + await ctx.send("That's not a valid timezone. You can look them up at https://kevinnovak.github.io/Time-Zone-Picker/") + return + + # set user's timezone to (verified) input zone + fair[userId]["timezone"] = tz + + # overwrite with new fair object + with open("fair.json", "w") as f: + json.dump(fair, f, indent = 4) + + await ctx.send(f"{discord.utils.escape_mentions(ctx.message.author.display_name)} your timezone has been set to {timeZone}") + + + # TODO (this also needs a better name) + # @commands.cooldown(1, 20, commands.BucketType.guild) + # @commands.command() + # async def fairleaderboard(self, ctx, timeZone): + # """show fair leaderboard, ordered by streak, then days?""" + # # get fair object + # with open("fair.json", "r") as f: + # fair = json.load(f) + @commands.cooldown(1, 60, commands.BucketType.member) @commands.command() diff --git a/fair.json b/fair.json new file mode 100644 index 0000000..3814d20 --- /dev/null +++ b/fair.json @@ -0,0 +1,14 @@ +{ + "exampleUserId": { + "day": "total days user said 'fair'", + "streak": "fair streak", + "date": "date 'fair' was said last", + "timezone": "Defaults to Europe/London" + }, + "615658069132836865": { + "day": -64, + "streak": 2, + "date": "2021-01-03", + "timezone": "Pacific/Honolulu" + } +} \ No newline at end of file -- cgit v1.2.3