Skip to content

Commit

Permalink
[Weather] 1.5.1 Include wind gusts if available
Browse files Browse the repository at this point in the history
- Add all weather info to list markdown to include wind gusts underneath but part of the wind line.
- Add support to run the commands in channels without embed links permission.
  - In cases where the bot cannot embed links the forecast options will only show 3 forecasts instead of 5 due to restrictions on characters the bot can send.
  • Loading branch information
TrustyJAID committed Aug 23, 2024
1 parent 6b50c5f commit 87db81c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TrustyJAID's Cogs for [Red-DiscordBot](https://github.com/Cog-Creators/Red-Disc
| Turbo | 1.0.0 | <details><summary>Relive that 90's computer feel with turbo mode on any command!</summary>Add turbo mode to all your commands!</details> | TrustyJAID |
| Tweets | 3.0.1 | <details><summary>Cog for getting info from Twitter</summary>Gets the latest Tweet from twitter accounts and posts them in the specified channels</details> | palmtree5 and TrustyJAID |
| Twitch | 1.4.0 | <details><summary>Get basic twitch account information.</summary>Get notified of new twitch followers and get basic profile info.</details> | TrustyJAID |
| Weather | 1.5.0 | <details><summary>Show the current weather in specified locations!</summary>Check the current weather in many cities around the world including in Kelvin.</details> | TrustyJAID |
| Weather | 1.5.1 | <details><summary>Show the current weather in specified locations!</summary>Check the current weather in many cities around the world including in Kelvin.</details> | TrustyJAID |
| Welcome | 2.5.3 | <details><summary>Welcome new users to the server</summary>Welcome new users to the server or say goodbye when they leave.</details> | irdumb and TrustyJAID |

