-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
109 lines (81 loc) · 4.1 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os, sys, aiohttp, discord, asyncio, pickle, validators
import credentials
from datetime import datetime
from html2text import html2text
from urllib.request import urlretrieve
from urllib.parse import unquote
client = discord.Client()
async def update_notifs():
await client.wait_until_ready()
with open('homework.txt', 'rb') as file :
homework = pickle.load(file)
with open('info.txt', 'rb') as file :
info = pickle.load(file)
while True:
await client.change_presence(status=discord.Status.dnd, activity=discord.Activity(type=discord.ActivityType.watching, name="Pronote..."))
async with aiohttp.ClientSession() as session:
async with session.post('http://127.0.0.1:21727/', json={"type": "fetch", "username": credentials.username, "password": credentials.password, "url": credentials.url, "cas": credentials.cas}) as r:
if r.status == 200:
data = await r.json()
for i in range(len(data['homeworks'])):
if data['homeworks'][i]['content'] not in homework:
homework.append(data['homeworks'][i]['content'])
with open('homework.txt', 'wb') as file :
pickle.dump(homework, file)
await send_notification(data['homeworks'][i]['subject'], data['homeworks'][i]['content'], data['homeworks'][i]['files'], data['homeworks'][i]['until'])
for i in range(len(data['infos'])):
if data['infos'][i]['content'] not in info:
info.append(data['infos'][i]['content'])
with open('info.txt', 'wb') as file :
pickle.dump(info, file)
if not 'title' in data['infos'][i] or data['infos'][i]['title'] == "":
data['infos'][i]['title'] = "Aucun titre"
await send_notification(data['infos'][i]['title'], data['infos'][i]['content'], data['infos'][i]['files'])
await client.change_presence(status=discord.Status.online)
await asyncio.sleep(1000)
async def send_notification(title, content, files=None, timestamp=None):
channel = client.get_channel(credentials.probote_channel)
if content.startswith('style="font-family') : # bug de l'API
content = ">".join(content.split(">")[1:])
content = html2text(content)
if len(content) > 2048 : content = content[:2043]+"[...]"
embed = discord.Embed(title=title, description=content, color=0x0095c7)
if timestamp :
embed.set_author(name="Travail à faire")
embed.timestamp = datetime.fromtimestamp(timestamp // 1000)
else : embed.set_author(name="Information")
nbFiles = len(files) if files else 0
if nbFiles == 0 : embed.set_footer(text="Aucun fichier attaché")
elif nbFiles == 1: embed.set_footer(text="1 fichier attaché")
else: embed.set_footer(text=str(nbFiles) + " fichiers attachés")
await channel.send(embed=embed)
if files :
for file in files :
if type(file) == dict :
if validators.url(file['name']): # si le prof joint un lien
await channel.send(file['name'])
else:
path, _ = urlretrieve(file['url'])
name = file['name']
await channel.send(file=discord.File(path, unquote(name)))
else :
print(file)
path, _ = urlretrieve(file)
name = file.split("/")[-1]
name = name.split("?")[0]
await channel.send(file=discord.File(path, unquote(name)))
@client.event
async def on_ready():
print('Succesfully logged in for Discord as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author.id != credentials.admin :
return
if message.content.startswith('pro ping'):
await message.channel.send('Pong')
print('Pong')
elif message.content.startswith('pro quit'):
await message.channel.send('Déconnexion...')
await client.close()
client.loop.create_task(update_notifs())
client.run(credentials.token)