Merge pull request #57 from deathbybandaid/development

Development
This commit is contained in:
Deathbybandaid 2019-06-11 08:13:29 -04:00 committed by GitHub
commit 0595396e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 94 deletions

View File

@ -12,6 +12,7 @@ import threading
from .Config import config as botconfig from .Config import config as botconfig
from .Database import db as botdb from .Database import db as botdb
from .Users import users as botusers
class SpiceBot_Channels_MainSection(StaticSection): class SpiceBot_Channels_MainSection(StaticSection):
@ -33,6 +34,31 @@ class BotChannels():
"InitialProcess": False "InitialProcess": False
} }
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:
raise Exception('Channel dict does not contain a function or key ' + str(name.lower()))
def get_channel_users(self, channel, idtype="nick"):
if channel.lower() not in list(self.dict["list"].keys()):
raise Exception('Channel ' + str(channel.lower()) + " does not seem to be on this server")
if 'users' not in list(self.dict["list"][channel.lower()].keys()):
raise Exception('Channel ' + str(channel.lower()) + " does not seem to be a channel the bot is in")
nick_ids = self.dict["list"][channel.lower()]['users']
if idtype.upper() == "ID":
return nick_ids
else:
nick_list = []
for nick_id in nick_ids:
nickname = botusers.ID(nick_id)
nick_list.append(nickname)
return nick_list
def setup_channels(self): def setup_channels(self):
botconfig.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) botconfig.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False)
@ -103,6 +129,7 @@ class BotChannels():
return nick_id return nick_id
def add_to_channel(self, channel, nick, nick_id=None): def add_to_channel(self, channel, nick, nick_id=None):
channel = str(channel)
if not nick_id: if not nick_id:
nick_id = self.whois_ident(nick) nick_id = self.whois_ident(nick)
if channel.lower() not in list(self.dict['list'].keys()): if channel.lower() not in list(self.dict['list'].keys()):
@ -113,6 +140,7 @@ class BotChannels():
self.dict['list'][channel.lower()]['users'].append(int(nick_id)) self.dict['list'][channel.lower()]['users'].append(int(nick_id))
def remove_from_channel(self, channel, nick, nick_id=None): def remove_from_channel(self, channel, nick, nick_id=None):
channel = str(channel)
if not nick_id: if not nick_id:
nick_id = self.whois_ident(nick) nick_id = self.whois_ident(nick)
if channel.lower() not in list(self.dict['list'].keys()): if channel.lower() not in list(self.dict['list'].keys()):
@ -123,6 +151,7 @@ class BotChannels():
self.dict['list'][channel.lower()]['users'].remove(nick_id) self.dict['list'][channel.lower()]['users'].remove(nick_id)
def remove_all_from_channel(self, channel): def remove_all_from_channel(self, channel):
channel = str(channel)
if channel.lower() not in list(self.dict['list'].keys()): if channel.lower() not in list(self.dict['list'].keys()):
self.dict['list'][channel.lower()] = dict() self.dict['list'][channel.lower()] = dict()
if 'users' not in self.dict['list'][channel.lower()]: if 'users' not in self.dict['list'][channel.lower()]:

View File

@ -237,6 +237,14 @@ def letters_in_string(text):
return False return False
def is_number(n):
try:
float(n)
except ValueError:
return False
return True
"""Channel Functions""" """Channel Functions"""

View File

