-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (102 loc) · 4.47 KB
/
Copy pathmain.py
File metadata and controls
137 lines (102 loc) · 4.47 KB
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
import os
import datetime
import logging
import discord
from discord.ext import commands, tasks
import data.config as config
from functions import Utils
from dao.pollDao import pollDao
from dao.reminderDao import ReminderDao
logging.basicConfig(
filename=os.path.join(config.path, "logs/bot.log"),
level=logging.ERROR,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
# --- Setup ---
default_intents = discord.Intents.default().all()
default_intents.members = True
client: discord.Client = commands.Bot(command_prefix=config.prefix, help_command=None, intents=default_intents)
utils = Utils(client)
pollDao = pollDao.get_instance()
reminderDao = ReminderDao.get_instance()
@client.event
async def on_ready():
# === general ===
await load(os.path.join(utils.bot_path(), "commands"))
synced = await client.tree.sync()
print(f"{len(synced)} commandes synchronisées")
print(f"Connecté à {client.user.name}")
# === guilds ===
for guild in client.guilds:
if not utils.server_exists_in_config(guild.id):
utils.add_new_server(guild.id)
# === polls ===
await load_polls()
periodic_check.start()
print("Initialisation terminée")
async def load(folder: str, first: bool = True):
"""Load all the cogs"""
for file in os.listdir(folder):
file = os.path.join(folder, file)
if os.path.isdir(file) and first:
await load(file, False)
elif file.endswith(".py"):
file = file.replace(utils.bot_path() + os.sep, "")
file = file.replace("\\", ".").replace("/", ".").replace(":", ".")
await client.load_extension(file[:-3])
async def load_polls():
polls = pollDao.get_all_poll()
for guild_id in polls.copy():
for channel_id in polls[guild_id].copy():
for message_id in polls[guild_id][channel_id].copy():
try:
channel = await client.fetch_channel(int(channel_id))
msg = await channel.fetch_message(int(message_id))
except:
pollDao.remove_poll(int(guild_id), int(channel_id), int(message_id))
continue
pollView = utils.get_poll_object(int(guild_id), int(channel_id), int(message_id))
await msg.edit(view=pollView, embed=pollView.embed)
@tasks.loop(minutes=1)
async def periodic_check():
# Remove all the files in tmp/
try:
for file in os.listdir(os.path.join(config.path, "tmp")):
os.remove(os.path.join(config.path, "tmp", file))
except:
pass
now_timestamp = datetime.datetime.now(config.my_timezone).timestamp()
# Send reminders
reminders = reminderDao.pop_reminder_to_send_at(int(now_timestamp))
for reminder in reminders:
user = await client.fetch_user(reminder.user_id)
await user.send(f"**Rappel:** {reminder.message}")
# Remove all the polls that are finished
polls = pollDao.get_all_poll()
list_to_remove = []
for guild_id in polls:
for channel_id in polls[guild_id]:
for message_id in polls[guild_id][channel_id]:
poll = utils.get_poll_object(guild_id, channel_id, message_id)
if poll.end_timestamp < now_timestamp and not pollDao.is_finish(guild_id, channel_id, message_id):
try:
channel = await client.fetch_channel(channel_id)
msg = await channel.fetch_message(message_id)
except:
list_to_remove.append((guild_id, channel_id, message_id))
continue
await msg.edit(view=poll, embed=poll.embed)
pollDao.set_finish(guild_id, channel_id, message_id)
if round(poll.end_timestamp + datetime.timedelta(weeks=1).total_seconds()) < now_timestamp:
list_to_remove.append((guild_id, channel_id, message_id))
try:
channel = await client.fetch_channel(channel_id)
msg = await channel.fetch_message(message_id)
except:
pollDao.remove_poll(guild_id, channel_id, message_id)
continue
await msg.edit(view=None, embed=poll.embed)
for guild_id, channel_id, message_id in list_to_remove:
pollDao.remove_poll(guild_id, channel_id, message_id)
client.run(config.token)