-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot-devoir.py
316 lines (279 loc) · 13.5 KB
/
bot-devoir.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import discord
from discord.ext import commands
import asyncio
import os
from datetime import datetime
import requests
from icalendar import Calendar
from pytz import timezone
from sqlite3 import *
import re
import typing
intents = discord.Intents.all()
client = commands.Bot(command_prefix='$', intents=intents)
#ID des rôles qui permettent de modifier la BD
role_ids=[1088741449723560027]
#Liste d'ID des personnes autorisées à modif la BD
list_users=[451457197675642901, 234673558935175168]
#Token du bot
TOKEN=""
#Interval d'update pour les devoirs (en secondes)
interval=15
@client.event
async def on_ready():
print("Ready.")
@client.event
async def on_command_error(ctx, error):
await ctx.send(error)
def is_authorized(ctx):
for role in ctx.message.author.roles:
for role_hardcoded in role_ids:
if role.id==role_hardcoded: #Autorise un certain role à faire les commandes (ID)
return true
if ctx.message.author.id in list_users: #Autorise les utilisateurs spécifiés (leur ID)
return True
return False
def create_database():
conn = connect("database.sqlite")
cur = conn.cursor()
cur.executescript("create table messages (id_channel int, id_embed int, id_server int, link text)")
cur.close()
conn.close()
#Création DB si non existante
if not os.path.isfile("database.sqlite"):
create_database()
print("Base de données créée.")
conn = connect("database.sqlite")
@client.command(brief="Qui a créé ce bot?", description="Qui a créé ce bot? (Crédits des devs)")
async def credits(ctx):
embed = discord.Embed(title="Credits", description="ThowZzy#2526 (Thomas, Cybersécurité 2021-2023) - https://thowzzy.be/ \nAstro^^$#7845 (Cybersécurité 2021-2023) - https://astroware.me/ \n\nGithub : https://github.com/ThowZzy/Bot-Devoirs", color=0x00ff00, timestamp=datetime.datetime.now().astimezone(timezone('Europe/Brussels')))
await ctx.send(embed=embed)
@commands.check(is_authorized)
@client.command(brief="Lier l'agenda à un channel avec un lien E-Learn",
description="Lier l'agenda à un channel, ce channel aura un embed qui sera mit à jour toutes les 5 secondes")
async def agenda(ctx, channel: discord.TextChannel, link):
if not valid_link(link):
await ctx.send("Lien invalide !\nExemple de lien : https://learn-technique.helmo.be/calendar/export_execute.php?userid=ID&authtoken=TOKEN&preset_what=all&preset_time=recentupcoming")
return
guild_id = ctx.message.guild.id
embed = discord.Embed(title="Devoirs (Moodle)", color=0x00ff00,
timestamp=datetime.datetime.now().astimezone(timezone('Europe/Brussels')))
embed.set_footer(text="Récupération des devoirs..")
embed_id = await channel.send(embed=embed, view=discord.ui.View(timeout=None).add_item(discord.ui.Button(style=discord.ButtonStyle.green, label="Description des devoirs")))
cur = conn.cursor()
cur.execute(f"insert into messages (id_channel, id_embed ,id_server, link) values (?, ?, ?, ?)", (channel.id, embed_id.id, guild_id, link))
conn.commit()
cur.close()
await ctx.send(f"Agenda assigné à {channel.mention}")
@commands.check(is_authorized)
@client.command(name="bd",
brief="Supprimer un embed de la base de données ou afficher la BD",
description=f"Supprimer un embed de la base de données ou afficher la DB. Usage: {client.command_prefix}bd <del|show> <ID|Optionnal>")
async def bd(ctx, command, ID=None):
guild_id = ctx.message.guild.id
if command == 'del':
if ID is None:
await ctx.send(f"ID de l'embed manquant.\nEX: {client.command_prefix}bd del 941048683221377054")
else:
cur = conn.cursor()
cur.execute(f"DELETE FROM messages WHERE id_embed = ?;", (ID,))
conn.commit()
cur.close()
await ctx.send(f"Embed {ID} supprimé (Si existant)")
elif command == 'show':
embed_edit=discord.Embed(title="Contenu base de données", description="Recupération des données de la BD et vérification de l'existence de chaque embed...")
embed_edit=await ctx.send(embed=embed_edit)
cur = conn.cursor()
cur.execute(f"select * from messages where id_server = ?", (guild_id,))
values=cur.fetchall()
cur.close()
empty=True
k=1
description=""
for value in values:
empty=False
if value[1]:
erreur=False
message=None
channel=None
try:
channel = client.get_channel(value[0])
message=await channel.fetch_message(int(value[1]))
except:
erreur=True
if not erreur:
description+=f"[{k}] Embed [{message.id}]({message.jump_url}) avec comme lien [LIEN]({value[3]}) dans {channel.mention}"
else:
description+=f"[{k}] Embed {value[1]} avec comme lien : [LIEN]({value[3]})"
if not channel:
description+=f"\n[{k}] ⚠️ Channel introuvable !"
else:
description+=f"\n[{k}] ⚠️ Message introuvable ! (Dans {channel.mention})"
description+="\n\n"
k+=1
if empty:
embed = discord.Embed(title="Contenu base de données", description="La BD liée à ce serveur est vide.")
await embed_edit.edit(embed=embed)
else:
embed = discord.Embed(title="Contenu base de données", description=description)
await embed_edit.edit(embed=embed)
else:
await ctx.send(f"Usage: {client.command_prefix}bd <del|show> <ID|Optionnal>")
def valid_link(link):
if not re.match("^https:\/\/learn-technique\.helmo\.be\/calendar\/export_execute\.php\?userid=",link):
return False
try:
get_devoirs(link)
except:
return False
return True
def get_devoirs(calendar):
try:
r = requests.get(calendar)
r.raise_for_status()
except:
return None
gcal = Calendar.from_ical(r.text)
devoirs = []
doublons={}
for event in gcal.walk('VEVENT'):
# Get 'start' date
if 'DTSTART' in event:
try:
dtstart = event['DTSTART'].dt.astimezone(timezone('Europe/Brussels'))
except:
dtstart = False
# Get 'stop' date
if 'DTEND' in event:
try:
dtend = event['DTEND'].dt.astimezone(timezone('Europe/Brussels'))
except:
dtend = False
if event["summary"].endswith("s'ouvre"):
continue
now = datetime.datetime.now().astimezone(timezone('Europe/Brussels'))
if now > dtend:
continue
if "CATEGORIES" in event.keys():
if event["categories"].cats[0] not in doublons.keys():
doublons[event["categories"].cats[0]]=0
else:
doublons[event["categories"].cats[0]]+=1
if doublons[event["categories"].cats[0]]>10:
continue
devoirs.append((event["summary"], event["description"], event["categories"].cats[0], dtstart, dtend))
else:
devoirs.append((event["summary"], event["description"], "Sans cours", dtstart, dtend))
return devoirs
async def agenda_embed():
global interval
while True:
await asyncio.sleep(interval)
cur = conn.cursor()
cur.execute("select * from messages")
rs=cur.fetchall()
cur.close()
for r in rs:
if r[0]:
try: # Continue l'execution en cas d'erreur innatendue
guild_id = r[2]
channel_id = r[0]
channel = client.get_channel(channel_id)
if r[1]:
embed_id = r[1]
embed_message = await channel.fetch_message(int(embed_id))
embed = discord.Embed(title="Devoirs (Moodle)", color=0x00ff00,
timestamp=datetime.datetime.now().astimezone(timezone('Europe/Brussels')))
embed.set_footer(text="Mis à jour")
devoirs = get_devoirs(r[3])
if devoirs is None:
embed = discord.Embed(title="Devoirs (Moodle)", description="Learn injoignable..",color=0x00ff00, timestamp=datetime.datetime.now().astimezone(timezone('Europe/Brussels')))
embed.set_footer(text="Mis à jour")
await embed_message.edit(embed=embed)
break
for devoir in devoirs:
now = datetime.datetime.now().astimezone(timezone('Europe/Brussels'))
delta = devoir[4] - now
jour, heure, minutes, seconds = delta.days, delta.seconds // 3600, (
delta.seconds // 60) % 60, delta.seconds % 60
embed_dict = embed.to_dict()
exists = False
if "fields" in embed_dict.keys():
for field in embed_dict["fields"]:
if field["name"] == f"{devoir[2]}":
field_suite_edit=None
for field_suite in embed_dict["fields"]:
if field_suite["name"] == f"{devoir[2]} (suite)":
field_suite_edit=field_suite
if field_suite_edit is not None:
field_edit=field_suite_edit
else:
field_edit=field
str=""
for value in field_edit["value"]:
str+=value
if len(str)>1024:
embed = discord.Embed.from_dict(embed_dict)
embed.add_field(name=f"{devoir[2]} (suite)",
value=f"{devoir[0]} (Se ferme dans {abs(jour)}j {abs(heure)}h {abs(minutes)}m {abs(seconds)}s)",
inline=False)
embed_dict = embed.to_dict()
exists = True
break
exists = True
field_edit["value"] += f"\n{devoir[0]} (Se ferme dans {abs(jour)}j {abs(heure)}h {abs(minutes)}m {abs(seconds)}s)"
embed = discord.Embed.from_dict(embed_dict)
if not exists:
embed.add_field(name=f"{devoir[2]}",
value=f"{devoir[0]} (Se ferme dans {abs(jour)}j {abs(heure)}h {abs(minutes)}m {abs(seconds)}s)",
inline=False)
if not embed.fields:
embed.add_field(name="Pas de devoirs.. Profites-en pour revoir tes cours",
value="\u200b")
await embed_message.edit(embed=embed)
else:
pass
except Exception as error:
print(error)
# cur.execute(f"DELETE FROM messages WHERE id_embed = {embed_id} ;")
# conn.commit()
await asyncio.sleep(10)
continue
async def react_button():
while True:
res = await client.wait_for('interaction', check=lambda interaction: interaction.data["component_type"] == 2)
calendar = ""
cur = conn.cursor()
cur.execute(f"select * from messages where id_embed = {res.message.id}")
rs=cur.fetchall()
cur.close()
for r in rs:
calendar = r[3]
description = ""
if calendar:
devoirs = get_devoirs(calendar)
if devoirs is None:
embed = discord.Embed(title="Learn injoignable..", description=f"Impossible de contacter learn avec le lien {calendar}")
await res.response.send_message(ephemeral=True,embed=embed)
continue
for devoir in devoirs:
if devoir[1]:
description += f"**{devoir[0]}:**\n{devoir[1].strip()}\n\n"
else:
description += f"**{devoir[0]}:**\nPas de description disponible.\n\n"
if description:
embed = discord.Embed(title="Descriptions des devoirs", description=description)
else:
embed = discord.Embed(title="Baah il n'y a pas de devoirs")
else:
embed = discord.Embed(title="Ce calendrier n'est pas répertorié en BD. omg")
await res.response.send_message(
ephemeral=True,
embed=embed
)
async def main():
async with client:
client.loop.create_task(agenda_embed())
client.loop.create_task(react_button())
await client.start(TOKEN)
asyncio.run(main())