This commit is contained in:
Adrian Amaglio 2024-05-10 09:54:20 +02:00
commit c13eb0e6df
4 changed files with 169 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.env
guilds.yml
lastreq

24
Dockerfile Normal file
View File

@ -0,0 +1,24 @@
FROM python:3.10-alpine
WORKDIR /usr/src/app
RUN apk add --no-cache \
alpine-conf
RUN pip install --no-cache-dir --upgrade pip
RUN setup-timezone -z Europe/Paris
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
#RUN pip install --no-cache-dir --upgrade --force-reinstall -r requirements.txt
RUN apk del --no-cache \
alpine-conf
ENV UID=0
ENV MOUNT=/
COPY ./main.py .
ENTRYPOINT ["python3", "main.py"]

123
main.py Normal file
View File

@ -0,0 +1,123 @@
import os
import discord
import yaml
import requests
from datetime import date
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.start()
from dotenv import load_dotenv
load_dotenv()
from email.message import EmailMessage
import smtplib, ssl
# Create a secure SSL context
ssl_context = ssl.create_default_context()
def load_guilds_data():
with open("guilds.yml", 'r') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
def send_mail(guild, subject, content):
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = guild['mail_from']
msg['To'] = guild['mailing']
msg.set_content(content)
with smtplib.SMTP_SSL(guild['smtp_host'], guild['smtp_port'], context=ssl_context) as server:
server.login(guild['smtp_username'], guild['smtp_pass'])
server.send_message(msg)
def mail_message(message):
send_mail(guilds[message.guild.id], f'Nouveau message discord de {message.author.display_name}', f'{message.author.display_name}:\n{message.content}')
# Load some data
TOKEN = os.getenv('DISCORD_TOKEN')
guilds = load_guilds_data()
reminder_channels = []
for i in guilds:
if 'reminder_channel' in guilds[i]:
reminder_channels.append(guilds[i]['reminder_channel'])
def erase_framadate (admin_url):
x = requests.post(admin_url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data='remove_all_votes=')
if x.status_code != 200:
print(x)
print(x.content)
x = requests.post(admin_url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data='confirm_remove_all_votes=')
@scheduler.scheduled_job('cron', day=25)
def cleaner ():
for guild_id in guilds:
erase_framadate(guilds[guild_id]['framadate_week'])
erase_framadate(guilds[guild_id]['framadate_weekend'])
# TODO erase calc revenus ?
@scheduler.scheduled_job('cron', day=5)
async def reminder ():
for i in guilds:
message = generate_reminder_message(guilds[i])
send_mail(guilds[i], 'La mutunion cest bientôt !', message)
channel = client.get_channel(guilds[i]['reminder_channel'])
await channel.send(message)
def generate_reminder_message (guild):
# If the 10 is weekend
sondage = guild['framadate_week']
if date.today().replace(day=10).weekday() > 4:
sondage = guild['framadate_weekend']
return f"""
Coucou !
Il est lheure de déclarer ses revenus :
<{guild['link_declaration']}>
Et dannoncer à quelle heure vous souhaitez faire la mututu :
<{sondage}>
Bon début de mois :D
Le mutubot
"""
# Discord API
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'{client.user} is connected to the following guild:\n')
for guild in client.guilds:
print(f'{guild.name} (id: {guild.id})')
await reminder()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.guild.id not in guilds:
return
if message.channel.id in guilds[message.guild.id]['mailed_channels']:
mail_message(message)
# Actually starts the bot
client.run(TOKEN)

19
requirements.txt Normal file
View File

@ -0,0 +1,19 @@
aiohttp==3.9.5
aiosignal==1.3.1
APScheduler==3.10.4
async-timeout==4.0.3
attrs==23.2.0
certifi==2024.2.2
charset-normalizer==3.3.2
discord.py==2.3.2
frozenlist==1.4.1
idna==3.7
multidict==6.0.5
python-dotenv==1.0.1
pytz==2024.1
PyYAML==6.0.1
requests==2.31.0
six==1.16.0
tzlocal==5.2
urllib3==2.2.1
yarl==1.9.4