-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (54 loc) · 1.99 KB
/
main.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
#Import library Dispord.py // https://discordpy.readthedocs.io/en/stable/
import discord
#Import livrary Sqlite // https://pypi.org/project/pysqlite3/
import sqlite3
#Import livrary for environment variables
import os
from dotenv import load_dotenv
load_dotenv()
#Set environment variables
BOT_TOKEN = os.getenv('BOT_TOKEN')
#Set Discord client
client = discord.Client()
#Connect to DB "mdkir users.db"
conn = sqlite3.connect('users.db')
#A dit function informs you when the bot is ready
@client.event
async def on_ready():
print("Bot is ready !")
#A function to manage commands sent to the bot
@client.event
async def on_message(message):
#Verify is command is send by bot
if (message.author.bot) : return
#Set data user
channel_id = message.channel.id
channel = client.get_channel(channel_id)
IdentityUserSendMessage = message.author.name + '#' + message.author.discriminator
#The different possible commands and an example CRUD for the interaction with the database
if (message.content == "!register"):
c = conn.cursor()
c.execute("INSERT INTO users (discordIdentity) VALUES (?)", (IdentityUserSendMessage,))
conn.commit()
text = "User " + IdentityUserSendMessage + " Registed !"
await channel.send(text)
if (message.content == "!init"):
c = conn.cursor()
c.execute('''CREATE TABLE users (discordIdentity)''')
conn.commit()
text = "Succefull Init !"
await channel.send(text)
if (message.content == "!list-user"):
c = conn.cursor()
c.execute('SELECT * FROM users')
result = c.fetchall()
print(result)
conn.commit()
await channel.send(result)
if (message.content == "!delete"):
c = conn.cursor()
delete = await c.execute('DELETE FROM users WHERE discordIdentity=?', (IdentityUserSendMessage,))
print(delete)
conn.commit()
#Starting the discord bot with the identification token obtained on the Discord dashboard linked to the bot
client.run(BOT_TOKEN)