Merge pull request #12 from HarHar/master

Various fixes for ircBot
This commit is contained in:
Uriziel 2013-01-24 13:24:52 -08:00
commit 95d698a5ed

View File

@ -5,6 +5,7 @@
import socket import socket
import threading import threading
from syncplay import utils from syncplay import utils
from time import sleep
class Bot(object): class Bot(object):
def __init__(self, server='irc.rizon.net', serverPassword='', port=6667, nick='SyncBot', nickservPass='', channel='', channelPassword='', functions=[]): def __init__(self, server='irc.rizon.net', serverPassword='', port=6667, nick='SyncBot', nickservPass='', channel='', channelPassword='', functions=[]):
@ -17,10 +18,10 @@ class Bot(object):
# channel - channel to autojoin and interact with # channel - channel to autojoin and interact with
# channelPassword - if channel is +k # channelPassword - if channel is +k
# functions - list/tuple of functions that can be used from the bot: # functions - list/tuple of functions that can be used from the bot:
# * pause(setBy, room, state=bool) # * pause(setBy, state=bool)
# * getRooms() -> list # * getRooms() -> list
# * getPosition(room) -> int # * getPosition(room) -> int
# * setPosition(setBy, room, seconds) # * setPosition(setBy, seconds)
# * getUsers(room) -> list of {'nick': str, 'file': str, 'length': int} # * getUsers(room) -> list of {'nick': str, 'file': str, 'length': int}
# * isPaused(room) -> bool # * isPaused(room) -> bool
@ -64,6 +65,8 @@ class Bot(object):
self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has paused (room ' + room + ')') self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has paused (room ' + room + ')')
def sp_fileplaying(self, who, filename, room): #for when syncplay knows what filename is being played def sp_fileplaying(self, who, filename, room): #for when syncplay knows what filename is being played
self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' is playing "' + filename + '" (room ' + room + ')') self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' is playing "' + filename + '" (room ' + room + ')')
def sp_seek(self, who, fromTime, toTime, room):
self.msg(self.channel, chr(2) + '<' + who + '>'+ chr(15) +' has jumped from ' + utils.formatTime(fromTime) + ' to ' + utils.formatTime(toTime) +' (room ' + room + ')')
################################## ##################################
def sockSend(self, s): def sockSend(self, s):
@ -83,6 +86,7 @@ class Bot(object):
self.sockSend('NICK ' + newnick) self.sockSend('NICK ' + newnick)
self.nick = newnick self.nick = newnick
def irc_onMsg(self, nickFrom, host, to, msg): def irc_onMsg(self, nickFrom, host, to, msg):
sleep(0.5)
if to[0] == '#': #channel if to[0] == '#': #channel
split = msg.split(' ') split = msg.split(' ')
@ -123,45 +127,33 @@ class Bot(object):
else: else:
out += chr(3) + '2' + user['nick'] + chr(15) + ', ' out += chr(3) + '2' + user['nick'] + chr(15) + ', '
i += 1 i += 1
self.msg(nickFrom, to, out) self.msg(to, out)
else: else:
self.msg(to, chr(2) + 'Usage:' + chr(15) + ' !roominfo [room]') self.msg(to, chr(2) + 'Usage:' + chr(15) + ' !roominfo [room]')
elif split[0].lower() == '!pause': elif split[0].lower() == '!pause':
if len(split) >= 2: rooms = self.functions[1]()
rooms = self.functions[1]()
for room in split[1:]:
if (room in rooms) == False:
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Room does not exists (' + room + ')')
else:
users = self.functions[4](room)
for u in users:
if u['nick'] == nickFrom:
break
else:
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick is not in the specified room')
continue
self.functions[6](nickFrom, room, True) for room in rooms:
else: users = self.functions[4](room)
self.msg(to, chr(2) + 'Usage:' + chr(15) + ' !pause [room]') for u in users:
if u['nick'] == nickFrom:
self.functions[6](nickFrom, True)
break
else:
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick was not found on the server')
return
elif split[0].lower() == '!play': elif split[0].lower() == '!play':
if len(split) >= 2: rooms = self.functions[1]()
rooms = self.functions[1]()
for room in split[1:]: for room in rooms:
if (room in rooms) == False: users = self.functions[4](room)
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Room does not exists (' + room + ')') for u in users:
else: if u['nick'] == nickFrom:
users = self.functions[4](room) self.functions[6](nickFrom, False)
for u in users: break
if u['nick'] == nickFrom: else:
break self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick was not found on the server')
else: return
self.msg(to, chr(3) + '5Error!' + chr(15) + ' Your nick is not in the specified room')
continue
self.functions[6](room, False)
else:
self.msg(to, chr(2) + 'Usage:' + chr(15) + ' !play [room]')
elif split[0].lower() == '!help': elif split[0].lower() == '!help':
self.msg(to, chr(2) + 'Available commands:' + chr(15) + ' !rooms / !roominfo [room] / !pause / !play') self.msg(to, chr(2) + 'Available commands:' + chr(15) + ' !rooms / !roominfo [room] / !pause / !play')