-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_management.py
207 lines (169 loc) · 9.04 KB
/
client_management.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
import discord
from discord.ext import commands
import sqlite3
import random
import string
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
from dotenv import load_dotenv
import os
load_dotenv()
EMAIL_SENDER = os.getenv("EMAIL_SENDER")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
def generate_key(length=12):
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
def send_email(client_email, key):
sender_email = EMAIL_SENDER
sender_password = EMAIL_PASSWORD
subject = "Bienvenue chez Digital Dynamics Studio !"
with open("email_welcome_template.html", "r") as file:
html_content = file.read()
html_content = html_content.replace("{{ key }}", key)
msg = MIMEMultipart("alternative")
msg['From'] = sender_email
msg['To'] = client_email
msg['Subject'] = subject
part1 = MIMEText(html_content, "html")
msg.attach(part1)
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
text = msg.as_string()
server.sendmail(sender_email, client_email, text)
server.quit()
print(f"Email envoyé à {client_email}")
except Exception as e:
print(f"Erreur lors de l'envoi de l'email : {e}")
def validate_date(date_text):
try:
date = datetime.strptime(date_text, '%d/%m/%Y')
return date
except ValueError:
return None
class ClientManagement(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.client_creation_data = {}
self.allowed_channel_id = 1267789734868947034
@commands.command()
async def creation(self, ctx):
print("Commande !creation appelée")
if ctx.channel.id != self.allowed_channel_id:
await ctx.send("Cette commande ne peut être utilisée que dans le canal autorisé.")
return
if not ctx.channel.permissions_for(ctx.author).manage_channels:
await ctx.send("Vous n'avez pas la permission d'utiliser cette commande.")
return
self.client_creation_data[ctx.author.id] = {}
await ctx.send("Entrez le nom du client :")
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
msg = await self.bot.wait_for('message', check=check, timeout=60)
self.client_creation_data[ctx.author.id]['name'] = msg.content
await ctx.send("Entrez le nom de l'entreprise :")
msg = await self.bot.wait_for('message', check=check, timeout=60)
self.client_creation_data[ctx.author.id]['company'] = msg.content
await ctx.send("Entrez l'email du client :")
msg = await self.bot.wait_for('message', check=check, timeout=60)
self.client_creation_data[ctx.author.id]['email'] = msg.content
await ctx.send("Entrez le numéro de téléphone du client :")
msg = await self.bot.wait_for('message', check=check, timeout=60)
self.client_creation_data[ctx.author.id]['phone'] = msg.content
await ctx.send("Entrez le nom du projet :")
msg = await self.bot.wait_for('message', check=check, timeout=60)
self.client_creation_data[ctx.author.id]['project_name'] = msg.content
await ctx.send("Entrez la description du projet :")
msg = await self.bot.wait_for('message', check=check, timeout=60)
self.client_creation_data[ctx.author.id]['project_description'] = msg.content
await ctx.send("Entrez la date de la première visio (jours/mois/année) :")
valid_date = None
while not valid_date:
msg = await self.bot.wait_for('message', check=check, timeout=60)
valid_date = validate_date(msg.content)
if not valid_date:
await ctx.send("Date invalide. Veuillez entrer une date au format jours/mois/année :")
self.client_creation_data[ctx.author.id]['visio_date'] = valid_date.strftime('%d/%m/%Y')
key = generate_key()
self.client_creation_data[ctx.author.id]['key'] = key
conn = sqlite3.connect('clients.db')
c = conn.cursor()
c.execute("INSERT INTO clients (name, company, email, phone, project_name, project_description, key, visio_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(self.client_creation_data[ctx.author.id]['name'],
self.client_creation_data[ctx.author.id]['company'],
self.client_creation_data[ctx.author.id]['email'],
self.client_creation_data[ctx.author.id]['phone'],
self.client_creation_data[ctx.author.id]['project_name'],
self.client_creation_data[ctx.author.id]['project_description'],
self.client_creation_data[ctx.author.id]['key'],
self.client_creation_data[ctx.author.id]['visio_date']))
conn.commit()
conn.close()
send_email(self.client_creation_data[ctx.author.id]['email'], key)
await ctx.send(f"Client ajouté et clé envoyée à {self.client_creation_data[ctx.author.id]['email']}")
del self.client_creation_data[ctx.author.id]
except Exception as e:
await ctx.send("Temps écoulé ou erreur. Veuillez réessayer.")
if ctx.author.id in self.client_creation_data:
del self.client_creation_data[ctx.author.id]
print(f"Erreur lors de l'exécution de la commande : {e}")
@commands.Cog.listener()
async def on_member_join(self, member):
await member.send("Bienvenue ! Veuillez entrer votre clé d'accès :")
def check(m):
return m.author == member and isinstance(m.channel, discord.DMChannel)
try:
msg = await self.bot.wait_for('message', check=check, timeout=60)
key = msg.content
conn = sqlite3.connect('clients.db')
c = conn.cursor()
c.execute("SELECT * FROM clients WHERE key=?", (key,))
client = c.fetchone()
conn.close()
if client:
await member.send("Veuillez entrer votre adresse email pour vérification :")
msg = await self.bot.wait_for('message', check=check, timeout=60)
email = msg.content
if email == client[3]: # Vérification de l'email
guild = member.guild
role = discord.utils.get(guild.roles, name="Client")
if role is not None:
await member.add_roles(role)
else:
await member.send("Le rôle 'Client' n'existe pas. Veuillez contacter un administrateur.")
if guild.me.guild_permissions.manage_channels:
# Créer une catégorie et des canaux pour le projet
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True)
}
category = await guild.create_category(f"{client[5]} - {client[2]}" , overwrites=overwrites)
await guild.create_text_channel('général', category=category)
await guild.create_text_channel('planning', category=category)
await guild.create_text_channel('discussion', category=category)
await guild.create_text_channel('notification-trello', category=category)
await guild.create_text_channel('notification-github', category=category)
await guild.create_voice_channel('visio', category=category)
# Envoyer un message de bienvenue
welcome_message = f"Bienvenue {client[1]} ! Votre projet '{client[5]}' a été configuré. La date de votre première visio est fixée au {client[8]}."
await member.send(welcome_message)
# Supprimer la clé après utilisation
conn = sqlite3.connect('clients.db')
c = conn.cursor()
c.execute("UPDATE clients SET key=NULL WHERE key=?", (key,))
conn.commit()
conn.close()
else:
await member.send("Je n'ai pas la permission de créer des canaux. Veuillez contacter un administrateur.")
else:
await member.send("Adresse email incorrecte. Veuillez contacter un administrateur.")
else:
await member.send("Clé invalide. Veuillez contacter un administrateur.")
except Exception as e:
await member.send("Temps écoulé ou erreur. Veuillez réessayer.")
print(e)
async def setup(bot):
await bot.add_cog(ClientManagement(bot))