Reimplement !weather

This commit is contained in:
Terry Hearst 2025-01-18 12:43:50 -05:00
parent d19e31bea0
commit 19a390d60d

58
bot.py
View file

@ -85,6 +85,10 @@ class Bot(commands.Bot):
if message.echo:
return
# Hack - remove invisible character inserted by 7tv completely
# Not good form to mutate the message object but ¯\_(ツ)_/¯
message.content = message.content.replace(u"\U000E0000", "").strip()
if "CHESS_ADDR" in os.environ and "CHESS_TOKEN" in os.environ:
await self.handle_chess(message)
@ -100,21 +104,20 @@ class Bot(commands.Bot):
await message.channel.send(output)
return
content = message.content.replace(u"\U000E0000", "").strip()
if content == self.peer_pressure_message:
if message.content == self.peer_pressure_message:
if not message.author.name in self.peer_pressure_users:
self.peer_pressure_users.append(message.author.name)
if len(self.peer_pressure_users) >= 3:
await message.channel.send(content)
await message.channel.send(message.content)
self.peer_pressure_users = []
else:
self.peer_pressure_message = content
self.peer_pressure_message = message.content
self.peer_pressure_users = [message.author.name]
await self.handle_commands(message)
if len(content.split(" ")) >= 3:
self.shuffle_message = content
if len(message.content.split(" ")) >= 3:
self.shuffle_message = message.content
async def handle_chess(self, message):
# Don't do this if this is already a chess command (e.g. likely to have false positives when entering FEN)
@ -288,7 +291,48 @@ class Bot(commands.Bot):
await ctx.send(message)
# TODO - !weather (requested by linguini15), !chess
# Requested by linguini15
@commands.command()
async def weather(self, ctx: commands.Context):
if "WEATHERAPI_KEY" not in os.environ:
await ctx.send("NoBitches No API Key?")
return
args = ctx.message.content.split(" ", 1)
if len(args) < 2:
await ctx.send("But where doe")
return
location = args[1]
params = {
"key": os.environ["WEATHERAPI_KEY"],
"q": location,
}
r = requests.get("https://api.weatherapi.com/v1/current.json", params=params)
try:
json = r.json()
except:
json = None
if r.status_code == 200:
loc = json["location"]
message = f"Weather in {loc['name']}, "
if loc["country"] == "United States of America":
# Add state and shorten to "USA". Maybe I should add the region for some other countries too
message += f"{loc['region']}, USA"
else:
message += loc["country"]
cur = json["current"]
message += f": {cur['condition']['text']}, {cur['temp_f']}°F/{cur['temp_c']}°C (feels like {cur['feelslike_f']}°F/{cur['feelslike_c']}°C)"
await ctx.send(message)
else:
if json is not None and "error" in json:
code = json["error"]["code"]
message = json["error"]["message"]
print(f"WeatherAPI error code {code}: {message}")
await ctx.send(f"Error: {message}")
else:
print("WeatherAPI unknown error: " + r.text)
await ctx.send("Error!!!! qhar check logs")
# TODO - !chess
def main():