-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecap.py
279 lines (227 loc) · 10.3 KB
/
recap.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
#coding=utf-8
import discord
import json
import aiohttp
import time
import asyncio
import os
from datetime import datetime,timedelta
from discord.ext import commands
from dotenv import load_dotenv, find_dotenv
import pymysql as MySQLdb
import init_db as init
init.init_database()
load_dotenv(find_dotenv())
months = [
'janvier',
'février',
'mars',
'avril',
'mai',
'juin',
'juillet',
'aout',
'septembre',
'octobre',
'novembre',
'décembre'
]
def get_reminders():
db = MySQLdb.connect(host=os.environ['DB_HOST'], user=os.environ['DB_USER'], password=os.environ['DB_PASSWORD'],
database=os.environ['DB_NAME'])
cursor = db.cursor()
query = "SELECT * FROM reminders WHERE date > NOW()";
cursor.execute(query)
tab = cursor.fetchall()
db.close()
return tab
def add_reminders(date, title, guild):
db = MySQLdb.connect(host=os.environ['DB_HOST'], user=os.environ['DB_USER'], password=os.environ['DB_PASSWORD'],database=os.environ['DB_NAME'])
cursor = db.cursor()
now = datetime.now()
# Format the date and time as a string in the desired format
date_now = now.strftime('%Y-%m-%d %H:%M')
parameters = (None, guild, title, date, date_now)
query = "INSERT INTO reminders VALUES(%s,%s,%s,%s,%s)"
cursor.execute(query, parameters)
db.commit()
db.close()
intents = discord.Intents.default()
client = commands.Bot(command_prefix='!', intents=intents, description="BOT Discord pour l'association Décibels")
client.remove_command('help')
intents.members = True
intents.message_content = True
async def my_background_task():
while True:
now = datetime.now()
now = str(now.strftime("%H:%M"))
if now == "10:00":
await all()
await asyncio.sleep(60)
await asyncio.sleep(45)
@client.event
async def on_ready():
print(f"Connecté en tant que {client.user} ! ")
client.loop.create_task(my_background_task())
# Défitioon de l'object discord event avec ses méthodes pour intéragir avec :
# crédits à https://gist.github.com/adamsbytes/8445e2f9a97ae98052297a4415b5356f pour son code
@client.command()
async def get(ctx):
t = DiscordEvents(os.environ['TOKEN'])
lis = await t.list_guild_events(ctx.guild.id)
channel = discord.utils.get(client.get_all_channels(), id=ctx.channel.id)
embed = discord.Embed(title='Récapitulatif des events à venir \n@everyone', url="https://assos.utc.fr/assos/decibels", description="Petit rappel quotidien des events qui arrivent à grand pas : ",
color=discord.Color.yellow())
embed.set_author(name='Décibels')
embed.set_thumbnail(url="https://assos.utc.fr/images/assos/722b5410-3af5-11e9-bec2-a144d884ae44/1559292260.png")
if(len(lis)==0):
embed.add_field(name="Pas d'évent prévu pour le moment !", value='Oublies pas le ricard', inline=False)
else:
for item in lis:
nom = item['name']
desc = item['description']
start = item['scheduled_start_time']
start_time = time.strptime(start[:-6] + start[-6:].replace(":", ""), '%Y-%m-%dT%H:%M:%S%z')
start_timestamp = time.mktime(start_time)
start_utc = datetime.utcfromtimestamp(start_timestamp)
date = start_utc.strftime('%d %m à %H:%M %Z')
end = item['scheduled_end_time']
place = item['entity_metadata']['location']
text = desc+'\n'+f'Rendez vous à {place} le {date}.'
embed.add_field(name=nom, value=text,inline=False)
embed.set_footer(text="La décibise 🎛️")
await channel.send(embed=embed)
async def all():
channel = await client.fetch_channel(1084526010508259379)
tab_all = []
t = DiscordEvents(os.environ['TOKEN'])
lis = await t.list_guild_events(1084493701943930981)
tab = get_reminders()
embed = discord.Embed(title="Récapitulatif de ce qu'il se passe dans DBS", url="https://assos.utc.fr/assos/decibels",
description="C'est pas toujours facile de penser à tout donc voici un rappel quotidien de ce qui arrive :",
color=discord.Color.yellow())
embed.set_author(name='Décibels')
embed.set_thumbnail(url="https://assos.utc.fr/images/assos/722b5410-3af5-11e9-bec2-a144d884ae44/1559292260.png")
if (len(lis) == 0 and len(tab) == 0):
embed.add_field(name="Rien de prévu pour l'instant", value="Tu peux aller te prendre un ricard", inline=False)
else:
for item in tab:
name = item[2]
date = item[3]
date = date.strftime('mois : %m jour : %d à %H:%M %Z')
tab_all.append((name, date, '', ''))
t = DiscordEvents(os.environ['TOKEN'])
for item in lis:
nom = item['name']
desc = item['description']
place = item['entity_metadata']['location']
start = item['scheduled_start_time']
start_time = time.strptime(start[:-6] + start[-6:].replace(":", ""), '%Y-%m-%dT%H:%M:%S%z')
start_timestamp = time.mktime(start_time)
start_utc = datetime.utcfromtimestamp(start_timestamp)
two_hours = timedelta(hours=2)
start_utc = start_utc + two_hours
date = start_utc.strftime(f' %d {months[int(start_utc.month)-1]} à %H:%M %Z')
tab_all.append((nom, date, desc, place))
tab_all = sorted(tab_all, key=lambda x: x[1])
for item in tab_all:
text = item[2] + '\n' + f'Prévue le {item[1]}'
if item[3] != '':
text = text + f' à {item[3]}'
embed.add_field(name=item[0], value=text, inline=False)
embed.set_footer(text="La décibise 🎛️")
await channel.send(embed=embed)
@client.command()
async def remind(ctx, *args):
if discord.utils.get(ctx.author.roles, name="Burö"):
if(len(args)==0):
await all()
else:
guild = ctx.guild.id
try:
date = args[0].replace('T', ' ') + ':00'
text = ''
t = 0
for mot in args:
if t != 0:
text += mot + ' '
t += 1
print(date)
add_reminders(date, text, guild)
await ctx.message.add_reaction('👍')
except:
await ctx.channel.send(
f"Attention {ctx.author.mention}, le format pour ajouter des évennements au reminder est le suivant: mm-ddTHH:MM")
else:
await ctx.send("Tu n'as pas le rôle nécessaire pour exécuter cette commande.")
class DiscordEvents:
'''Class to create and list Discord events utilizing their API'''
def __init__(self, discord_token: str) -> None:
self.base_api_url = 'https://discord.com/api/v9'
self.auth_headers = {
'Authorization': f'Bot {discord_token}',
'User-Agent': 'DiscordBot (https://your.bot/url) Python/3.11 aiohttp/3.8.1',
'Content-Type': 'application/json'
}
async def list_guild_events(self, guild_id: str) -> list:
'''Returns a list of upcoming events for the supplied guild ID
Format of return is a list of one dictionary per event containing information.'''
event_retrieve_url = f'{self.base_api_url}/guilds/{guild_id}/scheduled-events'
async with aiohttp.ClientSession(headers=self.auth_headers) as session:
try:
async with session.get(event_retrieve_url) as response:
response.raise_for_status()
assert response.status == 200
response_list = json.loads(await response.read())
except Exception as e:
print(f'EXCEPTION: {e}')
finally:
await session.close()
return response_list
async def create_guild_event(
self,
guild_id: str,
event_name: str,
event_description: str,
event_start_time: str,
event_end_time: str,
event_metadata: dict,
event_privacy_level=2,
channel_id=None
) -> None:
'''Creates a guild event using the supplied arguments
The expected event_metadata format is event_metadata={'location': 'YOUR_LOCATION_NAME'}
The required time format is %Y-%m-%dT%H:%M:%S'''
event_create_url = f'{self.base_api_url}/guilds/{guild_id}/scheduled-events'
event_data = json.dumps({
'name': event_name,
'privacy_level': event_privacy_level,
'scheduled_start_time': event_start_time,
'scheduled_end_time': event_end_time,
'description': event_description,
'channel_id': channel_id,
'entity_metadata': event_metadata,
'entity_type': 3
})
async with aiohttp.ClientSession(headers=self.auth_headers) as session:
try:
async with session.post(event_create_url, data=event_data) as response:
response.raise_for_status()
assert response.status == 200
except Exception as e:
print(f'EXCEPTION: {e}')
finally:
await session.close()
@client.command()
async def help(ctx):
embed = discord.Embed(title="Aide aux commandes",
url="https://assos.utc.fr/assos/decibels",
description="Ca peut toujours aider ! ",
color=discord.Color.orange())
embed.set_author(name='Décibels')
embed.set_thumbnail(url="https://assos.utc.fr/images/assos/722b5410-3af5-11e9-bec2-a144d884ae44/1559292260.png")
embed.add_field(name="!remind : affiche dans le channel planning un récap de tous les évents programmés dans discord et ceux ajouté dans le reminder.", inline=False)
embed.add_field(name="!remind YYYY-MM-JJThh:mm votre texte de rappel : Ajoute un reminder dans la base de données des reminders à la date souhaité !", inline=False)
embed.add_field(name="!get : Affiche dans le canal courant les évents programmés dans le discord !", inline=False)
embed.set_footer(text="De la part des passionés de Décibels : la décibise 🎛️")
client.run(os.environ['TOKEN'])