added weather base
This commit is contained in:
parent
90cd9bab71
commit
f69271e14c
43
task_bot.py
43
task_bot.py
@ -1,31 +1,66 @@
|
||||
import os
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from weather import OpenWeatherMapAPIClient
|
||||
|
||||
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
|
||||
DISCORD_TOKEN = os.getenv('MTQ4MDkxODI5Mzk5MTc4ODYzNQ.GQ4P7l.uqYM-awr8zAp6eun9iJr0ziDhscv3XiByqSMpw')
|
||||
WEATHER_TOKEN = os.getenv("WEATHER_TOKEN")
|
||||
|
||||
weather_client = OpenWeatherMapAPIClient(WEATHER_TOKEN, "MyDiscordWeatherBot")
|
||||
intents = discord.Intents(messages=True, guilds=True)
|
||||
intents.message_content = True
|
||||
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||
|
||||
@bot.tree.command(name="weather")
|
||||
async def current_weather(interaction: discord.Interaction, location: str):
|
||||
|
||||
print(f"Received weather command from {interaction.user.display_name}") # Log intéraction
|
||||
|
||||
current_weather = weather_client.get_current_weather(location)
|
||||
|
||||
print(f"JSON API {current_weather}")
|
||||
|
||||
# Check that current_weather is a dictionary and contains necessary keys
|
||||
if not isinstance(current_weather, dict) or 'main' not in current_weather or 'weather' not in current_weather:
|
||||
await interaction.response.send_message("Could not retrieve weather data. Please check the location.")
|
||||
return
|
||||
|
||||
weather_list = current_weather['weather']
|
||||
if len(weather_list) == 0:
|
||||
await interaction.response.send_message("Weather information is not available.")
|
||||
return
|
||||
|
||||
weather_condition = weather_list[0]['main']
|
||||
|
||||
temp = current_weather['main']['temp']
|
||||
|
||||
icon = current_weather['weather'][0]['icon']
|
||||
embed = discord.Embed(
|
||||
title=f"Current weather in {location}",
|
||||
description=f"Temperature: {temp}°C\n Sky: {weather_condition}",
|
||||
)
|
||||
embed.set_thumbnail(url=f"https://openweathermap.org/img/wn/{icon}.png")
|
||||
|
||||
await interaction.response.send_message(embed=embed)
|
||||
|
||||
|
||||
@bot.tree.command(name='hello')
|
||||
async def hello_command(interaction: discord.Interaction):
|
||||
|
||||
print(f"Received hello command from {interaction.user.display_name}") # Log intéraction
|
||||
|
||||
user_nick = interaction.user.display_name
|
||||
await ctx.response.send_message(f'Hello {ctx.user.nick}!')
|
||||
await interaction.response.send_message(f'Hello {user_nick}!')
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f'Logged in as {bot.user}')
|
||||
await bot.tree.sync()
|
||||
|
||||
#Liste commandes registré
|
||||
#Liste commandes enregistré
|
||||
commands = await bot.tree.fetch_commands()
|
||||
print("Registered Commands:")
|
||||
for command in commands:
|
||||
print(f"- {command.name}")
|
||||
|
||||
|
||||
bot.run(DISCORD_TOKEN)
|
||||
29
weather.py
Normal file
29
weather.py
Normal file
@ -0,0 +1,29 @@
|
||||
import requests
|
||||
from geopy.geocoders import Nominatim
|
||||
|
||||
class OpenWeatherMapAPIClient:
|
||||
def __init__(self, api_token, name):
|
||||
self.base_url = "https://api.openweathermap.org"
|
||||
self._api_token = "c6ee0c7c013f6d4bc9dc20982aecb6ec"
|
||||
self.name = name
|
||||
|
||||
def get_geodata(self, location):
|
||||
geolocator = Nominatim(user_agent=self.name)
|
||||
geodata = geolocator.geocode(location, language="en-us").raw
|
||||
|
||||
return geodata["lat"], geodata["lon"]
|
||||
|
||||
def get_current_weather(self, location, units="metric"):
|
||||
url = f"{self.base_url}/data/2.5/weather"
|
||||
lat, lon = self.get_geodata(location)
|
||||
params = {
|
||||
"lat": lat,
|
||||
"lon": lon,
|
||||
"units": units,
|
||||
"appid": self._api_token,
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
data = response.json()
|
||||
|
||||
return data
|
||||
Loading…
x
Reference in New Issue
Block a user