Adding a weather function, written entirely by AI

This commit is contained in:
Luke Robles 2025-02-26 12:27:51 -08:00
parent 8aa69114b2
commit 58161a1bea
2 changed files with 92 additions and 0 deletions

0
.gitea/FUNDING.yml Normal file → Executable file
View File

View File

@ -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",