forked from TheMineWizard/trukbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·107 lines (84 loc) · 2.88 KB
/
main.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
#!/usr/bin/env python
import re, socket, json, threading
from ConfigParser import RawConfigParser
from modules import lastfm, tf2lobby
commands = {}
parser = RawConfigParser()
np, lobbyparser = lastfm.NowPlaying(), tf2lobby.lobbyParser()
parser.read('botcfg.cfg')
botnick, password, streamname, lastfmuser, lastfmapikey, owner = parser.get('settings', 'botnick'), parser.get('settings', 'password'), parser.get('settings', 'streamname'), parser.get('settings', 'lastfmuser'), parser.get('settings', 'lastfmapikey'), parser.get('settings', 'owner')
channel = "#%s" % (streamname)
picks = 0
def connect(server, port):
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, port))
ircsock.send("Pass %s\n" % (password))
ircsock.send("NICK %s\n" % (botnick))
ircsock.send("JOIN %s\n" % (channel))
global ircsock
def doTF2Lobby(msg):
lobbymain = lobbyparser.main(msg)
lobbyname, mapname, missingclasses, numpeople, maxpeople = lobbymain
sendmsg("Lobby name: %s, map: %s, number of people: %s/%s" % (lobbyname, mapname, numpeople, maxpeople))
sendmsg("Missing classes: %s" % (missingclasses))
def doLastFM():
npmain = np.main(lastfmuser, lastfmapikey)
artistName, trackName, albumName = npmain
sendmsg("Now playing: %s - %s (album: %s)" % (artistName, trackName, albumName))
def DispenserPicks():
global picks
picks += 1
sendmsg("Dispenser picks: %s" % (picks))
def sendmsg(message):
ircsock.send('PRIVMSG %s :%s\n' % (channel, message))
def dict():
cmdfile = file('commands.json', 'r').read()
#cmdfile = open("commands.txt")
""" print cmdfile
for line in cmdfile:
(key, val) = line.split('=')
commands[(key)] = val
print commands.keys()
print commands.values()
"""
global commands
commands = json.loads(cmdfile)
def cmds(nick, msg, sendmsg):
for x in commands["commands"]:
if re.search(x, msg, re.I):
sendmsg(commands["commands"].get(x))
for x in commands["execs"]:
if re.search(x, msg, re.I):
exec(commands["execs"].get(x))
for x in commands["owner_commands"]:
if nick in owner:
if re.search(x, msg, re.I):
exec(commands["owner_commands"].get(x))
#def triggers(msg, sendmsg):
# for x in commands["triggers"]:
# if re.search(x, msg, re.I):
# sendmsg(commands["triggers"].get(x))
def main():
dict()
try:
connect("199.9.253.210", 6667)
except Exception as e:
print e
while True:
ircmsg = ircsock.recv(1024)
ircmsg = ircmsg.strip('\r\n')
if not ircmsg:
try:
ircsock.close()
connect(server, 6667)
except Exception as e:
print e
print ircmsg
if ircmsg.find('PING ') != -1:
ircsock.send('PING :PONG\n')
if ircmsg.find(' PRIVMSG ') != -1:
nick = ircmsg.split('!')[0][1:]
msg = ircmsg.split(' PRIVMSG ')[-1].split(' :')[1].lower()
cmds(nick, msg, sendmsg)
# triggers(msg, sendmsg)
main()