Any questions you can find [TrustyBot](https://discordapp.com/api/oauth2/authorize?client_id=268562382173765643&permissions=2146958583&scope=bot) and myself over on [my server](https://discord.gg/wVVrqej) or on the [Redbot Cog Support server](https://discord.gg/GET4DVk).
Expand Down
55 changes: 43 additions & 12 deletions weather/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,22 +344,29 @@ def __str__(self):
sunrise_ts = f"<t:{self.sunrise}:t>" if self.sunrise else _("No Data")
sunset_ts = f"<t:{self.sunset}:t>" if self.sunset else _("No Data")
windspeed = str(self.wind_speed) + " " + self.units.get().speed
wind_gust = ""
cloudiness_emoji = WEATHER_EMOJIS[get_cloud_num(self.clouds or 0)]
if self.wind_gust:
wind_gust = (
_(" - \N{WIND BLOWING FACE}\N{VARIATION SELECTOR-16} **Wind Gusts**: ")
+ f"{self.wind_gust} {self.units.get().speed}\n"
)
ret = _(
"{weather_emoji} **Weather**: {weather}\n"
"\N{FACE WITH COLD SWEAT} **Humidity**: {humidity}%\n"
"\N{DASH SYMBOL} **Wind Speed**: {wind_speed} {direction}\n"
"\N{THERMOMETER} **Temperature**: {temp}\n"
"{cloudiness_emoji} **Cloudiness**: {clouds}%\n"
"\N{SUNRISE OVER MOUNTAINS} **Sunrise**: {sunrise_ts}\n"
"\N{SUNSET OVER BUILDINGS} **Sunset**: {sunset_ts}\n"
"\N{BLACK SUN WITH RAYS}\N{VARIATION SELECTOR-16} **UV Index**: {uvi}\n"
"\N{BALLOON} **Atmospheric Pressure**: {pressure} hPa\n"
"- {weather_emoji} **Weather**: {weather}\n"
"- \N{FACE WITH COLD SWEAT} **Humidity**: {humidity}%\n"
"- \N{DASH SYMBOL} **Wind Speed**: {wind_speed} {direction}\n{wind_gust}"
"- \N{THERMOMETER} **Temperature**: {temp}\n"
"- {cloudiness_emoji} **Cloudiness**: {clouds}%\n"
"- \N{SUNRISE OVER MOUNTAINS} **Sunrise**: {sunrise_ts}\n"
"- \N{SUNSET OVER BUILDINGS} **Sunset**: {sunset_ts}\n"
"- \N{BLACK SUN WITH RAYS}\N{VARIATION SELECTOR-16} **UV Index**: {uvi}\n"
"- \N{BALLOON} **Atmospheric Pressure**: {pressure} hPa\n"
).format(
weather_emoji="".join(i.emoji for i in self.weather),
weather=humanize_list([i.description for i in self.weather]),
humidity=self.humidity,
wind_speed=windspeed,
wind_gust=wind_gust,
direction=self.wind_dir,
temp=self.temp,
cloudiness_emoji=cloudiness_emoji,
Expand All @@ -370,15 +377,15 @@ def __str__(self):
pressure=self.pressure,
)
if self.visibility:
ret += _("\N{EYEGLASSES} **Visibility**: {visibility} m\n").format(
ret += _("- \N{EYEGLASSES} **Visibility**: {visibility} m\n").format(
visibility=self.visibility
)
if self.rain:
ret += _("\N{CLOUD WITH RAIN}\N{VARIATION SELECTOR-16} **Rain**: {rain}\n").format(
ret += _("- \N{CLOUD WITH RAIN}\N{VARIATION SELECTOR-16} **Rain**: {rain}\n").format(
rain=str(self.rain)
)
if self.snow:
ret += _("\N{CLOUD WITH SNOW}\N{VARIATION SELECTOR-16} **Snow**: {snow}\n").format(
ret += _("- \N{CLOUD WITH SNOW}\N{VARIATION SELECTOR-16} **Snow**: {snow}\n").format(
snow=str(self.snow)
)
return ret
Expand Down Expand Up @@ -617,6 +624,30 @@ async def get(
raise APIError(data["message"])
return cls.from_json(data, units, name, state, country)

def text(
self, include_forecast: Optional[bool] = None, include_hourly: Optional[bool] = None
) -> str:
if self.state:
location = f"{self.name}, {self.state}, {self.country}"
else:
location = f"{self.name}, {self.country}"
msg = _("Weather for {location}").format(location=location) + "\n"
if not include_forecast and not include_hourly:
if self.daily and self.daily[0].summary:
msg += self.daily[0].summary + "\n"
msg += _("Current Weather") + f" (<t:{self.current.dt}:R>)\n{self.current}\n"
elif include_forecast:
for day in self.daily[:3]:
msg += f"<t:{day.dt}:D>\n{day}\n"
if include_hourly:
for hour in self.hourly[:3]:
msg += f"<t:{hour.dt}:t> (<t:{hour.dt}:R>)\n{hour}\n"
alerts = "\n".join(str(a) for a in self.alerts)
if alerts:
msg += alerts + "\n"
msg += _("-# Powered by <https://openweathermap.org>")
return msg

def embed(
self, include_forecast: Optional[bool] = None, include_hourly: Optional[bool] = None
) -> discord.Embed:
Expand Down
6 changes: 4 additions & 2 deletions weather/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ async def format_page(self, view: BaseMenu, page: Geocoding):
if self._last_coords is None:
self._last_coords = page
self._last_we = we
em = we.embed(include_forecast=self.forecast, include_hourly=self.hourly)
return em
if await view.ctx.embed_requested():
return we.embed(include_forecast=self.forecast, include_hourly=self.hourly)
else:
return we.text(include_forecast=self.forecast, include_hourly=self.hourly)


class StopButton(discord.ui.Button):
Expand Down
7 changes: 1 addition & 6 deletions weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Weather(commands.Cog):
"""Get weather data from https://openweathermap.org"""

__author__ = ["TrustyJAID"]
__version__ = "1.5.0"
__version__ = "1.5.1"

def __init__(self, bot):
self.bot = bot
Expand Down Expand Up @@ -94,7 +94,6 @@ async def get_appid(self, ctx: commands.Context) -> Optional[str]:
forecast="Whether or not to include the 5 day forecast information",
units="The units to display, standard is kelvin",
)
@commands.bot_has_permissions(embed_links=True)
async def weather(
self,
ctx: commands.Context,
Expand Down Expand Up @@ -132,13 +131,11 @@ async def weather(
).start(ctx=ctx)

@weather.command(name="zip")
@commands.bot_has_permissions(embed_links=True)
@discord.app_commands.describe(
zipcode="zip/postal code, country code format",
forecast="Whether or not to include the 5 day forecast information",
units="The units to display, standard is kelvin",
)
@commands.bot_has_permissions(embed_links=True)
async def weather_by_zip(
self,
ctx: commands.Context,
Expand Down Expand Up @@ -173,14 +170,12 @@ async def weather_by_zip(
).start(ctx=ctx)

@weather.command(name="coords", aliases=["co", "coordinates"])
@commands.bot_has_permissions(embed_links=True)
@discord.app_commands.describe(
lat="The latitude",
lon="The longitude",
forecast="Whether or not to include the 5 day forecast information",
units="The units to display, standard is kelvin",
)
@commands.bot_has_permissions(embed_links=True)
async def weather_by_coordinates(
self,
ctx: commands.Context,
Expand Down

0 comments on commit 87db81c

Please sign in to comment.