working a bit more

This commit is contained in:
Adrian 2024-11-07 19:37:12 +01:00
parent 25e51c0e94
commit 8a3ded3673

60
main.py
View File

@ -3,6 +3,7 @@ import discord
import yaml import yaml
import requests import requests
from datetime import date from datetime import date
import urllib.parse
# To send discord messages (fucking async functions…) # To send discord messages (fucking async functions…)
import asyncio import asyncio
@ -44,6 +45,12 @@ def send_mail(guild, subject, content):
def mail_message(message): 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}') send_mail(guilds[message.guild.id], f'Nouveau message discord de {message.author.display_name}', f'{message.author.display_name}:\n{message.content}')
def req(url, data):
x = requests.post(url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=data)
if x.status_code != 200:
print('ERROR', x)
print(x.content)
raise Exception('Request error ' + str(x.status_code))
# Load some data # Load some data
@ -55,12 +62,42 @@ for i in guilds:
reminder_channels.append(guilds[i]['reminder_channel']) reminder_channels.append(guilds[i]['reminder_channel'])
from html.parser import HTMLParser
class TokenFinder(HTMLParser):
def __init__(self):
self.token=None
super().__init__()
def handle_starttag(self, tag, attrs_tuple):
if tag != 'input':
return
attrs = dict(attrs_tuple)
if tag == 'input' and 'type' in attrs and attrs['type'] == 'hidden' and 'name' in attrs and attrs['name'] == 'control' and 'value' in attrs:
self.token = attrs['value']
def create_framavote (guild, names):
erase_framadate(guild['framavote'])
for i in range(len(guild['members'])):
create_line_framadate(guild['framavote'], 'AnneONyme'+str(i), names)
def erase_framadate (admin_url): def erase_framadate (admin_url):
x = requests.post(admin_url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data='remove_all_votes=') req(admin_url, 'remove_all_votes=')
if x.status_code != 200: req(admin_url, 'confirm_remove_all_votes=')
print(x)
print(x.content)
x = requests.post(admin_url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data='confirm_remove_all_votes=') def create_line_framadate (admin_url, voter, names):
finder = TokenFinder()
finder.feed(requests.get(admin_url).content.decode('UTF-8'))
if not finder.token:
print('Framavote token not found')
data = 'control=' + finder.token + '&name=' + voter + ''.join('&choices%5B'+str(i)+'%5D=+' for i in range(len(names))) + '&save='
print(data)
x = req(admin_url, data)
@scheduler.scheduled_job('cron', day=25) @scheduler.scheduled_job('cron', day=25)
def cleaner (): def cleaner ():
@ -104,7 +141,7 @@ Le mutubot
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
client = discord.Client(intents=intents) client = discord.Client(intents=intents)
randomvote = None
@client.event @client.event
async def on_ready(): async def on_ready():
@ -114,13 +151,22 @@ async def on_ready():
@client.event @client.event
async def on_message(message): async def on_message(message):
global randomvote
if message.author == client.user: if message.author == client.user:
return return
if message.guild.id not in guilds: if message.guild.id not in guilds:
return return
if message.content.startswith('!randomvote '):
await message.reply('Êtes vous sûr·e ? Répondez « Pamplemousse agrivoltaiste » pour confirmer')
randomvote = message.content.split(' ')[1:]
return
if message.content == 'Pamplemousse agrivoltaiste' and message.type == discord.MessageType.reply :
create_framavote(guilds[message.guild.id], ['1', '2', '3', '4', '5', '6'])
randomvote = None
await message.reply("Cest fait ! Vérifiez bien par vous même parce que je suis un peu fini à larache…")
return
if message.channel.id in guilds[message.guild.id]['mailed_channels']: if message.channel.id in guilds[message.guild.id]['mailed_channels']:
mail_message(message) mail_message(message)
# Actually starts the bot # Actually starts the bot
client.run(TOKEN) client.run(TOKEN)