· 6 years ago · Aug 28, 2019, 02:04 PM
1import discord
2import sqlite3
3
4# Global variables
5repeat = False
6target_author = ""
7boops_num = 0
8count = 0
9
10client = discord.Client()
11
12conn = sqlite3.connect("discord_bot")
13
14c = conn.cursor()
15
16# SQL table definitions
17c.execute("""
18CREATE TABLE IF NOT EXISTS table1(
19 num_boops INTEGER
20);
21""")
22
23c.execute("""
24CREATE TABLE IF NOT EXISTS count_table(
25 ID INTEGER NOT NULL ,
26 count INTEGER PRIMARY KEY AUTOINCREMENT
27 );
28""")
29
30temp = c.execute("SELECT count FROM count_table").fetchall()
31# checks if count is empty, if it isn't it adds the tuple to the count variable
32if temp:
33 count = temp[-1][0]
34 print("Count: ", count)
35temp = c.execute("SELECT * FROM table1").fetchall()
36
37# checks if the table is empty, if it isn't it fetches the number
38if not temp:
39 c.execute("""
40 INSERT INTO table1 (num_boops)
41 VALUES (0)
42 """)
43else:
44 boops_num = temp[0][0]
45conn.commit()
46
47c.close()
48
49
50# try:
51# with open('discord_bot.dat', 'r') as f:
52# num_boops = int(f.readline())
53# except FileNotFoundError:
54# with open('discord_bot.dat', 'w') as f:
55# f.write('0')
56# num_boops = 0
57
58
59@client.event
60async def on_ready():
61 print('We have logged in as {0.user}'.format(client))
62
63
64@client.event
65async def on_message(message):
66 global repeat
67 global target_author
68 global boops_num
69 global count
70
71 if message.author == client.user:
72 return
73
74 if message.content.startswith('$stoprepeat'):
75 repeat = False
76
77 if message.content.startswith('$count'):
78 count += 1
79 c = conn.cursor()
80 c.execute("""
81 INSERT INTO count_table (ID)
82 VALUES (?)
83 """, (int(message.author.id),))
84 conn.commit()
85 c.close()
86 await message.channel.send(count)
87
88 if message.content.startswith("$check"):
89 c = conn.cursor()
90 count_list = c.execute("""
91 SELECT count FROM count_table WHERE ID = ?
92 """, (int(message.author.id),)).fetchall()
93 conn.commit()
94 c.close()
95 await message.channel.send(count_list)
96
97 if message.content.startswith('$boop'):
98 boops_num += 1
99 c = conn.cursor()
100 c.execute("""
101 UPDATE table1 SET num_boops = ?
102 """, (boops_num,))
103 # with open("discord_bot.dat", "w") as f:
104 # f.seek(0, 0)
105 # print(num_boops)
106 # temp_boops = str(num_boops)
107 # f.write(temp_boops)
108 conn.commit()
109 c.close()
110 await message.channel.send("I've been booped {} times.".format(boops_num))
111
112 if message.content.startswith('$calculate'):
113 expression = message.content.replace('$calculate', '')
114 await message.channel.send(eval(expression))
115
116 if repeat and not message.content.startswith('$'):
117 print(target_author)
118 if str(message.author.id) == target_author:
119 await message.channel.send(message.content)
120
121 if message.content.startswith('$repeat'):
122 temp_msg = message.content.split(" ")
123 target_author = str(temp_msg[1])
124 target_author = target_author[2:-1]
125 repeat = True
126
127 if message.content.startswith('$hello'):
128 await message.channel.send('Hello!')
129
130
131client.run('NjEyMzQwNzQ0MTY5OTc5OTE2.XVg9tw.BsrSnClFazrpgegMCA9ZBofhVe8')