diff options
author | Thomas Voss <57815710+Mango0x45@users.noreply.github.com> | 2021-01-03 23:54:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-03 23:54:51 +0000 |
commit | b324ac424dd1e31654c14b832fb791119c1934eb (patch) | |
tree | f4f6bda57732ab9537a87018d18caf2cb3062bfd | |
parent | debe83ad9d540927f92783a8032caf99fe4c5443 (diff) | |
parent | 29c57f70097717c95d5e1656937701725e73a45a (diff) | |
download | steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.tar steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.tar.gz steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.tar.bz2 steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.tar.lz steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.tar.xz steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.tar.zst steve-bot-b324ac424dd1e31654c14b832fb791119c1934eb.zip |
Merge pull request #37 from 0ceanlight/master
Fair.
-rwxr-xr-x | cogs/utils.py | 97 | ||||
-rw-r--r-- | fair.json | 14 |
2 files changed, 109 insertions, 2 deletions
diff --git a/cogs/utils.py b/cogs/utils.py index 30a2c19..67a29ee 100755 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -5,6 +5,7 @@ import json import subprocess 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 @@ -382,12 +383,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 |