This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
beambot.py
300 lines (216 loc) · 7.53 KB
/
beambot.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
# -+=============================================================+-
# Version: 3.2.0
# Author: RPiAwesomeness
# Date: July 10, 2015
#
# Changelog: Added response variables to custom commands
# Added !currency command
# Updated !give, !gears, and !quote to support @USERNAME as well
# as the current USERNAME (sans-@)
# Various bug fixes
# -+=============================================================+
import os, sys
import json
import asyncio, websockets, requests
import time, random
import pickle
import responses, commands
from datetime import datetime
from time import sleep
@asyncio.coroutine
def autoCurrency():
global activeChat
timeIncr = 0
while True:
with requests.Session() as session: # Get the list of currently active users
usersRet = session.get(
addr + '/api/v1/chats/' + str(channel) + '/users')
usersRet = usersRet.json()
for user in usersRet: # Give all users +1 gear per minute
userName = user['userName']
curItem = '!give ' + userName + " 1"
autoCurrencyResponse = responses.give('pybot', curItem) # Give the users +1 gear
if timeIncr == 3:
for user in usersRet: # Check all the currently active users
userName = user['userName']
if userName in activeChat: # Has the user chatted in the last 3 minutes?
curItem = '!give ' + userName + " 3"
autoCurrencyResponse = responses.give('pybot', curItem) # Give the users +3 gear for being involved
timeIncr = 0 # Reset the time incrementer
activeChat = [] # Reset the active chat watcher thingy
yield from asyncio.sleep(60)
timeIncr += 1
@asyncio.coroutine
def connect():
global initTime
websocket = yield from websockets.connect(endpoint)
packet = {
"type":"method",
"method":"auth",
"arguments":[channel, user_id, authkey],
"id":0
}
yield from websocket.send(json.dumps(packet))
ret = yield from websocket.recv()
ret = json.loads(ret)
if ret["error"] != None:
print (ret["error"])
print ("Error - Non-None error returned!")
quit()
if int(datetime.now().strftime('%H')) < 12: # It's before 12 PM - morning
timeStr = "mornin'"
elif int(datetime.now().strftime('%H')) >= 12 and int(datetime.now().strftime('%H')) < 17: # It's afternoon
timeStr = "afternoon"
elif int(datetime.now().strftime('%H')) >= 17: # It's after 5 - evening
timeStr = "evenin'"
# If the message doesn't send initially, send it again. The bot just needs to wake up chat
packet = {
"type":"method",
"method":"msg",
"arguments":['Top o\' the {} to you!'.format(timeStr)],
"id":1
}
yield from websocket.send(json.dumps(packet))
ret_msg = yield from websocket.recv()
ret_msg = json.loads(ret_msg)
yield from websocket.close()
@asyncio.coroutine
def readChat():
global initTime, activeChat
activeChat = []
msgLocalID = 0
session = requests.Session()
if os.path.exists('data/blacklist.p'):
msgs_acted = pickle.load(open('data/blacklist.p', "rb"))
else:
msgs_acted = []
pickle.dump(msgs_acted, open('data/blacklist.p', 'wb'))
activeChat = []
if os.path.exists('data/bannedUsers.p'):
bannedUsers = pickle.load(open('data/bannedUsers.p', 'rb'))
else:
bannedUsers = []
pickle.dump(bannedUsers, open('data/bannedUsers.p', 'wb'))
websocket = yield from websockets.connect(endpoint)
packet = {
"type":"method",
"method":"auth",
"arguments":[channel, user_id, authkey],
"id":0
}
response = yield from websocket.send(json.dumps(packet))
while True:
timeCur = datetime.now().strftime("%S")
result = yield from websocket.recv()
if result != None:
result = json.loads(result)
next
print ('result:\t',result,'\n')
if 'event' in result:
if result['event'] == "ChatMessage":
msg = result['data']
msgID = msg['id']
userName = msg['user_name']
if userName not in activeChat:
activeChat.append(userName)
response, goodbye = commands.prepCMD(msg, bannedUsers, msgLocalID, msgs_acted)
if goodbye: # If goodbye is set to true, bot is supposed to turn off
yield from websocket.send(json.dumps(response)) # Send the message
yield from websocket.close()
quit()
if response != None: # Make sure response isn't nothing
#----------------------------------------------------------
# Send the message
#----------------------------------------------------------
# Create the packet
packet = {
"type":"method",
"method":"msg",
"arguments":[response],
"id":msgLocalID
}
msgLocalID += 1 # Increment the msg number variable
yield from websocket.send(json.dumps(packet)) # Send the message
ret_msg = yield from websocket.recv() # Get the response
ret_msg = json.loads(ret_msg) # Convert response to JSON
print ('ret_msg:\t',ret_msg)
if msgID not in msgs_acted: # Don't add duplicates
msgs_acted.append(msgID) # Make sure we don't act on messages again
# Dump the list of msgs_acted into the blacklist.p pickle file so we don't act on those
# messages again.
f = open('data/.blist_temp.p', 'wb')
pickle.dump(msgs_acted, f)
f.flush()
os.fsync(f.fileno())
f.close()
os.rename('data/.blist_temp.p', 'data/blacklist.p')
# ----------------------------------------------------------------------
# Main Code
# ----------------------------------------------------------------------
def _get_auth_body():
return {
'username': config['USERNAME'],
'password': config['PASSWORD']
}
def main():
global authkey, endpoint, channel, user_id, addr, loop, config
config = json.load(open('data/config.json', 'r'))
addr = config['BEAM_ADDR']
session = requests.Session()
loginRet = session.post(
addr + '/api/v1/users/login',
data=_get_auth_body()
)
if loginRet.status_code != requests.codes.ok:
print (loginRet.text)
print ("Not Authenticated!")
quit()
user_id = loginRet.json()['id']
if config['CHANNEL'] == None: # If it's NOT None, then there's no auto-connect
chanOwner = input("Channel [Channel owner's username]: ").lower()
chatChannel = session.get(
addr + '/api/v1/channels/' + chanOwner
)
if chatChannel.status_code != requests.codes.ok:
print ('ERROR!')
print ('Message:\t',chatChannel.json()['message'])
quit()
channel = chatChannel.json()['id']
else:
channel = config['CHANNEL']
chatRet = session.get(
addr + '/api/v1/chats/{}'.format(channel)
)
if chatRet.status_code != requests.codes.ok:
print ('ERROR!')
print ('Message:\t',chatRet.json())
quit()
chat_details = chatRet.json()
endpoint = chat_details['endpoints'][0]
authkey = chat_details['authkey']
print ('authkey:\t',authkey)
print ('endpoint:\t',endpoint)
loop = asyncio.get_event_loop()
tasks = [
asyncio.async(readChat()),
asyncio.async(autoCurrency())
]
loop.run_until_complete(connect())
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
if input("\033[1;34mAre you sure you would like to quit? (Y/n)\033[0m ").lower().startswith('y'):
sys.exit("\033[1;31mTerminated.\033[0m")
else:
main()
except Exception as e:
exception_type, exception_obj, exception_tb = sys.exc_info()
filename = exception_tb.tb_frame.f_code.co_filename
print('\033[1;31mI have crashed.\033[33m\n\nFile "{file}", line {line}\n{line_text}\n{exception}\033[0m'.format(file=filename, line=exception_tb.tb_lineno, line_text=open(filename).readlines()[exception_tb.tb_lineno-1], exception=repr(e)))
print('\033[1;32mRestarting in 10 seconds. Ctrl-C to cancel.\033[0m')
sleep(10)
os.execl(sys.executable, sys.executable, * sys.argv)