-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
58 lines (44 loc) · 1.59 KB
/
example.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
from messages import SayText2
from events import Event
from threaded_mysql import ThreadedMySQL
# Initializes the class
TSQL = ThreadedMySQL()
# Connects to a mysql database
TSQL.connect('localhost', 'root', '123', 'my_database', 'utf8')
# Starts the queuehandler (should only be called once)
TSQL.handlequeue_start()
# The callback from !fetchone
def sql_callback(data):
name = data['name']
SayText2(name).send()
# The callback from !fetchall
def sql_callback_2(data, data_pack):
text = data_pack['text']
SayText2("You wrote {}".format(text)).send()
for x in data:
name = x['name']
SayText2('Name: {}'.format(name)).send()
# The callback from !info
def sql_callback_3(get_info):
"""
get_info includes 'query', 'time', 'prioritized'
"""
query = get_info['query']
time = get_info['time']
prio = get_info['prioritized']
SayText2('Query: {0}\nTime: {1} seconds\nPrioritized: {2}'.format(query, time, prio)).send()
@Event('player_say')
def on_player_say(game_event):
# What did the player write
text = game_event['text']
if text == '!fetchone':
# Fetches all the names
TSQL.fetchone('SELECT name FROM my_database', callback=sql_callback)
if text == '!fetchall':
# Let's pass some extra things...
data_pack = {'text': text}
# Fetches one name
TSQL.fetchall('SELECT name FROM my_database', callback=sql_callback_2, data_pack=data_pack)
if text == '!info':
# Fetches one name
TSQL.execute("INSERT INTO my_database (name) VALUES('John')", callback=sql_callback_3, get_info=True)