From 7fed9ae7159d4ba9a99f1e771a567896ee53e018 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Mon, 22 May 2023 23:34:00 +0530 Subject: [PATCH 01/29] Add weather commands to a new cog This adds the `/weather` and `/weather_set_location` commands. This also adds the weather database for storing default locations. --- cogs/weather.py | 75 +++++++++++++++++++++++++++++++++++++++++++ database/weather.json | 1 + 2 files changed, 76 insertions(+) create mode 100644 cogs/weather.py create mode 100644 database/weather.json diff --git a/cogs/weather.py b/cogs/weather.py new file mode 100644 index 00000000..2d77f5bb --- /dev/null +++ b/cogs/weather.py @@ -0,0 +1,75 @@ +# Imports +import discord +import json +import requests +from discord import ApplicationContext, option +from discord.ext import commands + +# Variables +api_key = "21cab08deb7b27f4c2b55f3e2df28ea4" +with open("database/weather.json", 'r', encoding="utf-8") as f: user_db = json.load(f) + +# Functions +def save(): + """Dumps all cached databases to storage.""" + with open("database/weather.json", 'w+', encoding="utf-8") as f: json.dump(user_db, f, indent=4) + +# Commands +class Weather(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.slash_command( + name="weather_set_location", + description="Set your default location for the /weather command." + ) + @option(name="location", description="What location do you want to set?", type=str) + def weather_set_location(ctx: ApplicationContext, location: str): + if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None + test_ping = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content + test_ping_json = json.loads(test_ping) + if test_ping_json["cod"] == 404: return await ctx.respond(":warning: This location does not exist.", ephemeral=True) + else: + user_db[str(ctx.author.id)] = location.lower() + save() + localembed = discord.Embed(description="Your default location has been updated.") + await ctx.respond(embed=localembed) + + @commands.slash_command( + name="weather", + description="See the current weather conditions of your set location, or another location." + ) + @option(name="location", description="The location you want weather info about (leave empty for set location)", type=str, default=None) + def weather(ctx: ApplicationContext, location: str = None): + if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None + if location == None: + if user_db[str(ctx.author.id)] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) + else: location = user_db[str(ctx.author.id)] + api_request = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content + req = json.loads(api_request) + + # Stripped API request data + loc_name = req["name"] + temp = 273 - req["main"]["temp"] + temp_max = 273 - req["main"]["temp_max"] + temp_min = 273 - req["main"]["temp_min"] + humidity = req["main"]["humidity"] + sunrise = req["sys"]["sunset"] + sunset = req["sys"]["sunrise"] + rain_chance = req["rain"]["1h"] + forcast = req["weather"]["0"]["main"] + forcast_description = req["weather"]["0"]["description"] + + localembed = discord.Embed( + title=f"Weather for {loc_name}", + description=f"**{forcast}**\n{forcast_description}", + color=discord.Color.light_blue() + ) + localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") + localembed.add_field(name="Humidity", value=f"{humidity}%") + localembed.add_field(name="Chance of rain (%)", value=f"{rain_chance}%") + localembed.add_field(name="Sunrise", value=f"") + localembed.add_field(name="Sunset", value=f"") + +# Initialization +def setup(bot): bot.add_cog(Weather(bot)) diff --git a/database/weather.json b/database/weather.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/database/weather.json @@ -0,0 +1 @@ +{} From 8a09997458f36da8b50d28269e8c12036a690184 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Mon, 22 May 2023 23:47:59 +0530 Subject: [PATCH 02/29] Add new commands to commands db --- config/commands.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/config/commands.json b/config/commands.json index 4cd02517..c0799504 100644 --- a/config/commands.json +++ b/config/commands.json @@ -658,5 +658,25 @@ "usable_by": "everyone", "disabled": false, "bugged": false + }, + "weather": { + "name": "Weather", + "description": "See the current weather conditions of your set location, or another location.", + "type": "weather", + "cooldown": null, + "args": ["location (optional, configurable by /weather_set_location)"], + "usable_by": "everyone", + "disabled": false, + "bugged": false + }, + "weather_set_location": { + "name": "Weather", + "description": "See the current weather conditions of your set location, or another location.", + "type": "weather", + "cooldown": null, + "args": ["location"], + "usable_by": "everyone", + "disabled": false, + "bugged": false } } From 1d91b4c60c699bd347e3b1693fc959ebb115fe2b Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 19:26:53 +0530 Subject: [PATCH 03/29] Add `async` keyword for `weather()` and `weather_set_location()` --- cogs/weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 2d77f5bb..a01db139 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -24,7 +24,7 @@ def __init__(self, bot): description="Set your default location for the /weather command." ) @option(name="location", description="What location do you want to set?", type=str) - def weather_set_location(ctx: ApplicationContext, location: str): + async def weather_set_location(ctx: ApplicationContext, location: str): if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None test_ping = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content test_ping_json = json.loads(test_ping) @@ -40,7 +40,7 @@ def weather_set_location(ctx: ApplicationContext, location: str): description="See the current weather conditions of your set location, or another location." ) @option(name="location", description="The location you want weather info about (leave empty for set location)", type=str, default=None) - def weather(ctx: ApplicationContext, location: str = None): + async def weather(ctx: ApplicationContext, location: str = None): if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None if location == None: if user_db[str(ctx.author.id)] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) From 835371e6db6d0ab2ca794d5467008fe78d9935f2 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 19:28:12 +0530 Subject: [PATCH 04/29] Add weather cog to `active_cogs` list on bot init --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index c5a949e3..0f3ab2a1 100644 --- a/main.py +++ b/main.py @@ -296,7 +296,8 @@ async def reload(ctx: ApplicationContext, cog: str): "fun", "utils", "afk", - "osu" + "osu", + "weather" ] i = 1 cog_errors = 0 From 5bc2cdfd29ac362ee60655b93c0e1f125237d60a Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 19:36:36 +0530 Subject: [PATCH 05/29] Add `self` parameter in `weather()` and `weather_set_location()` Seriously how do I keep forgetting to do this --- cogs/weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index a01db139..d6fdf905 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -24,7 +24,7 @@ def __init__(self, bot): description="Set your default location for the /weather command." ) @option(name="location", description="What location do you want to set?", type=str) - async def weather_set_location(ctx: ApplicationContext, location: str): + async def weather_set_location(self, ctx: ApplicationContext, location: str): if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None test_ping = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content test_ping_json = json.loads(test_ping) @@ -40,7 +40,7 @@ async def weather_set_location(ctx: ApplicationContext, location: str): description="See the current weather conditions of your set location, or another location." ) @option(name="location", description="The location you want weather info about (leave empty for set location)", type=str, default=None) - async def weather(ctx: ApplicationContext, location: str = None): + async def weather(self, ctx: ApplicationContext, location: str = None): if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None if location == None: if user_db[str(ctx.author.id)] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) From b79f982cda652bf1702c4396bfc304ed8dd12d04 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 19:39:13 +0530 Subject: [PATCH 06/29] Add `respond()` to send embed after `/weather` successfully executed --- cogs/weather.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cogs/weather.py b/cogs/weather.py index d6fdf905..baf48022 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -70,6 +70,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): localembed.add_field(name="Chance of rain (%)", value=f"{rain_chance}%") localembed.add_field(name="Sunrise", value=f"") localembed.add_field(name="Sunset", value=f"") + await ctx.respond(embed=localembed) # Initialization def setup(bot): bot.add_cog(Weather(bot)) From 6faabe04b3c04733eb422a7c7c1ea9d92607cafc Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 19:51:23 +0530 Subject: [PATCH 07/29] Convert `author.id` type to `str` for db key-matching --- cogs/weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index baf48022..4dbbfefb 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -25,7 +25,7 @@ def __init__(self, bot): ) @option(name="location", description="What location do you want to set?", type=str) async def weather_set_location(self, ctx: ApplicationContext, location: str): - if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = None test_ping = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content test_ping_json = json.loads(test_ping) if test_ping_json["cod"] == 404: return await ctx.respond(":warning: This location does not exist.", ephemeral=True) @@ -41,7 +41,7 @@ async def weather_set_location(self, ctx: ApplicationContext, location: str): ) @option(name="location", description="The location you want weather info about (leave empty for set location)", type=str, default=None) async def weather(self, ctx: ApplicationContext, location: str = None): - if ctx.author.id not in user_db: user_db[str(ctx.author.id)] = None + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = None if location == None: if user_db[str(ctx.author.id)] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) else: location = user_db[str(ctx.author.id)] From e92eab35dcb3c37d1089675e304b405d75b8f6c2 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:04:53 +0530 Subject: [PATCH 08/29] Add API request error code handling in `/weather` --- cogs/weather.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 4dbbfefb..5f62caf6 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -18,7 +18,7 @@ def save(): class Weather(commands.Cog): def __init__(self, bot): self.bot = bot - + @commands.slash_command( name="weather_set_location", description="Set your default location for the /weather command." @@ -47,7 +47,11 @@ async def weather(self, ctx: ApplicationContext, location: str = None): else: location = user_db[str(ctx.author.id)] api_request = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content req = json.loads(api_request) - + print(req) + if req["cod"] == 404: return await ctx.respond(":x: This location was not found. Check your spelling or try another location instead.", ephemeral=True) + elif req["cod"] != 200: return await ctx.respond("A slight problem occured when trying to get information. This error has been automatically reported to the devs.", ephemeral=True) + else: pass + # Stripped API request data loc_name = req["name"] temp = 273 - req["main"]["temp"] @@ -61,8 +65,8 @@ async def weather(self, ctx: ApplicationContext, location: str = None): forcast_description = req["weather"]["0"]["description"] localembed = discord.Embed( - title=f"Weather for {loc_name}", - description=f"**{forcast}**\n{forcast_description}", + title=f"Weather for {loc_name}", + description=f"**{forcast}**\n{forcast_description}", color=discord.Color.light_blue() ) localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") From 352b5025f0294dc4e31f081cc00c612f2f87f88e Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:21:51 +0530 Subject: [PATCH 09/29] Fix typo where list index for `forcast` & `forcast_description` were `str` and not `int` --- cogs/weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 5f62caf6..45ef5feb 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -61,8 +61,8 @@ async def weather(self, ctx: ApplicationContext, location: str = None): sunrise = req["sys"]["sunset"] sunset = req["sys"]["sunrise"] rain_chance = req["rain"]["1h"] - forcast = req["weather"]["0"]["main"] - forcast_description = req["weather"]["0"]["description"] + forcast = req["weather"][0]["main"] + forcast_description = req["weather"][0]["description"] localembed = discord.Embed( title=f"Weather for {loc_name}", From 8929a725b539dcb2495dfa2c5777f8b59eb0cc69 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:25:59 +0530 Subject: [PATCH 10/29] Fix arithmetic error while converting `temp`, `temp_min` and `temp_max` to Celsius --- cogs/weather.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 45ef5feb..371ca797 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -54,9 +54,9 @@ async def weather(self, ctx: ApplicationContext, location: str = None): # Stripped API request data loc_name = req["name"] - temp = 273 - req["main"]["temp"] - temp_max = 273 - req["main"]["temp_max"] - temp_min = 273 - req["main"]["temp_min"] + temp = req["main"]["temp"] - 273 + temp_max = req["main"]["temp_max"] - 273 + temp_min = req["main"]["temp_min"] - 273 humidity = req["main"]["humidity"] sunrise = req["sys"]["sunset"] sunset = req["sys"]["sunrise"] From 88517e5e00430be4d03e9d6013d3353a43f870e8 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:28:27 +0530 Subject: [PATCH 11/29] `round()` temperature values No decimal spam :( --- cogs/weather.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 371ca797..b90e1a10 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -54,9 +54,9 @@ async def weather(self, ctx: ApplicationContext, location: str = None): # Stripped API request data loc_name = req["name"] - temp = req["main"]["temp"] - 273 - temp_max = req["main"]["temp_max"] - 273 - temp_min = req["main"]["temp_min"] - 273 + temp = round(req["main"]["temp"] - 273) + temp_max = round(req["main"]["temp_max"] - 273) + temp_min = round(req["main"]["temp_min"] - 273) humidity = req["main"]["humidity"] sunrise = req["sys"]["sunset"] sunset = req["sys"]["sunrise"] From 2264285aeb7312d5a2db300d51c091cc6170ffa9 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:34:10 +0530 Subject: [PATCH 12/29] Convert `humidity` from 2-decimal float to integer Mathematically converted! --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index b90e1a10..46f3598b 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -57,7 +57,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): temp = round(req["main"]["temp"] - 273) temp_max = round(req["main"]["temp_max"] - 273) temp_min = round(req["main"]["temp_min"] - 273) - humidity = req["main"]["humidity"] + humidity = round(req["rain"]["1h"] * 100) sunrise = req["sys"]["sunset"] sunset = req["sys"]["sunrise"] rain_chance = req["rain"]["1h"] From c8e8fb83fb9d72a96016c15adf4632dfadf848f4 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:43:39 +0530 Subject: [PATCH 13/29] Remove `rain_chance` as it is a dynamic response Can't do something that doesn't exist --- cogs/weather.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 46f3598b..ddfe7a55 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -60,7 +60,6 @@ async def weather(self, ctx: ApplicationContext, location: str = None): humidity = round(req["rain"]["1h"] * 100) sunrise = req["sys"]["sunset"] sunset = req["sys"]["sunrise"] - rain_chance = req["rain"]["1h"] forcast = req["weather"][0]["main"] forcast_description = req["weather"][0]["description"] @@ -71,7 +70,6 @@ async def weather(self, ctx: ApplicationContext, location: str = None): ) localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") localembed.add_field(name="Humidity", value=f"{humidity}%") - localembed.add_field(name="Chance of rain (%)", value=f"{rain_chance}%") localembed.add_field(name="Sunrise", value=f"") localembed.add_field(name="Sunset", value=f"") await ctx.respond(embed=localembed) From 8d729bad089cc808ef2d633623938bd8868bdfc4 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:46:06 +0530 Subject: [PATCH 14/29] Change response embed color from `light_blue` to `blue` Either light blue embed color doesn't exist, or I did another typo --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index ddfe7a55..c622c74f 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -66,7 +66,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): localembed = discord.Embed( title=f"Weather for {loc_name}", description=f"**{forcast}**\n{forcast_description}", - color=discord.Color.light_blue() + color=discord.Color.blue() ) localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") localembed.add_field(name="Humidity", value=f"{humidity}%") From efbbfc9f2ef4cf737a5715b68bfb90d1d76d4ed0 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:47:15 +0530 Subject: [PATCH 15/29] Move `sunset` embed field before `sunrise` embed field --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index c622c74f..b24d8d84 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -58,8 +58,8 @@ async def weather(self, ctx: ApplicationContext, location: str = None): temp_max = round(req["main"]["temp_max"] - 273) temp_min = round(req["main"]["temp_min"] - 273) humidity = round(req["rain"]["1h"] * 100) - sunrise = req["sys"]["sunset"] sunset = req["sys"]["sunrise"] + sunrise = req["sys"]["sunset"] forcast = req["weather"][0]["main"] forcast_description = req["weather"][0]["description"] From 054765a73a89d8990e9b0dc2921f1af59f05efb8 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 20:48:32 +0530 Subject: [PATCH 16/29] Move `sunset` and `sunrise` embed fields to single line --- cogs/weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index b24d8d84..4cb7446a 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -70,8 +70,8 @@ async def weather(self, ctx: ApplicationContext, location: str = None): ) localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") localembed.add_field(name="Humidity", value=f"{humidity}%") - localembed.add_field(name="Sunrise", value=f"") - localembed.add_field(name="Sunset", value=f"") + localembed.add_field(name="Sunrise", value=f"", inline=False) + localembed.add_field(name="Sunset", value=f"", inline=True) await ctx.respond(embed=localembed) # Initialization From f49e5125197271e03b521a112b0392bb58e133dc Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 23:34:36 +0530 Subject: [PATCH 17/29] Add country flag emoji next to location name in weather embed --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index 4cb7446a..1d9ec8a5 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -64,7 +64,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): forcast_description = req["weather"][0]["description"] localembed = discord.Embed( - title=f"Weather for {loc_name}", + title=f"Weather for {loc_name} (:flag_{req['country'].lower()}:)", description=f"**{forcast}**\n{forcast_description}", color=discord.Color.blue() ) From 04fd75e7dfb9ccd0c3651f1e9f52b1ddf4e905dc Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 23:37:03 +0530 Subject: [PATCH 18/29] Add missing parent key in country data --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index 1d9ec8a5..4fd70b37 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -64,7 +64,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): forcast_description = req["weather"][0]["description"] localembed = discord.Embed( - title=f"Weather for {loc_name} (:flag_{req['country'].lower()}:)", + title=f"Weather for {loc_name} (:flag_{req['sys']['country'].lower()}:)", description=f"**{forcast}**\n{forcast_description}", color=discord.Color.blue() ) From 477e9054b392b851a94c97058ad95f3cb48a9f8e Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 23:40:00 +0530 Subject: [PATCH 19/29] Remove `()` between flag display emoji It honestly looked weirder than it should've --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index 4fd70b37..5ac470c7 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -64,7 +64,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): forcast_description = req["weather"][0]["description"] localembed = discord.Embed( - title=f"Weather for {loc_name} (:flag_{req['sys']['country'].lower()}:)", + title=f"Weather for {loc_name} :flag_{req['sys']['country'].lower()}:", description=f"**{forcast}**\n{forcast_description}", color=discord.Color.blue() ) From cdfacac6a982233e187bfc7299008ce336ca5875 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Tue, 23 May 2023 23:59:52 +0530 Subject: [PATCH 20/29] Update weather database format to support temp scale setting --- cogs/weather.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 5ac470c7..08027045 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -25,12 +25,12 @@ def __init__(self, bot): ) @option(name="location", description="What location do you want to set?", type=str) async def weather_set_location(self, ctx: ApplicationContext, location: str): - if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = None + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "celsius"} test_ping = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content test_ping_json = json.loads(test_ping) if test_ping_json["cod"] == 404: return await ctx.respond(":warning: This location does not exist.", ephemeral=True) else: - user_db[str(ctx.author.id)] = location.lower() + user_db[str(ctx.author.id)]["location"] = location.lower() save() localembed = discord.Embed(description="Your default location has been updated.") await ctx.respond(embed=localembed) @@ -41,12 +41,12 @@ async def weather_set_location(self, ctx: ApplicationContext, location: str): ) @option(name="location", description="The location you want weather info about (leave empty for set location)", type=str, default=None) async def weather(self, ctx: ApplicationContext, location: str = None): - if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = None + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "celsius"} if location == None: - if user_db[str(ctx.author.id)] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) - else: location = user_db[str(ctx.author.id)] + if user_db[str(ctx.author.id)]["location"] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) + else: location = user_db[str(ctx.author.id)]["location"] api_request = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content - req = json.loads(api_request) + req: dict = json.loads(api_request) print(req) if req["cod"] == 404: return await ctx.respond(":x: This location was not found. Check your spelling or try another location instead.", ephemeral=True) elif req["cod"] != 200: return await ctx.respond("A slight problem occured when trying to get information. This error has been automatically reported to the devs.", ephemeral=True) @@ -57,7 +57,7 @@ async def weather(self, ctx: ApplicationContext, location: str = None): temp = round(req["main"]["temp"] - 273) temp_max = round(req["main"]["temp_max"] - 273) temp_min = round(req["main"]["temp_min"] - 273) - humidity = round(req["rain"]["1h"] * 100) + humidity = req["main"]["humidity"] sunset = req["sys"]["sunrise"] sunrise = req["sys"]["sunset"] forcast = req["weather"][0]["main"] From 93a759701223c31f73b70efe765551ec5f5f8956 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 00:08:17 +0530 Subject: [PATCH 21/29] Add scale unit settings checking and conversion in `/weather` --- cogs/weather.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 08027045..37f38a2d 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -54,9 +54,18 @@ async def weather(self, ctx: ApplicationContext, location: str = None): # Stripped API request data loc_name = req["name"] - temp = round(req["main"]["temp"] - 273) - temp_max = round(req["main"]["temp_max"] - 273) - temp_min = round(req["main"]["temp_min"] - 273) + if user_db[str(ctx.author.id)]["scale"] == "celsius": + temp = round(req["main"]["temp"] - 273) + temp_max = round(req["main"]["temp_max"] - 273) + temp_min = round(req["main"]["temp_min"] - 273) + elif user_db[str(ctx.author.id)]["scale"] == "fahrenheit": + temp = round(((req["main"]["temp"] - 273) * 9/5) + 32) + temp_max = round(((req["main"]["temp_max"] - 273) * 9/5) + 32) + temp_min = round(((req["main"]["temp_min"] - 273) * 9/5) + 32) + else: + temp = round(req["main"]["temp"]) + temp_max = round(req["main"]["temp_max"]) + temp_min = round(req["main"]["temp_min"]) humidity = req["main"]["humidity"] sunset = req["sys"]["sunrise"] sunrise = req["sys"]["sunset"] @@ -68,7 +77,9 @@ async def weather(self, ctx: ApplicationContext, location: str = None): description=f"**{forcast}**\n{forcast_description}", color=discord.Color.blue() ) - localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") + if user_db[str(ctx.author.id)]["scale"] == "celsius": localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") + elif user_db[str(ctx.author.id)]["scale"] == "fahrenheit": localembed.add_field(name="Temperature", value=f"**{temp}F** (max: {temp_max}F, min: {temp_min}F)") + else: localembed.add_field(name="Temperature", value=f"**{temp}K** (max: {temp_max}K, min: {temp_min}K)") localembed.add_field(name="Humidity", value=f"{humidity}%") localembed.add_field(name="Sunrise", value=f"", inline=False) localembed.add_field(name="Sunset", value=f"", inline=True) From 6af5b0a8c874604883f7b30b08d4df809216f665 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 00:15:05 +0530 Subject: [PATCH 22/29] Add `/weather_set_scale` to allow users to set their preferred unit scale for `/weather` --- cogs/weather.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index 37f38a2d..e5dac4e3 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -32,9 +32,22 @@ async def weather_set_location(self, ctx: ApplicationContext, location: str): else: user_db[str(ctx.author.id)]["location"] = location.lower() save() - localembed = discord.Embed(description="Your default location has been updated.") + localembed = discord.Embed(description="Your default location has been updated.", discord.Color.green()) await ctx.respond(embed=localembed) + @commands.slash_command( + name="weather_set_scale", + description="Set your preferred unit scale for temperature for the /weather command." + ) + @option(name="scale", description="Which scale do you want to use?", type=str, choices=["Celsius", "Fahrenheit", "Kelvin"]) + async def weather_set_scale(self, ctx: ApplicationContext, scale: str): + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "celsius"} + if scale not in ["Celsius", "Fahrenheit", "Kelvin"]: return 1 + user_db[str(ctx.author.id)]["scale"]: scale + save() + localembed = discord.Embed(description="Your preferred unit scale has been updated.", color=discord.Color.green()) + await ctx.respond(embed=localembed) + @commands.slash_command( name="weather", description="See the current weather conditions of your set location, or another location." From 38e9c50f97ccf769d3da30ca86df0a4de076b752 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 00:16:26 +0530 Subject: [PATCH 23/29] Update unit scale capitalization --- cogs/weather.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index e5dac4e3..a682c61c 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -25,7 +25,7 @@ def __init__(self, bot): ) @option(name="location", description="What location do you want to set?", type=str) async def weather_set_location(self, ctx: ApplicationContext, location: str): - if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "celsius"} + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "Celsius"} test_ping = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}").content test_ping_json = json.loads(test_ping) if test_ping_json["cod"] == 404: return await ctx.respond(":warning: This location does not exist.", ephemeral=True) @@ -41,7 +41,7 @@ async def weather_set_location(self, ctx: ApplicationContext, location: str): ) @option(name="scale", description="Which scale do you want to use?", type=str, choices=["Celsius", "Fahrenheit", "Kelvin"]) async def weather_set_scale(self, ctx: ApplicationContext, scale: str): - if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "celsius"} + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "Celsius"} if scale not in ["Celsius", "Fahrenheit", "Kelvin"]: return 1 user_db[str(ctx.author.id)]["scale"]: scale save() @@ -54,7 +54,7 @@ async def weather_set_scale(self, ctx: ApplicationContext, scale: str): ) @option(name="location", description="The location you want weather info about (leave empty for set location)", type=str, default=None) async def weather(self, ctx: ApplicationContext, location: str = None): - if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "celsius"} + if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "Celsius"} if location == None: if user_db[str(ctx.author.id)]["location"] == None: return await ctx.respond("You do not have a default location set yet.\nEnter a location name and try again.", ephemeral=True) else: location = user_db[str(ctx.author.id)]["location"] @@ -67,11 +67,11 @@ async def weather(self, ctx: ApplicationContext, location: str = None): # Stripped API request data loc_name = req["name"] - if user_db[str(ctx.author.id)]["scale"] == "celsius": + if user_db[str(ctx.author.id)]["scale"] == "Celsius": temp = round(req["main"]["temp"] - 273) temp_max = round(req["main"]["temp_max"] - 273) temp_min = round(req["main"]["temp_min"] - 273) - elif user_db[str(ctx.author.id)]["scale"] == "fahrenheit": + elif user_db[str(ctx.author.id)]["scale"] == "Fahrenheit": temp = round(((req["main"]["temp"] - 273) * 9/5) + 32) temp_max = round(((req["main"]["temp_max"] - 273) * 9/5) + 32) temp_min = round(((req["main"]["temp_min"] - 273) * 9/5) + 32) From a99c49ab43e818bca2079649d297f383cc38d5f1 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 00:18:03 +0530 Subject: [PATCH 24/29] Fix missing argument name in embed color configuration --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index a682c61c..649637ef 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -32,7 +32,7 @@ async def weather_set_location(self, ctx: ApplicationContext, location: str): else: user_db[str(ctx.author.id)]["location"] = location.lower() save() - localembed = discord.Embed(description="Your default location has been updated.", discord.Color.green()) + localembed = discord.Embed(description="Your default location has been updated.", color=discord.Color.green()) await ctx.respond(embed=localembed) @commands.slash_command( From fd9828b1aff694c7d5939cabae0ebe287c5eed26 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 00:22:23 +0530 Subject: [PATCH 25/29] Update unit scale capitalization --- cogs/weather.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/weather.py b/cogs/weather.py index 649637ef..bb372d48 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -90,8 +90,8 @@ async def weather(self, ctx: ApplicationContext, location: str = None): description=f"**{forcast}**\n{forcast_description}", color=discord.Color.blue() ) - if user_db[str(ctx.author.id)]["scale"] == "celsius": localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") - elif user_db[str(ctx.author.id)]["scale"] == "fahrenheit": localembed.add_field(name="Temperature", value=f"**{temp}F** (max: {temp_max}F, min: {temp_min}F)") + if user_db[str(ctx.author.id)]["scale"] == "Celsius": localembed.add_field(name="Temperature", value=f"**{temp}C** (max: {temp_max}C, min: {temp_min}C)") + elif user_db[str(ctx.author.id)]["scale"] == "Fahrenheit": localembed.add_field(name="Temperature", value=f"**{temp}F** (max: {temp_max}F, min: {temp_min}F)") else: localembed.add_field(name="Temperature", value=f"**{temp}K** (max: {temp_max}K, min: {temp_min}K)") localembed.add_field(name="Humidity", value=f"{humidity}%") localembed.add_field(name="Sunrise", value=f"", inline=False) From ea582fd592f6cf81342a9484384d25229f67e35f Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 00:24:45 +0530 Subject: [PATCH 26/29] Fix a small logical error in `/weather_set_scale` --- cogs/weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/weather.py b/cogs/weather.py index bb372d48..5d05438f 100644 --- a/cogs/weather.py +++ b/cogs/weather.py @@ -43,7 +43,7 @@ async def weather_set_location(self, ctx: ApplicationContext, location: str): async def weather_set_scale(self, ctx: ApplicationContext, scale: str): if str(ctx.author.id) not in user_db: user_db[str(ctx.author.id)] = {"location": None, "scale": "Celsius"} if scale not in ["Celsius", "Fahrenheit", "Kelvin"]: return 1 - user_db[str(ctx.author.id)]["scale"]: scale + user_db[str(ctx.author.id)]["scale"] = scale save() localembed = discord.Embed(description="Your preferred unit scale has been updated.", color=discord.Color.green()) await ctx.respond(embed=localembed) From 12738d2b0c2a365c37bf9c71c9cc92e87879b497 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 09:47:36 +0530 Subject: [PATCH 27/29] Add `/weather_set_scale` to commands db --- config/commands.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/config/commands.json b/config/commands.json index c0799504..63d2fc0b 100644 --- a/config/commands.json +++ b/config/commands.json @@ -678,5 +678,15 @@ "usable_by": "everyone", "disabled": false, "bugged": false + }, + "weather_set_scale": { + "name": "Set weather temperature unit scale", + "description": "Set your preferred unit scale for temperature for the `/weather` command.", + "type": "weather", + "cooldown": null, + "args": ["location"], + "usable_by": "everyone", + "disabled": false, + "bugged": false } } From 918caea3f34572a3f5917a4a6011236d8c39559b Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 09:49:27 +0530 Subject: [PATCH 28/29] Fix name and description for `/weather_set_location` command in commands db --- config/commands.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/commands.json b/config/commands.json index 63d2fc0b..e9e85304 100644 --- a/config/commands.json +++ b/config/commands.json @@ -670,8 +670,8 @@ "bugged": false }, "weather_set_location": { - "name": "Weather", - "description": "See the current weather conditions of your set location, or another location.", + "name": "Set weather default location", + "description": "Set your default location for the `/weather` command.", "type": "weather", "cooldown": null, "args": ["location"], From 508240ef22f3482c9186d0e7660c86d6630a3c77 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Wed, 24 May 2023 09:51:24 +0530 Subject: [PATCH 29/29] Fix args in `/weather_set_scale` command in commands db --- config/commands.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/commands.json b/config/commands.json index e9e85304..ce87ffb2 100644 --- a/config/commands.json +++ b/config/commands.json @@ -684,7 +684,7 @@ "description": "Set your preferred unit scale for temperature for the `/weather` command.", "type": "weather", "cooldown": null, - "args": ["location"], + "args": ["scale"], "usable_by": "everyone", "disabled": false, "bugged": false