-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.py
179 lines (145 loc) · 4.16 KB
/
Bot.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
#!usr/bin/python python
from google import search
import websocket
import threading
import json
import sys
import random
import time
import ssl
class ChatBot(threading.Thread):
def __init__(self, BotName, Room):
threading.Thread.__init__(self)
self.trigger = '*'
self.botname = BotName
self.room = Room
self.online = []
self.bots = ['AtlasBot', 'coderRank', 'hatBot', 'modBot', 'TBotT', 'wwbot']
self.botsInfo = {
'AtlasBot': {
'creator': 'Atlas',
'version': '1.0',
'trigger': '*'
},
'TBotT': {
'creator': 'M4GNV5',
'version': '?',
'trigger': '!'
},
'wwbot': {
'creator': 'wwandrew',
'version': '1.5',
'trigger': '#'
}
}
# Commands
def help_(self, data):
self.sendMessage("Commands are: " + self.trigger + "help, " + self.trigger + "info, " + self.trigger + "slap.")
def checkBots(self):
onlineBots = set(self.bots) & set(self.online)
if len(list(onlineBots)) == 0:
self.sendMessage("There are no active bots in this room.")
else:
online = ("Yes. Current active bots are: " + ', '.join(onlineBots) + ". Type *info <botname> to see more information about bot")
self.sendMessage(online)
def checkMods(self):
pass
def info(self, data):
if len(self.msg) == 1:
self.sendMessage("Type *info <subject> for information about said subject.")
else:
if (self.msg[1] in self.bots) == True:
info = "Info about " + self.msg[1] + ": " + str(self.botsInfo[self.msg[1]])
self.sendMessage(info)
# Chat
def main(self, data):
print(data['nick']) + ":"
print(data['text'])
self.splitData(data['text'])
trig = ''.join(self.msg[0])
self.trig = trig.split()[0][:1]
if data['nick'] != self.botname:
pass
self.checkMessage(data)
self.commands(data)
def online_Set(self, data):
self.online = data['nicks']
print("[*] Users online:")
for n in data['nicks']:
print(n)
def online_Add(self, data):
self.online.append(data['nick'])
print("[*] User " + data['nick'] + " joined.")
def online_Remove(self, data):
self.online.remove(data['nick'])
print("[*] User " + data['nick'] + " left.")
def handleInfo(self, data):
print("[*] Unknown Info.")
print(data)
# Functions
def ping(self):
self.sendJSON({"cmd":"ping"})
threading.Timer(30.0, self.ping).start()
def sendJSON(self, data):
self.ws.send(json.dumps(data))
def sendMessage(self, data):
self.ws.send(json.dumps({"cmd": "chat", "text": data}))
def splitData(self, data):
self.msg = (data.split())
self.Lmsg = [x.lower() for x in self.msg]
def checkMessage(self, data):
if set(['are', 'there', 'any']) & set(self.Lmsg) == set(['are', 'there', 'any']):
try:
{
'bots': self.checkBots,
'mods': self.checkMods
}[self.msg[3]]()
except Exception as b:
print b
elif ('google' in self.Lmsg[0]) == True:
results = [url for url in search(' '.join(self.msg[1:]), stop=1)]
results = ', '.join(results[:1])
self.sendMessage(results)
def commands(self, data):
if self.trig == self.trigger:
try:
{
self.trigger + 'help': self.help_,
self.trigger + 'info': self.info
}[self.msg[0]](data)
except Exception as a:
if len(self.msg) == 1:
self.sendMessage("Not a valid command.")
else:
pass
# Websockets
def on_message(self, wbsk, message):
print
data = json.loads(message)
cmd = data['cmd']
try:
{
"onlineSet":self.online_Set,
"onlineAdd":self.online_Add,
"onlineRemove":self.online_Remove,
"chat":self.main,
"info":self.handleInfo
}[cmd](data)
except Exception as e:
print(e)
def on_error(self, wbsk, error):
print error
def on_close(self, wbsk):
print("Closed")
def on_open(self, wbsk):
self.sendJSON({"cmd": "join", "channel": self.room, "nick": self.botname})
self.sendMessage("Type '*help' for help.")
self.ping()
def run(self):
self.ws = websocket.WebSocketApp('wss://hack.chat/chat-ws', on_message = self.on_message, on_error = self.on_error, on_close = self.on_close)
self.ws.on_open = self.on_open
self.ws.run_forever(sslopt={"cert_reqs":ssl.CERT_NONE})
BotName = raw_input("Input Bot name and trip:: ")
Room = raw_input("Input room name:: ")
Bot = ChatBot(BotName, Room)
Bot.start()