mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-12 06:29:54 -05:00
test ID
This commit is contained in:
parent
5095780390
commit
dac8673607
@ -237,6 +237,14 @@ def letters_in_string(text):
|
||||
return False
|
||||
|
||||
|
||||
def is_number(n):
|
||||
try:
|
||||
float(n)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
"""Channel Functions"""
|
||||
|
||||
|
||||
|
||||
@ -4,39 +4,45 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
|
||||
|
||||
from sopel.tools import Identifier
|
||||
|
||||
from .Database import db as botdb
|
||||
from .Database import db as botdb,
|
||||
from .Tools import is_number
|
||||
|
||||
# TODO timestamp for new .seen
|
||||
|
||||
|
||||
class BotUsers():
|
||||
|
||||
def __init__(self):
|
||||
# TODO timestamp for new .seen
|
||||
self.all = botdb.get_bot_value('users') or dict()
|
||||
self.online = []
|
||||
self.offline = []
|
||||
for user_id in list(self.all.keys()):
|
||||
self.offline.append(int(user_id))
|
||||
self.current = {}
|
||||
self.dict = {
|
||||
"all": botdb.get_bot_value('users') or {},
|
||||
"online": [],
|
||||
"offline": [],
|
||||
"current": {},
|
||||
}
|
||||
"""during setup, all users from database are offline until marked online"""
|
||||
self.dict["offline"] = list(self.dict["all"].keys())
|
||||
|
||||
def whois(self, nick):
|
||||
nick_id = self.whois_ident(nick)
|
||||
self.add_to_all(nick, nick_id)
|
||||
self.add_to_current(nick, nick_id)
|
||||
return self.current[nick_id]["nick"]
|
||||
|
||||
def whois_id(self, nick_id):
|
||||
if nick_id in list(self.current.keys()):
|
||||
return self.current[nick_id]["nick"]
|
||||
elif nick_id in list(self.all.keys()) and len(self.all[nick_id]):
|
||||
return self.all[nick_id][0]
|
||||
def __getattr__(self, name):
|
||||
''' will only get called for undefined attributes '''
|
||||
"""We will try to find a dict value, or return None"""
|
||||
if name.lower() in list(self.dict.keys()):
|
||||
return self.dict[str(name).lower()]
|
||||
else:
|
||||
return None
|
||||
raise Exception('User dict does not contain a function or key ' + name.lower())
|
||||
|
||||
def whois_nick(self, nick):
|
||||
nick_id = self.whois_ident(nick)
|
||||
self.add_to_all(nick, nick_id)
|
||||
self.add_to_current(nick, nick_id)
|
||||
return nick_id
|
||||
def ID(self, nick):
|
||||
if is_number(nick):
|
||||
if nick in list(self.dict["current"].keys()):
|
||||
return self.dict["current"][nick]["nick"]
|
||||
elif nick in list(self.dict["all"].keys()) and len(self.dict["all"][nick]):
|
||||
return self.dict["all"][nick][0]
|
||||
else:
|
||||
raise Exception('ID ' + nick + ' does not appear to be associated with a nick')
|
||||
else:
|
||||
nick_id = self.whois_ident(nick)
|
||||
self.add_to_all(nick, nick_id)
|
||||
self.add_to_current(nick, nick_id)
|
||||
return self.dict["current"][nick_id]["nick"]
|
||||
|
||||
def whois_ident(self, nick):
|
||||
nick = Identifier(nick)
|
||||
@ -44,25 +50,26 @@ class BotUsers():
|
||||
return int(nick_id)
|
||||
|
||||
def save_user_db(self):
|
||||
botdb.set_bot_value('users', self.all)
|
||||
botdb.set_bot_value('users', self.dict["all"])
|
||||
|
||||
def add_to_all(self, nick, nick_id=None):
|
||||
if not nick_id:
|
||||
nick_id = self.whois_ident(nick)
|
||||
nick_id = self.ID(nick)
|
||||
# add to all if not there
|
||||
if nick_id not in list(self.all.keys()):
|
||||
self.all[nick_id] = []
|
||||
# add nick alias
|
||||
if nick not in self.all[nick_id]:
|
||||
self.all[nick_id].append(nick)
|
||||
if nick_id not in list(self.dict["all"].keys()):
|
||||
self.dict["all"][nick_id] = []
|
||||
# add nick alias as index 0
|
||||
if nick in self.dict["all"][nick_id]:
|
||||
self.dict["all"][nick_id].remove(nick)
|
||||
self.dict["all"][nick_id].insert(0, nick)
|
||||
self.save_user_db()
|
||||
|
||||
def add_to_current(self, nick, nick_id=None):
|
||||
if not nick_id:
|
||||
nick_id = self.whois_ident(nick)
|
||||
# add to current if not there
|
||||
if nick_id not in list(self.current.keys()):
|
||||
self.current[nick_id] = {"channels": [], "nick": nick}
|
||||
if nick_id not in list(self.dict["current"].keys()):
|
||||
self.dict["current"][nick_id] = {"channels": [], "nick": nick}
|
||||
|
||||
def channel_scan(self, bot):
|
||||
for channel in list(bot.channels.keys()):
|
||||
@ -74,15 +81,15 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(user, nick_id)
|
||||
# set current nick
|
||||
self.current[nick_id]["nick"] = user
|
||||
self.dict["current"][nick_id]["nick"] = user
|
||||
# add joined channel to nick list
|
||||
if channel not in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].append(channel)
|
||||
if channel not in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].append(channel)
|
||||
# mark user as online
|
||||
if nick_id not in self.online:
|
||||
self.online.append(int(nick_id))
|
||||
if nick_id in self.offline:
|
||||
self.offline.remove(nick_id)
|
||||
if nick_id not in self.dict["online"]:
|
||||
self.dict["online"].append(int(nick_id))
|
||||
if nick_id in self.dict["offline"]:
|
||||
self.dict["offline"].remove(nick_id)
|
||||
|
||||
def join(self, bot, trigger):
|
||||
if trigger.nick == bot.nick:
|
||||
@ -94,15 +101,15 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(user, nick_id)
|
||||
# set current nick
|
||||
self.current[nick_id]["nick"] = user
|
||||
self.dict["current"][nick_id]["nick"] = user
|
||||
# add joined channel to nick list
|
||||
if trigger.sender not in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].append(trigger.sender)
|
||||
if trigger.sender not in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].append(trigger.sender)
|
||||
# mark user as online
|
||||
if nick_id not in self.online:
|
||||
self.online.append(int(nick_id))
|
||||
if nick_id in self.offline:
|
||||
self.offline.remove(nick_id)
|
||||
if nick_id not in self.dict["online"]:
|
||||
self.dict["online"].append(int(nick_id))
|
||||
if nick_id in self.dict["offline"]:
|
||||
self.dict["offline"].remove(nick_id)
|
||||
return
|
||||
# Identify
|
||||
nick_id = self.whois_ident(trigger.nick)
|
||||
@ -111,15 +118,15 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(trigger.nick, nick_id)
|
||||
# set current nick
|
||||
self.current[nick_id]["nick"] = trigger.nick
|
||||
self.dict["current"][nick_id]["nick"] = trigger.nick
|
||||
# add joined channel to nick list
|
||||
if trigger.sender not in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].append(trigger.sender)
|
||||
if trigger.sender not in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].append(trigger.sender)
|
||||
# mark user as online
|
||||
if nick_id not in self.online:
|
||||
self.online.append(int(nick_id))
|
||||
if nick_id in self.offline:
|
||||
self.offline.remove(nick_id)
|
||||
if nick_id not in self.dict["online"]:
|
||||
self.dict["online"].append(int(nick_id))
|
||||
if nick_id in self.dict["offline"]:
|
||||
self.dict["offline"].remove(nick_id)
|
||||
|
||||
def quit(self, bot, trigger):
|
||||
if trigger.nick == bot.nick:
|
||||
@ -131,25 +138,25 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(trigger.nick, nick_id)
|
||||
# empty nicks channel list
|
||||
if trigger.sender in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].remove(trigger.sender)
|
||||
self.current[nick_id]["channels"] = []
|
||||
if trigger.sender in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].remove(trigger.sender)
|
||||
self.dict["current"][nick_id]["channels"] = []
|
||||
# mark user as offline
|
||||
if nick_id in self.online:
|
||||
self.online.remove(nick_id)
|
||||
if nick_id not in self.offline:
|
||||
self.offline.append(int(nick_id))
|
||||
if nick_id in self.dict["online"]:
|
||||
self.dict["online"].remove(nick_id)
|
||||
if nick_id not in self.dict["offline"]:
|
||||
self.dict["offline"].append(int(nick_id))
|
||||
|
||||
def part(self, bot, trigger):
|
||||
if trigger.nick == bot.nick:
|
||||
for nick_id in list(self.current.keys()):
|
||||
if trigger.sender in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].remove(trigger.sender)
|
||||
for nick_id in list(self.dict["current"].keys()):
|
||||
if trigger.sender in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].remove(trigger.sender)
|
||||
# mark offline
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id in self.online:
|
||||
self.online.remove(nick_id)
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline:
|
||||
self.offline.append(int(nick_id))
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
|
||||
self.dict["online"].remove(nick_id)
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
|
||||
self.dict["offline"].append(int(nick_id))
|
||||
return
|
||||
# Identify
|
||||
nick_id = self.whois_ident(trigger.nick)
|
||||
@ -158,25 +165,25 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(trigger.nick, nick_id)
|
||||
# remove channel from nick list
|
||||
if trigger.sender in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].remove(trigger.sender)
|
||||
if trigger.sender in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].remove(trigger.sender)
|
||||
# mark offline
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id in self.online:
|
||||
self.online.remove(nick_id)
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline:
|
||||
self.offline.append(int(nick_id))
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
|
||||
self.dict["online"].remove(nick_id)
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
|
||||
self.dict["offline"].append(int(nick_id))
|
||||
|
||||
def kick(self, bot, trigger):
|
||||
targetnick = Identifier(str(trigger.args[1]))
|
||||
if targetnick == bot.nick:
|
||||
for nick_id in list(self.current.keys()):
|
||||
if trigger.sender in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].remove(trigger.sender)
|
||||
for nick_id in list(self.dict["current"].keys()):
|
||||
if trigger.sender in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].remove(trigger.sender)
|
||||
# mark offline
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id in self.online:
|
||||
self.online.remove(nick_id)
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline:
|
||||
self.offline.append(int(nick_id))
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
|
||||
self.dict["online"].remove(nick_id)
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
|
||||
self.dict["offline"].append(int(nick_id))
|
||||
return
|
||||
# Identify
|
||||
nick_id = self.whois_ident(targetnick)
|
||||
@ -185,13 +192,13 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(targetnick, nick_id)
|
||||
# remove channel from nick list
|
||||
if trigger.sender in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].remove(trigger.sender)
|
||||
if trigger.sender in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].remove(trigger.sender)
|
||||
# mark offline
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id in self.online:
|
||||
self.online.remove(nick_id)
|
||||
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline:
|
||||
self.offline.append(int(nick_id))
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
|
||||
self.dict["online"].remove(nick_id)
|
||||
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
|
||||
self.dict["offline"].append(int(nick_id))
|
||||
|
||||
def nick(self, bot, trigger):
|
||||
newnick = Identifier(trigger)
|
||||
@ -207,15 +214,15 @@ class BotUsers():
|
||||
# Verify nick is in the all list
|
||||
self.add_to_current(newnick, nick_id)
|
||||
# set current nick
|
||||
self.current[nick_id]["nick"] = newnick
|
||||
self.dict["current"][nick_id]["nick"] = newnick
|
||||
# add joined channel to nick list
|
||||
if trigger.sender not in self.current[nick_id]["channels"]:
|
||||
self.current[nick_id]["channels"].append(trigger.sender)
|
||||
if trigger.sender not in self.dict["current"][nick_id]["channels"]:
|
||||
self.dict["current"][nick_id]["channels"].append(trigger.sender)
|
||||
# mark user as online
|
||||
if nick_id not in self.online:
|
||||
self.online.append(int(nick_id))
|
||||
if nick_id in self.offline:
|
||||
self.offline.remove(nick_id)
|
||||
if nick_id not in self.dict["online"]:
|
||||
self.dict["online"].append(int(nick_id))
|
||||
if nick_id in self.dict["offline"]:
|
||||
self.dict["offline"].remove(nick_id)
|
||||
|
||||
def mode(self, bot, trigger):
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user