· 6 years ago · May 05, 2019, 12:02 PM
1import discord, sqlite3, re
2
3#function to read token in another file
4def readToken():
5 with open("token.txt", "r") as f:
6 lines = f.readlines()
7 return lines[0].strip()
8
9#reading token
10token = readToken()
11
12client = discord.Client()
13
14
15#creating student class to fill the database
16class student:
17 def __init__(self, UID, nick, squat, bench, deadlift):
18 self.UID = UID
19 self.nick = nick
20 self.squat = squat
21 self.bench = bench
22 self.deadlift = deadlift
23
24conn = sqlite3.connect("atlas.db")
25
26c = conn.cursor()
27
28#creating the database, ignore if already exists
29c.execute("""CREATE TABLE IF NOT EXISTS leaderboards (
30 id integer PRIMARY KEY,
31 nick text,
32 squat integer,
33 bench integer,
34 deadlift integer
35 )""")
36
37#function to add or update the leaderboards table
38def insert_stud(stud):
39 with conn:
40 c.execute("REPLACE INTO leaderboards VALUES (?,?,?,?,?)", (stud.UID, stud.nick, stud.squat, stud.bench, stud.deadlift))
41
42
43#create a Regular expression to find the weights in the user's message on Discord
44weightRegex = re.compile(r'(\d?\d\d)\s(\d?\d\d)\s(\d?\d\d)')
45
46#function to return the best Squat
47def bestSquat():
48 c.execute("SELECT nick, MAX(squat) FROM leaderboards")
49 champ=c.fetchall()
50 msg= str(champ[0][0] + " a le record du squat avec " + str(champ[0][1]) + "Kgs.")
51 return msg
52
53#function to return the best Bench
54def bestBench():
55 c.execute("SELECT nick, MAX(bench) FROM leaderboards")
56 champ=c.fetchall()
57 msg= str(champ[0][0] + " a le record du bench avec " + str(champ[0][1]) + "Kgs.")
58 return msg
59
60#function to return the best deadlift
61def bestDeadlift():
62 c.execute("SELECT nick, MAX(deadlift) FROM leaderboards")
63 champ=c.fetchall()
64 msg= str(champ[0][0] + " a le record du deadlift avec " + str(champ[0][1]) + "Kgs.")
65 return msg
66
67
68@client.event
69async def on_ready():
70 print('We have logged in as {0.user}'.format(client))
71
72@client.event
73async def on_message(message):
74 if message.author == client.user:
75 return
76
77 if message.content.startswith('!ldbs'):
78 dID = message.author.id #get the ID as primary key
79 name = str(message.author) #transform the author name into a string
80 mo = weightRegex.search(message.content) #search for the weights in the text
81 studInfo = student(int(dID),name[:-5],int(mo.group(1)),int(mo.group(2)),int(mo.group(3)))
82 insert_stud(studInfo) #create or update the student
83 conn.commit()
84
85 if message.content.startswith('!bsquat'):
86 await message.channel.send(bestSquat())
87
88 if message.content.startswith('!bbench'):
89 await message.channel.send(bestBench())
90
91 if message.content.startswith('!bdeadlift'):
92 await message.channel.send(bestDeadlift())
93
94 if message.content.startswith('!mymax'): #return my max in kilos
95 sqlcmd="SELECT squat,bench,deadlift FROM leaderboards WHERE id = '"+str(message.author.id)+"'"
96 c.execute(sqlcmd)
97 number=c.fetchall()
98 total= number[0][0] + number[0][1] + number[0][2]
99 await message.channel.send("Ton total en poids est de " + str(total) +" Kilos!")
100
101 if message.content.startswith('!hello'):
102 msg = 'Hello {0.author.mention}'.format(message)
103 await message.channel.send(msg)
104
105
106
107client.run(token)