diff --git a/main.py b/main.py index 1cf6eb2..8e219f2 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import discord import yaml import requests from datetime import date +import urllib.parse # To send discord messages (fucking async functions…) import asyncio @@ -44,6 +45,12 @@ def send_mail(guild, subject, content): 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}') +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 @@ -55,12 +62,42 @@ for i in guilds: 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): - 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=') + req(admin_url, 'remove_all_votes=') + req(admin_url, '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) def cleaner (): @@ -104,7 +141,7 @@ Le mutubot intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) - +randomvote = None @client.event async def on_ready(): @@ -114,13 +151,22 @@ async def on_ready(): @client.event async def on_message(message): + global randomvote if message.author == client.user: return if message.guild.id not in guilds: 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("C’est fait ! Vérifiez bien par vous même parce que je suis un peu fini à l’arache…") + return if message.channel.id in guilds[message.guild.id]['mailed_channels']: mail_message(message) - # Actually starts the bot client.run(TOKEN)