@ -5,38 +5,45 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
from sopel.tools import Identifier 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(): class BotUsers():
def __init__(self): def __init__(self):
# TODO timestamp for new .seen self.dict = {
self.all = botdb.get_bot_value('users') or dict() "all": botdb.get_bot_value('users') or {},
self.online = [] "online": [],
self.offline = [] "offline": [],
for user_id in list(self.all.keys()): "current": {},
self.offline.append(int(user_id)) }
self.current = {} """during setup, all users from database are offline until marked online"""
for user_id in list(self.dict["all"].keys()):
self.dict["offline"].append(int(user_id))
def whois(self, nick): def __getattr__(self, name):
nick_id = self.whois_ident(nick) ''' will only get called for undefined attributes '''
self.add_to_all(nick, nick_id) """We will try to find a dict value, or return None"""
self.add_to_current(nick, nick_id) if name.lower() in list(self.dict.keys()):
return self.current[nick_id]["nick"] return self.dict[str(name).lower()]
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]
else: else:
return None raise Exception('User dict does not contain a function or key ' + str(name.lower()))
def whois_nick(self, nick): def ID(self, nickinput):
nick_id = self.whois_ident(nick) if is_number(nickinput):
self.add_to_all(nick, nick_id) nick_id = nickinput
self.add_to_current(nick, nick_id) if nick_id in list(self.dict["current"].keys()):
return nick_id nick = self.dict["current"][nick_id]["nick"]
elif nick_id in list(self.dict["all"].keys()) and len(self.dict["all"][nick_id]):
nick = self.dict["all"][nick_id][0]
else:
raise Exception('ID ' + str(nickinput) + ' does not appear to be associated with a nick')
return nick
else:
nick_id = self.whois_ident(nickinput)
return nick_id
def whois_ident(self, nick): def whois_ident(self, nick):
nick = Identifier(nick) nick = Identifier(nick)
@ -44,25 +51,26 @@ class BotUsers():
return int(nick_id) return int(nick_id)
def save_user_db(self): 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): def add_to_all(self, nick, nick_id=None):
if not nick_id: if not nick_id:
nick_id = self.whois_ident(nick) nick_id = self.ID(nick)
# add to all if not there # add to all if not there
if nick_id not in list(self.all.keys()): if nick_id not in list(self.dict["all"].keys()):
self.all[nick_id] = [] self.dict["all"][nick_id] = []
# add nick alias # add nick alias as index 0
if nick not in self.all[nick_id]: if nick in self.dict["all"][nick_id]:
self.all[nick_id].append(nick) self.dict["all"][nick_id].remove(nick)
self.dict["all"][nick_id].insert(0, nick)
self.save_user_db() self.save_user_db()
def add_to_current(self, nick, nick_id=None): def add_to_current(self, nick, nick_id=None):
if not nick_id: if not nick_id:
nick_id = self.whois_ident(nick) nick_id = self.whois_ident(nick)
# add to current if not there # add to current if not there
if nick_id not in list(self.current.keys()): if nick_id not in list(self.dict["current"].keys()):
self.current[nick_id] = {"channels": [], "nick": nick} self.dict["current"][nick_id] = {"channels": [], "nick": nick}
def channel_scan(self, bot): def channel_scan(self, bot):
for channel in list(bot.channels.keys()): for channel in list(bot.channels.keys()):
@ -74,15 +82,15 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(user, nick_id) self.add_to_current(user, nick_id)
# set current nick # set current nick
self.current[nick_id]["nick"] = user self.dict["current"][nick_id]["nick"] = user
# add joined channel to nick list # add joined channel to nick list
if channel not in self.current[nick_id]["channels"]: if channel not in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].append(channel) self.dict["current"][nick_id]["channels"].append(channel)
# mark user as online # mark user as online
if nick_id not in self.online: if nick_id not in self.dict["online"]:
self.online.append(int(nick_id)) self.dict["online"].append(int(nick_id))
if nick_id in self.offline: if nick_id in self.dict["offline"]:
self.offline.remove(nick_id) self.dict["offline"].remove(nick_id)
def join(self, bot, trigger): def join(self, bot, trigger):
if trigger.nick == bot.nick: if trigger.nick == bot.nick:
@ -94,15 +102,15 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(user, nick_id) self.add_to_current(user, nick_id)
# set current nick # set current nick
self.current[nick_id]["nick"] = user self.dict["current"][nick_id]["nick"] = user
# add joined channel to nick list # add joined channel to nick list
if trigger.sender not in self.current[nick_id]["channels"]: if trigger.sender not in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].append(trigger.sender) self.dict["current"][nick_id]["channels"].append(trigger.sender)
# mark user as online # mark user as online
if nick_id not in self.online: if nick_id not in self.dict["online"]:
self.online.append(int(nick_id)) self.dict["online"].append(int(nick_id))
if nick_id in self.offline: if nick_id in self.dict["offline"]:
self.offline.remove(nick_id) self.dict["offline"].remove(nick_id)
return return
# Identify # Identify
nick_id = self.whois_ident(trigger.nick) nick_id = self.whois_ident(trigger.nick)
@ -111,15 +119,15 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id) self.add_to_current(trigger.nick, nick_id)
# set current nick # set current nick
self.current[nick_id]["nick"] = trigger.nick self.dict["current"][nick_id]["nick"] = trigger.nick
# add joined channel to nick list # add joined channel to nick list
if trigger.sender not in self.current[nick_id]["channels"]: if trigger.sender not in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].append(trigger.sender) self.dict["current"][nick_id]["channels"].append(trigger.sender)
# mark user as online # mark user as online
if nick_id not in self.online: if nick_id not in self.dict["online"]:
self.online.append(int(nick_id)) self.dict["online"].append(int(nick_id))
if nick_id in self.offline: if nick_id in self.dict["offline"]:
self.offline.remove(nick_id) self.dict["offline"].remove(nick_id)
def quit(self, bot, trigger): def quit(self, bot, trigger):
if trigger.nick == bot.nick: if trigger.nick == bot.nick:
@ -131,25 +139,25 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id) self.add_to_current(trigger.nick, nick_id)
# empty nicks channel list # empty nicks channel list
if trigger.sender in self.current[nick_id]["channels"]: if trigger.sender in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].remove(trigger.sender) self.dict["current"][nick_id]["channels"].remove(trigger.sender)
self.current[nick_id]["channels"] = [] self.dict["current"][nick_id]["channels"] = []
# mark user as offline # mark user as offline
if nick_id in self.online: if nick_id in self.dict["online"]:
self.online.remove(nick_id) self.dict["online"].remove(nick_id)
if nick_id not in self.offline: if nick_id not in self.dict["offline"]:
self.offline.append(int(nick_id)) self.dict["offline"].append(int(nick_id))
def part(self, bot, trigger): def part(self, bot, trigger):
if trigger.nick == bot.nick: if trigger.nick == bot.nick:
for nick_id in list(self.current.keys()): for nick_id in list(self.dict["current"].keys()):
if trigger.sender in self.current[nick_id]["channels"]: if trigger.sender in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].remove(trigger.sender) self.dict["current"][nick_id]["channels"].remove(trigger.sender)
# mark offline # mark offline
if not len(self.current[nick_id]["channels"]) and nick_id in self.online: if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.online.remove(nick_id) self.dict["online"].remove(nick_id)
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline: if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.offline.append(int(nick_id)) self.dict["offline"].append(int(nick_id))
return return
# Identify # Identify
nick_id = self.whois_ident(trigger.nick) nick_id = self.whois_ident(trigger.nick)
@ -158,25 +166,25 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id) self.add_to_current(trigger.nick, nick_id)
# remove channel from nick list # remove channel from nick list
if trigger.sender in self.current[nick_id]["channels"]: if trigger.sender in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].remove(trigger.sender) self.dict["current"][nick_id]["channels"].remove(trigger.sender)
# mark offline # mark offline
if not len(self.current[nick_id]["channels"]) and nick_id in self.online: if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.online.remove(nick_id) self.dict["online"].remove(nick_id)
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline: if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.offline.append(int(nick_id)) self.dict["offline"].append(int(nick_id))
def kick(self, bot, trigger): def kick(self, bot, trigger):
targetnick = Identifier(str(trigger.args[1])) targetnick = Identifier(str(trigger.args[1]))
if targetnick == bot.nick: if targetnick == bot.nick:
for nick_id in list(self.current.keys()): for nick_id in list(self.dict["current"].keys()):
if trigger.sender in self.current[nick_id]["channels"]: if trigger.sender in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].remove(trigger.sender) self.dict["current"][nick_id]["channels"].remove(trigger.sender)
# mark offline # mark offline
if not len(self.current[nick_id]["channels"]) and nick_id in self.online: if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.online.remove(nick_id) self.dict["online"].remove(nick_id)
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline: if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.offline.append(int(nick_id)) self.dict["offline"].append(int(nick_id))
return return
# Identify # Identify
nick_id = self.whois_ident(targetnick) nick_id = self.whois_ident(targetnick)
@ -185,13 +193,13 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(targetnick, nick_id) self.add_to_current(targetnick, nick_id)
# remove channel from nick list # remove channel from nick list
if trigger.sender in self.current[nick_id]["channels"]: if trigger.sender in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].remove(trigger.sender) self.dict["current"][nick_id]["channels"].remove(trigger.sender)
# mark offline # mark offline
if not len(self.current[nick_id]["channels"]) and nick_id in self.online: if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.online.remove(nick_id) self.dict["online"].remove(nick_id)
if not len(self.current[nick_id]["channels"]) and nick_id not in self.offline: if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.offline.append(int(nick_id)) self.dict["offline"].append(int(nick_id))
def nick(self, bot, trigger): def nick(self, bot, trigger):
newnick = Identifier(trigger) newnick = Identifier(trigger)
@ -207,18 +215,21 @@ class BotUsers():
# Verify nick is in the all list # Verify nick is in the all list
self.add_to_current(newnick, nick_id) self.add_to_current(newnick, nick_id)
# set current nick # set current nick
self.current[nick_id]["nick"] = newnick self.dict["current"][nick_id]["nick"] = newnick
# add joined channel to nick list # add joined channel to nick list
if trigger.sender not in self.current[nick_id]["channels"]: if trigger.sender not in self.dict["current"][nick_id]["channels"]:
self.current[nick_id]["channels"].append(trigger.sender) self.dict["current"][nick_id]["channels"].append(trigger.sender)
# mark user as online # mark user as online
if nick_id not in self.online: if nick_id not in self.dict["online"]:
self.online.append(int(nick_id)) self.dict["online"].append(int(nick_id))
if nick_id in self.offline: if nick_id in self.dict["offline"]:
self.offline.remove(nick_id) self.dict["offline"].remove(nick_id)
def mode(self, bot, trigger): def mode(self, bot, trigger):
return return
def targetcheck(self):
return True
users = BotUsers() users = BotUsers()