From 58161a1bea6409e5cb39abf2e9c8cadecc3c8b6e Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Wed, 26 Feb 2025 12:27:51 -0800 Subject: [PATCH] Adding a weather function, written entirely by AI --- .gitea/FUNDING.yml | 0 app/cogs/actual_utils.py | 92 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) mode change 100644 => 100755 .gitea/FUNDING.yml diff --git a/.gitea/FUNDING.yml b/.gitea/FUNDING.yml old mode 100644 new mode 100755 diff --git a/app/cogs/actual_utils.py b/app/cogs/actual_utils.py index 69df3b3e..070c85cc 100755 --- a/app/cogs/actual_utils.py +++ b/app/cogs/actual_utils.py @@ -3,6 +3,7 @@ from discord.ext import commands import discord import os import tempfile +import requests class ActualUtils(commands.Cog): @@ -108,6 +109,97 @@ class ActualUtils(commands.Cog): except Exception: return await ctx.send_followup("Sorry, I'm unable to answer that") + @commands.slash_command( + guild_ids=None, + name="weather", + description="Get current weather for a location", + ) + @option( + name="location", + required=True, + description="City name or location to get weather for", + ) + async def weather(self, ctx, location): + api_key = os.getenv("openweather_api_key") + if not api_key: + return await ctx.respond("OpenWeather API key not configured") + + await ctx.defer() + + # Weather condition to emoji mapping + weather_emojis = { + # Clear + "01d": "☀️", # clear sky (day) + "01n": "🌙", # clear sky (night) + # Clouds + "02d": "⛅", # few clouds (day) + "02n": "☁️", # few clouds (night) + "03d": "☁️", # scattered clouds + "03n": "☁️", # scattered clouds + "04d": "☁️", # broken clouds + "04n": "☁️", # broken clouds + # Rain + "09d": "🌧️", # shower rain + "09n": "🌧️", # shower rain + "10d": "🌦️", # rain (day) + "10n": "🌧️", # rain (night) + # Thunderstorm + "11d": "⛈️", # thunderstorm + "11n": "⛈️", # thunderstorm + # Snow + "13d": "❄️", # snow + "13n": "❄️", # snow + # Mist/Fog + "50d": "🌫️", # mist + "50n": "🌫️", # mist + } + + # Default emoji if condition code not found + default_emoji = "🌡️" + + try: + # Get weather data from OpenWeather API + url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=imperial" + response = requests.get(url) + data = response.json() + + if response.status_code != 200: + return await ctx.send_followup( + f"Error: {data.get('message', 'Unknown error')}" + ) + + # Extract weather information + weather_main = data["weather"][0]["main"] + weather_desc = data["weather"][0]["description"] + weather_icon = data["weather"][0]["icon"] + temp = data["main"]["temp"] + feels_like = data["main"]["feels_like"] + humidity = data["main"]["humidity"] + wind_speed = data["wind"]["speed"] + + # Get appropriate emoji + emoji = weather_emojis.get(weather_icon, default_emoji) + + # Create embed + embed = discord.Embed( + title=f"Weather in {data['name']}, {data.get('sys', {}).get('country', '')}", + description=f"{emoji} **{weather_main}** - {weather_desc}", + color=discord.Color.blue(), + ) + + embed.add_field(name="Temperature", value=f"{temp}°F", inline=True) + embed.add_field(name="Feels Like", value=f"{feels_like}°F", inline=True) + embed.add_field(name="Humidity", value=f"{humidity}%", inline=True) + embed.add_field(name="Wind Speed", value=f"{wind_speed} m/s", inline=True) + + # Add timestamp + embed.set_footer(text="Data from OpenWeather API") + + await ctx.send_followup(embed=embed) + + except Exception as e: + await ctx.send_followup(f"Error fetching weather data: {str(e)}") + @commands.slash_command( guild_ids=None, name="wifiqr",