Added Tell plugin to server management bot

This commit is contained in:
Rob Nelson
2013-12-20 11:09:15 -08:00
parent 2dec12e74b
commit 7657efd85e
6 changed files with 135 additions and 25 deletions

1
.gitignore vendored
View File

@@ -20,3 +20,4 @@ info.json
.metadata
/bot/config.yml
/bot/data

View File

@@ -26,12 +26,12 @@ class NudgePlugin(IPlugin):
thread.start_new_thread(self.nudge_listener, ())
def OnShaddap(self, event):
def OnShaddap(self, event, args):
self.dropNudges = True
self.bot.notice(event.source.nick, 'Now dropping nudges.')
return True
def OnSpeak(self, event):
def OnSpeak(self, event, args):
self.dropNudges = False
self.bot.notice(event.source.nick, 'No longer dropping nudges.')
return True

84
bot/plugins/Tell.py Normal file
View File

@@ -0,0 +1,84 @@
# Inspired by Skilibliaasdadas's bot.
# nt, tell nickname something
# nt, received
from vgstation.common.plugin import IPlugin, Plugin
import vgstation.common.config as globalConfig
@Plugin
class TellPlugin(IPlugin):
def __init__(self, bot):
IPlugin.__init__(self, bot)
# Recipient => Messages ({from,message})
self.data = {}
self.LoadPluginData()
# {from,to,message}
self.lastMessages = []
self.RegisterCommand('tell', self.OnTell, help='Leave a message for someone.')
self.RegisterCommand('received', self.OnReceived, help='Bot will mark messages sent to you as read.')
self.RegisterCommand('messages', self.OnMessages, help='Rattle off the messages sent to you.')
self.RegisterCommand('belay', self.OnBelay, help='Remove last message you sent.')
def OnTell(self, event, args):
channel = event.target
nick = event.source.nick
if len(args) < 3:
self.bot.notice(nick,'The format is: bot, tell NICKNAME MESSAGE TO SEND')
msg = ' '.join(args[2:])
to = args[1]
message = {'from':nick, 'to':to, 'message':msg}
self.lastMessages += [message]
if to not in self.data:
self.data[to] = []
else:
if len(self.data[to]) == 5:
self.bot.privmsg(channel, '{to} has too many messages. They need to use the received command before I can add more.'.format(**message))
return True
self.data[to] += [message]
self.SavePluginData()
self.bot.privmsg(channel, 'Your message has been sent. It will be displayed the next time {to} joins or uses messages.'.format(**message))
return True
def OnReceived(self, event, args):
channel = event.target
nick = event.source.nick
self.data[nick] = []
self.SavePluginData()
self.bot.privmsg(channel, 'Your messages have been cleared.')
return True
def OnBelay(self, event, args):
channel = event.target
nick = event.source.nick
lm = None
for m in self.lastMessages:
if m['from'] == nick:
lm = m
if lm is not None:
self.data[lm['to']].remove(lm)
self.bot.privmsg(channel, 'Your message to {to} was removed.'.format(**lm))
self.SavePluginData()
return True
def OnJoin(self, channel, nick):
self.SendMessages(channel, nick)
return False # Let other plugins use it.
def OnMessages(self, event, args):
channel = event.target
nick = event.source.nick
self.SendMessages(channel,nick)
return True
def SendMessages(self,channel,nick):
if nick in self.data:
if len(self.data[nick]) > 0:
self.bot.privmsg(channel, '{0}, you have {1} messages. Say "{2}, received" to clear them.'.format(nick, len(self.data[nick]), globalConfig.get('names', ['nt'])[0]))
for message in self.data[nick]:
self.bot.privmsg(channel, '{from}: {message}'.format(**message))

View File

@@ -19,6 +19,26 @@ class Bot(irc.bot.SingleServerIRCBot):
self.connection.add_global_handler(i, getattr(self, "on_" + i), 0)
self.welcomeReceived = False
self.messageQueue = []
self.connection.execute_every(1, self.SendQueuedMessage)
def SendQueuedMessage(self):
if len(self.messageQueue) == 0: return
msg = self.messageQueue[0]
msgtype, target, message = msg
logging.info('{0} -> {1}: {2}'.format(msgtype, target, self.stripUnprintable(message)))
if msgtype == 'PRIVMSG':
self.connection.privmsg(target, message)
elif msgtype == 'NOTICE':
self.connection.notice(target, message)
self.messageQueue.remove(msg)
def on_join(self, c, e):
ch = e.target
nick = e.source.nick
for plugin in self.plugins:
if plugin.OnJoin(ch, nick): break
def on_ping(self, c, e):
for plugin in self.plugins:
if plugin.OnPing(): break
@@ -67,27 +87,29 @@ class Bot(irc.bot.SingleServerIRCBot):
return filter(lambda x: x in string.printable, msg)
def notice(self, nick, message):
logging.info('NOTICE -> {0}: {1}'.format(nick, self.stripUnprintable(message)))
self.connection.notice(nick, message)
self.messageQueue += [('NOTICE', nick, message)]
# self.connection.notice(nick, message)
def privmsg(self, nick, message):
logging.info('PRIVMSG -> {0}: {1}'.format(nick, self.stripUnprintable(message)))
self.connection.privmsg(nick, message)
self.messageQueue += [('PRIVMSG', nick, message)]
# self.connection.privmsg(nick, message)
def do_command(self, e, cmd):
nick = e.source.nick
channel = nick
if e.target:
channel = e.target
args = cmd.split(' ')
cmd = args[0]
c = self.connection
if cmd == 'help':
self.privmsg(nick, '-- VGBot 1.0 Help: (All commands are accessed with {0}, <command> [args]'.format(c.get_nickname()))
for name in self.command:
for name in sorted(self.command.keys()):
self.privmsg(nick, ' {0}: {1}'.format(name, self.command[name].get('help', 'No help= argument for this command.')))
elif cmd == 'version':
self.notice(channel, 'VGBot 1.0 - By N3X15') # pls to not change
elif cmd in self.command:
self.command[cmd]['handler'](e)
self.command[cmd]['handler'](e, args)
else:
self.notice(channel, 'I don\'t know that command, sorry. Say "{0}, help" for available commands.'.format(c.get_nickname()))

View File

@@ -47,7 +47,7 @@ class IPlugin(object):
self.datadir = os.path.join('data')
if not os.path.isdir(self.datadir):
os.makedirs(self.datadir)
self.datafile = os.path.join(self.datadir, 'redmine.yml')
self.datafile = os.path.join(self.datadir, self.__class__.__name__+'.yml')
if os.path.isfile(self.datafile):
with open(self.datafile, 'r') as stream:
self.data = yaml.load(stream)
@@ -68,3 +68,6 @@ class IPlugin(object):
def OnPing(self):
return False # Not handled
def OnJoin(self,channel,nick):
return False # Not handled