This commit is contained in:
2024-02-14 22:06:20 +01:00
commit e9aaaaf3ff
6 changed files with 183 additions and 0 deletions

146
main.py Normal file
View File

@@ -0,0 +1,146 @@
from os import getenv
import aiohttp
from dotenv import load_dotenv
import discord
from discord.ext import tasks
import asyncio
import datetime
import pytz
load_dotenv()
TOKEN = getenv('DISCORD_TOKEN')
CHANNEL_ID = int(getenv('CHANNEL_ID'))
TEMPO_API_BASE = "https://www.api-couleur-tempo.fr/api"
TEMPO_API_TODAY = f"{TEMPO_API_BASE}/jourTempo/today"
TEMPO_API_TOMORROW = f"{TEMPO_API_BASE}/jourTempo/tomorrow"
map_code_jour = {
0: {
"title": "Non déterminé",
"icon": ""
},
1: {
"title": "Jour Bleu",
"icon": "🔵"
},
2: {
"title": "Jour Blanc",
"icon": "",
},
3: {
"title": "Jour Rouge (TRÈS CHÈRE)",
"icon": "🔴"
}
}
paris_timezone = pytz.timezone("Europe/Paris")
# fonctionne pas correctement avec les timezone
# today_hour = datetime.time(hour=0, minute=1, tzinfo=paris_timezone)
# tomorrow_hour = datetime.time(hour=12, minute=22, tzinfo=paris_timezone)
async def fetch_today():
async with aiohttp.ClientSession() as session:
async with session.get(TEMPO_API_TODAY) as response:
return await response.json()
async def fetch_tomorrow():
async with aiohttp.ClientSession() as session:
async with session.get(TEMPO_API_TOMORROW) as response:
return await response.json()
def format_today(data):
date_day = data["dateJour"]
code_day = data["codeJour"]
return f"!tempo_today - {map_code_jour[code_day]['icon']} {map_code_jour[code_day]['title']} - {date_day}"
def format_tomorrow(data):
date_day = data["dateJour"]
code_day = data["codeJour"]
return f"!tempo_tomorrow - {map_code_jour[code_day]['icon']} {map_code_jour[code_day]['title']} - {date_day}"
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sent_messages = {
"today": None,
"tomorrow": None
}
self.channel = None
async def setup_hook(self) -> None:
print("Setting up")
self.background_update_today.start()
self.background_update_tomorrow.start()
async def on_ready(self):
print(f'Logged in as {self.user}')
self.channel = self.get_channel(CHANNEL_ID)
async def on_message(self, message: discord.Message) -> None:
# print(f"Message from {message.author}: {message}")
match message.content:
case "?tempo_today":
await self._send_today(message)
case "?tempo_tomorrow":
await self._send_tomorrow(message)
async def _send_today(self, response_to: discord.Message=None):
data = await fetch_today()
message = format_today(data)
if response_to:
await response_to.reply(message)
else:
await self.channel.send(message)
async def _send_tomorrow(self, response_to: discord.Message=None):
data = await fetch_tomorrow()
message = format_tomorrow(data)
if response_to:
await response_to.reply(message)
else:
await self.channel.send(message)
@tasks.loop(seconds=60)
async def background_update_today(self):
now = datetime.datetime.now(tz=paris_timezone).time()
print(now.hour, now.minute)
if now.hour == 0 and now.minute == 0:
async for message in self.channel.history():
content = message.content
if message.author.id == self.user.id:
if content.startswith("!tempo_today") or content.startswith("!tempo_tomorrow"):
await message.delete()
await self._send_today()
@tasks.loop(seconds=60)
async def background_update_tomorrow(self):
now = datetime.datetime.now(tz=paris_timezone).time()
if now.hour == 12 and now.minute == 0:
async for message in self.channel.history():
content = message.content
if message.author.id == self.user.id:
if content.startswith("!tempo_tomorrow"):
await message.delete()
await self._send_tomorrow()
intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)
client.run(TOKEN)