Init
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.idea/
|
||||||
|
.venv/
|
||||||
|
.env
|
||||||
13
Dockerfile
Normal file
13
Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
FROM python:3.12-slim
|
||||||
|
|
||||||
|
ENV PYTHONUNBUFFERED 1
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
RUN pip install --upgrade pip
|
||||||
|
|
||||||
|
COPY ./requirements.txt ./requirements.txt
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
CMD bash -c "python main.py"
|
||||||
9
docker-compose.yml
Normal file
9
docker-compose.yml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
restart: unless-stopped
|
||||||
146
main.py
Normal file
146
main.py
Normal 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)
|
||||||
10
requirements.txt
Normal file
10
requirements.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
aiohttp==3.9.3
|
||||||
|
aiosignal==1.3.1
|
||||||
|
attrs==23.2.0
|
||||||
|
discord.py==2.3.2
|
||||||
|
frozenlist==1.4.1
|
||||||
|
idna==3.6
|
||||||
|
multidict==6.0.5
|
||||||
|
python-dotenv==1.0.1
|
||||||
|
pytz==2024.1
|
||||||
|
yarl==1.9.4
|
||||||
Reference in New Issue
Block a user