|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import datetime |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from bot.apis.weather import DailyParameters, HourlyParameters, OpenMeteo |
| 7 | +from bot.ext import Context, Response, commands |
| 8 | +from bot.models import User |
| 9 | +from bot.utils import SessionsCaches, StringTools |
| 10 | + |
| 11 | +from .translations import Translations |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from bot.bot import Gorenmu |
| 15 | + |
| 16 | + |
| 17 | +class WeatherCmd(commands.CustomComponent): |
| 18 | + def __init__(self, bot: Gorenmu) -> None: |
| 19 | + self.bot = bot |
| 20 | + self.translations: Translations = Translations(bot) |
| 21 | + self.SessionsCaches: SessionsCaches = SessionsCaches(bot) |
| 22 | + self.StringTools: StringTools = StringTools() |
| 23 | + |
| 24 | + cooldown_rate = 3 |
| 25 | + cooldown_per = 10 |
| 26 | + cooldown_key = commands.BucketType.user |
| 27 | + |
| 28 | + async def component_command_error(self, payload: commands.CommandErrorPayload) -> bool | None: ... |
| 29 | + |
| 30 | + @commands.Component.guard() |
| 31 | + def guards_component(self, ctx: commands.Context) -> bool: # NOQA |
| 32 | + return True |
| 33 | + |
| 34 | + @commands.command(name='weather', aliases=['wt']) |
| 35 | + async def weather(self, ctx: Context, *, location: str = "") -> Response: |
| 36 | + from_chat = True |
| 37 | + location, from_user = self.StringTools.extract_and_remove_field(location, "user", False) |
| 38 | + user = None |
| 39 | + if from_user: |
| 40 | + user = await User.get_user_or_none(ctx, self.translations, name=from_user) |
| 41 | + if not user: |
| 42 | + return self.translations.Exceptions.user_not_found_name(ctx, name=from_user) |
| 43 | + if not user.city: |
| 44 | + return self.translations.Weather.user_has_no_city(ctx, from_user) |
| 45 | + from_chat = False |
| 46 | + location = user.city |
| 47 | + if ctx.user.city and not location: |
| 48 | + location = ctx.user.city |
| 49 | + from_chat = False |
| 50 | + |
| 51 | + is_private = not from_chat and (user or ctx.user).city_hidden |
| 52 | + safe_location_name = self.translations.Weather.hidden(ctx) if is_private else location |
| 53 | + if not location: |
| 54 | + return self.translations.Weather.city_not_passed(ctx) |
| 55 | + |
| 56 | + meteo = OpenMeteo(self.SessionsCaches.Weather.session) |
| 57 | + try: |
| 58 | + async with meteo as open_meteo: |
| 59 | + geocoding_result = await open_meteo.geocoding(name=location) |
| 60 | + if geocoding_result.results: |
| 61 | + city = geocoding_result.results[0] |
| 62 | + else: |
| 63 | + return self.translations.Weather.city_not_found(ctx, safe_location_name) |
| 64 | + except Exception as e: |
| 65 | + ctx.bot.log.error(e) |
| 66 | + return self.translations.Weather.city_not_found(ctx, safe_location_name) |
| 67 | + |
| 68 | + async with meteo as open_meteo: |
| 69 | + weather_obj = await open_meteo.forecast( |
| 70 | + latitude=city.latitude, |
| 71 | + longitude=city.longitude, |
| 72 | + current_weather=True, |
| 73 | + daily=[ |
| 74 | + DailyParameters.SUNRISE, |
| 75 | + DailyParameters.SUNSET, |
| 76 | + DailyParameters.PRECIPITATION_HOURS, |
| 77 | + DailyParameters.TEMPERATURE_2M_MAX, |
| 78 | + ], |
| 79 | + hourly=[ |
| 80 | + HourlyParameters.APPARENT_TEMPERATURE, |
| 81 | + HourlyParameters.TEMPERATURE_2M, |
| 82 | + HourlyParameters.RELATIVE_HUMIDITY_2M, |
| 83 | + HourlyParameters.PRESSURE_MSL, |
| 84 | + HourlyParameters.WIND_SPEED_10M, |
| 85 | + HourlyParameters.PRECIPITATION, |
| 86 | + HourlyParameters.IS_DAY, |
| 87 | + ], |
| 88 | + ) |
| 89 | + if not weather_obj.current_weather: |
| 90 | + return self.translations.Weather.no_weather_found(ctx, safe_location_name) |
| 91 | + hour = weather_obj.hourly.time |
| 92 | + index = min(range(len(hour)), key=lambda i: abs(hour[i] - datetime.datetime.now())) |
| 93 | + |
| 94 | + weather_str, emoji = self.translations.Weather.weather_from_codes( |
| 95 | + ctx, weather_obj.current_weather.weather_code, weather_obj.hourly.is_day[index] |
| 96 | + ) |
| 97 | + wind_direction = self.translations.Weather.weather_wind_direction( |
| 98 | + ctx, weather_obj.current_weather.wind_direction, True |
| 99 | + ) |
| 100 | + |
| 101 | + return self.translations.Weather.weather( |
| 102 | + ctx, |
| 103 | + weather_obj=weather_obj, |
| 104 | + index=index, |
| 105 | + city_name=safe_location_name if is_private else city.display, |
| 106 | + weather_str=weather_str, |
| 107 | + emoji=emoji, |
| 108 | + wind_direction=wind_direction, |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +async def setup(bot: Gorenmu) -> None: |
| 113 | + await bot.add_component(WeatherCmd(bot)) |
| 114 | + |
| 115 | + |
| 116 | +async def teardown(bot: Gorenmu) -> None: ... # NOQA |
0 commit comments