added callbacks for whois idle time, .updick works on others now

This commit is contained in:
iou1name 2018-05-29 17:41:47 -04:00
parent 4140f52c69
commit 76ad41ec48
2 changed files with 42 additions and 2 deletions

22
bot.py
View File

@ -476,6 +476,18 @@ class Fulvia(irc.IRCClient):
self.realname = realname
def whoisIdle(self, nick, idle, signon):
"""
Called when the bot receives a WHOIS about a particular user.
nick - nickname of the user
idle - seconds since last activity from user
signon - unix timestamp when user signed on
"""
if self.memory.get("idle_callbacks"):
self.memory["idle_callbacks"][nick].callback((nick, idle, signon))
self.memory["idle_callbacks"].pop(nick)
## User commands, from client->server
def msg(self, user, message, length=None):
@ -570,12 +582,20 @@ class Fulvia(irc.IRCClient):
def irc_RPL_WHOISUSER(self, prefix, params):
"""
Called when we receive the server's response from our "WHOIS [user]"
query.
query. Contains hostmask information.
"""
_, nick, ident, host, _, realname = params
self.whoisUser(nick, ident, host, realname)
def irc_RPL_WHOISIDLE(self, prefix, params):
"""
Called when we receive the server's response from our "WHOIS [user]"
query. Contains idle and signon time information.
"""
_, nick, idle, signon, _ = params
self.whoisIdle(nick, idle, signon)
class FulviaWrapper():
"""

View File

@ -4,12 +4,15 @@ How long the bot has been running.
"""
import datetime
from twisted.internet import defer
from module import commands
def setup(bot):
if "uptime" not in bot.memory:
bot.memory["uptime"] = datetime.datetime.now()
bot.memory["idle_callbacks"] = {}
@commands('uptime')
@ -23,5 +26,22 @@ def uptime(bot, trigger):
@commands('updick')
def updick(bot, trigger):
""".updick - Returns the uptime of Fulvia, measured in dicks."""
delta = datetime.datetime.now() - bot.memory["uptime"]
if trigger.group(2):
if trigger.group(2) in bot.users:
d = defer.Deferred()
d.addCallback(idleTime, bot)
bot.memory["idle_callbacks"][trigger.group(2)] = d
bot.whois(trigger.group(2))
else:
delta = datetime.datetime.now() - bot.memory["uptime"]
bot.msg("8" + "="*delta.days + "D")
def idleTime(result, bot):
"""
result - tuple containing (nick, idle, signon)
"""
nick, idle, signon = result
t = datetime.datetime.fromtimestamp(int(signon))
delta = datetime.datetime.now() - t
bot.msg("8" + "="*delta.days + "D")