· 6 years ago · Aug 28, 2019, 02:21 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 # this makes it so the bot ignores its own messages
72 if message.author == client.user:
73 return
74
75 # stops repeating
76 if message.content.startswith('$stoprepeat'):
77 repeat = False
78
79 # counts up and remembers which user counted up
80 if message.content.startswith('$count'):
81 count += 1
82 c = conn.cursor()
83 c.execute("""
84 INSERT INTO count_table (ID)
85 VALUES (?)
86 """, (int(message.author.id),))
87 conn.commit()
88 c.close()
89 await message.channel.send(count)
90 # returns a list with which numbers the user that inputted the command has counted using the function above
91 if message.content.startswith("$check"):
92 c = conn.cursor()
93 count_list = c.execute("""
94 SELECT count FROM count_table WHERE ID = ?
95 """, (int(message.author.id),)).fetchall()
96 conn.commit()
97 c.close()
98 await message.channel.send(count_list)
99
100 # stores the number of times the bot has been "booped" in an sql database
101 if message.content.startswith('$boop'):
102 boops_num += 1
103 c = conn.cursor()
104 c.execute("""
105 UPDATE table1 SET num_boops = ?
106 """, (boops_num,))
107 # with open("discord_bot.dat", "w") as f:
108 # f.seek(0, 0)
109 # print(num_boops)
110 # temp_boops = str(num_boops)
111 # f.write(temp_boops)
112 conn.commit()
113 c.close()
114 await message.channel.send("I've been booped {} times.".format(boops_num))
115
116 # calculates the expression after $calculate
117 if message.content.startswith('$calculate'):
118 expression = message.content.replace('$calculate', '')
119 await message.channel.send(eval(expression))
120
121 # this code just ignores messages that start with '$' just so the bot doesn't repeat commands
122 if repeat and not message.content.startswith('$'):
123 print(target_author)
124 if str(message.author.id) == target_author:
125 await message.channel.send(message.content)
126
127 # repeats what the tagged user says until $stoprepeat is inputted
128 if message.content.startswith('$repeat'):
129 temp_msg = message.content.split(" ")
130 target_author = str(temp_msg[1])
131 target_author = target_author[2:-1]
132 repeat = True
133
134 # hello world in discord
135 if message.content.startswith('$hello'):
136 await message.channel.send('Hello!')
137
138
139client.run('NjEyMzQwNzQ0MTY5OTc5OTE2.XVg9tw.BsrSnClFazrpgegMCA9ZBofhVe8')