diff options
author | 0ceanlight <imahacubak@gmail.com> | 2021-01-03 23:33:27 +0000 |
---|---|---|
committer | 0ceanlight <imahacubak@gmail.com> | 2021-01-03 23:33:27 +0000 |
commit | 7ef50546a8938612ca8f3e208cabf6d9eac8017c (patch) | |
tree | 5380ca6bdf5ebaf5b4a9bff4bff9ddc3a0d7ed95 /cogs/utils.py | |
parent | e9c471617708ab1c7c773590e45327bfdc03cf57 (diff) | |
download | steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.tar steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.tar.gz steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.tar.bz2 steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.tar.lz steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.tar.xz steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.tar.zst steve-bot-7ef50546a8938612ca8f3e208cabf6d9eac8017c.zip |
added fair day && streak tracking, function to set timezone
Diffstat (limited to 'cogs/utils.py')
-rwxr-xr-x | cogs/utils.py | 97 |
1 files changed, 95 insertions, 2 deletions
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() |