· 7 years ago · Sep 04, 2018, 06:44 PM
1```python
2import discord
3import asyncio
4import sqlite3
5import string
6import random
7
8## Hash
9
10def hash_generator():
11 return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20))
12
13## DB
14
15database = sqlite3.connect('test.db')
16cursor = database.cursor()
17cursor.execute("""
18CREATE TABLE IF NOT EXISTS users(
19 id INTEGER PRIMARY KEY UNIQUE,
20 login TEXT,
21 hash TEXT,
22 confirmed INTEGER
23)""")
24
25database.commit()
26
27def is_new(id):
28 cursor.execute("""SELECT id FROM users WHERE id=?""", (id,))
29 return cursor.fetchone() == None
30
31def new_student(id, hash):
32 cursor.execute("""
33 INSERT INTO users(id, hash, confirmed)
34 VALUES(? , ? , ?)""",
35 (id, hash, 0))
36 database.commit()
37 return
38
39def test_email(login, hash):
40 cursor.execute("""SELECT confirmed FROM users WHERE hash=?""", (hash,))
41 out = cursor.fetchone()
42 if out == None:
43 print("unknown hash : ", hash, " by ", login)
44 elif out[0]:
45 print("hash already used : ", hash, " attempted by : ", login)
46 else:
47 cursor.execute("""
48 UPDATE users SET confirmed = ?, login = ? WHERE hash = ?""",
49 (1, login, hash))
50 database.commit()
51
52## Discord
53client = discord.Client()
54
55@client.event
56async def on_ready():
57 print('Logged in as', end=" : ")
58 print(client.user.name)
59 # print(client.user.id)
60 # print('------')
61
62#@client.event
63#async def on_message(message):
64 #print(message.author.id)
65
66@client.event
67async def on_member_join(member):
68 print("new user : " + member.id)
69 if is_new(member.id):
70 hash = hash_generator()
71 print("hash = " + hash, end="")
72 await client.start_private_message(member)
73 await client.send_message(member,
74 """Salut a toi ! \n Tu es inconnu de nos services,
75 je t'invite donc a envoyer a l'adresse epibot@parou.eu
76 un email avec ta boite epita avec pour sujet le texte suivant :""")
77 await client.send_message(member, "[" + hash +"]")
78 new_student(member.id, hash)
79 print(" id = ", member.id, " done")
80 else:
81 print("Je le connait deja !")
82
83client.run('token')
84```