Change the database to give channels and users ids and use those in the settings
tables
This commit is contained in:
parent
88eb7b695f
commit
9a3f2e5de6
6 changed files with 172 additions and 131 deletions
194
Database.py
194
Database.py
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
import json, os, sqlite3, threading, time
|
import json, os, sqlite3, threading, time
|
||||||
|
|
||||||
class Table(object):
|
class Table(object):
|
||||||
|
@ -26,6 +25,34 @@ class Servers(Table):
|
||||||
server_id=?""",
|
server_id=?""",
|
||||||
[id])
|
[id])
|
||||||
|
|
||||||
|
class Channels(Table):
|
||||||
|
def add(self, server_id, name):
|
||||||
|
self.database.execute("""INSERT OR IGNORE INTO channels
|
||||||
|
(server_id, name) VALUES (?, ?)""",
|
||||||
|
[server_id, name.lower()])
|
||||||
|
def delete(self, channel_id):
|
||||||
|
self.database.execute("DELETE FROM channels WHERE channel_id=?",
|
||||||
|
[channel_id])
|
||||||
|
def get_id(self, server_id, name):
|
||||||
|
value = self.database.execute_fetchone("""SELECT channel_id FROM
|
||||||
|
channels WHERE server_id=? AND name=?""",
|
||||||
|
[server_id, name.lower()])
|
||||||
|
return value if value == None else value[0]
|
||||||
|
|
||||||
|
class Users(Table):
|
||||||
|
def add(self, server_id, nickname):
|
||||||
|
self.database.execute("""INSERT OR IGNORE INTO users
|
||||||
|
(server_id, nickname) VALUES (?, ?)""",
|
||||||
|
[server_id, nickname.lower()])
|
||||||
|
def delete(self, user_id):
|
||||||
|
self.database.execute("DELETE FROM users WHERE user_id=?",
|
||||||
|
[user_id])
|
||||||
|
def get_id(self, server_id, nickname):
|
||||||
|
value = self.database.execute_fetchone("""SELECT user_id FROM
|
||||||
|
users WHERE server_id=? and nickname=?""",
|
||||||
|
[server_id, nickname.lower()])
|
||||||
|
return value if value == None else value[0]
|
||||||
|
|
||||||
class BotSettings(Table):
|
class BotSettings(Table):
|
||||||
def set(self, setting, value):
|
def set(self, setting, value):
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
|
@ -85,129 +112,118 @@ class ServerSettings(Table):
|
||||||
[server_id, setting.lower()])
|
[server_id, setting.lower()])
|
||||||
|
|
||||||
class ChannelSettings(Table):
|
class ChannelSettings(Table):
|
||||||
def set(self, server_id, channel, setting, value):
|
def set(self, channel_id, setting, value):
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
"INSERT OR REPLACE INTO channel_settings VALUES (?, ?, ?, ?)",
|
"INSERT OR REPLACE INTO channel_settings VALUES (?, ?, ?)",
|
||||||
[server_id, channel.lower(), setting.lower(), json.dumps(value)])
|
[channel_id, setting.lower(), json.dumps(value)])
|
||||||
def get(self, server_id, channel, setting, default=None):
|
def get(self, channel_id, setting, default=None):
|
||||||
value = self.database.execute_fetchone(
|
value = self.database.execute_fetchone(
|
||||||
"""SELECT value FROM channel_settings WHERE
|
"""SELECT value FROM channel_settings WHERE
|
||||||
server_id=? AND channel=? AND setting=?""",
|
channel_id=? AND setting=?""", [channel_id, setting.lower()])
|
||||||
[server_id, channel.lower(), setting.lower()])
|
|
||||||
if value:
|
if value:
|
||||||
return json.loads(value[0])
|
return json.loads(value[0])
|
||||||
return default
|
return default
|
||||||
def find(self, server_id, channel, pattern, default=[]):
|
def find(self, channel_id, pattern, default=[]):
|
||||||
values = self.database.execute_fetchall(
|
values = self.database.execute_fetchall(
|
||||||
"""SELECT setting, value FROM channel_settings WHERE
|
"""SELECT setting, value FROM channel_settings WHERE
|
||||||
server_id=? AND channel=? setting LIKE '?'""",
|
channel_id=? setting LIKE '?'""", [channel_id, pattern.lower()])
|
||||||
[server_id, channel.lower(), pattern.lower()])
|
|
||||||
if values:
|
if values:
|
||||||
for i, value in enumerate(values):
|
for i, value in enumerate(values):
|
||||||
values[i] = value[0], json.loads(value[1])
|
values[i] = value[0], json.loads(value[1])
|
||||||
return values
|
return values
|
||||||
return default
|
return default
|
||||||
def find_prefix(self, server_id, channel, prefix,
|
def find_prefix(self, channel_id, prefix, default=[]):
|
||||||
default=[]):
|
return self.find_channel_settings(channel_id, "%s%" % prefix,
|
||||||
return self.find_channel_settings(server_id, channel, "%s%" % prefix,
|
|
||||||
default)
|
default)
|
||||||
def delete(self, server_id, channel, setting):
|
def delete(self, channel_id, setting):
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
"""DELETE FROM channel_settings WHERE
|
"""DELETE FROM channel_settings WHERE channel_id=?
|
||||||
server_id=? AND channel=? AND setting=?""",
|
AND setting=?""", [channel_id, setting.lower()])
|
||||||
[server_id, channel.lower(), setting.lower()])
|
|
||||||
|
|
||||||
class UserSettings(Table):
|
class UserSettings(Table):
|
||||||
def set(self, server_id, nickname, setting, value):
|
def set(self, user_id, setting, value):
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
"INSERT OR REPLACE INTO user_settings VALUES (?, ?, ?, ?)",
|
"INSERT OR REPLACE INTO user_settings VALUES (?, ?, ?)",
|
||||||
[server_id, nickname.lower(), setting.lower(), json.dumps(value)])
|
[user_id, setting.lower(), json.dumps(value)])
|
||||||
def get(self, server_id, nickname, setting, default=None):
|
def get(self, user_id, setting, default=None):
|
||||||
value = self.database.execute_fetchone(
|
value = self.database.execute_fetchone(
|
||||||
"""SELECT value FROM user_settings WHERE
|
"""SELECT value FROM user_settings WHERE
|
||||||
server_id=? AND nickname=? and setting=?""",
|
user_id=? and setting=?""", [user_id, setting.lower()])
|
||||||
[server_id, nickname.lower(), setting.lower()])
|
|
||||||
if value:
|
if value:
|
||||||
return json.loads(value[0])
|
return json.loads(value[0])
|
||||||
return default
|
return default
|
||||||
def find_all_by_setting(self, server_id, setting, default=[]):
|
def find_all_by_setting(self, server_id, setting, default=[]):
|
||||||
values = self.database.execute_fetchall(
|
values = self.database.execute_fetchall(
|
||||||
"""SELECT nickname, value FROM user_settings WHERE
|
"""SELECT users.nickname, user_settings.value FROM
|
||||||
server_id=? AND setting=?""",
|
user_settings INNER JOIN users ON
|
||||||
|
user_settings.user_id=users.user_id WHERE
|
||||||
|
users.server_id=? AND user_settings.setting=?""",
|
||||||
[server_id, setting])
|
[server_id, setting])
|
||||||
if values:
|
if values:
|
||||||
for i, value in enumerate(values):
|
for i, value in enumerate(values):
|
||||||
values[i] = value[0], json.loads(value[1])
|
values[i] = value[0], json.loads(value[1])
|
||||||
return values
|
return values
|
||||||
return default
|
return default
|
||||||
def find(self, server_id, nickname, pattern, default=[]):
|
def find(self, user_id, pattern, default=[]):
|
||||||
values = self.database.execute(
|
values = self.database.execute(
|
||||||
"""SELECT setting, value FROM user_settings WHERE
|
"""SELECT setting, value FROM user_settings WHERE
|
||||||
server_id=? AND nickname=? AND setting LIKE '?'""",
|
user_id=? AND setting LIKE '?'""", [user_id, pattern.lower()])
|
||||||
[server_id, nickname.lower(), pattern.lower()])
|
|
||||||
if values:
|
if values:
|
||||||
for i, value in enumerate(values):
|
for i, value in enumerate(values):
|
||||||
values[i] = value[0], json.loads(value[1])
|
values[i] = value[0], json.loads(value[1])
|
||||||
return values
|
return values
|
||||||
return default
|
return default
|
||||||
def find_prefix(self, server_id, nickname, prefix,
|
def find_prefix(self, user_id, prefix, default=[]):
|
||||||
default=[]):
|
return self.find_user_settings(user_id, "%s%" % prefix, default)
|
||||||
return self.find_user_settings(server_id, nickname, "%s%" % prefix,
|
def delete(self, user_idsetting):
|
||||||
default)
|
|
||||||
def delete(self, server_id, nickname, setting):
|
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
"""DELETE FROM user_settings WHERE
|
"""DELETE FROM user_settings WHERE
|
||||||
server_id=? AND nickname=? AND setting=?""",
|
user_id=? AND setting=?""", [user_id, setting.lower()])
|
||||||
[server_id, nickname.lower(), setting.lower()])
|
|
||||||
|
|
||||||
class UserChannelSettings(Table):
|
class UserChannelSettings(Table):
|
||||||
def set(self, server_id, channel, nickname,
|
def set(self, user_id, channel_id, setting, value):
|
||||||
setting, value):
|
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
"""INSERT OR REPLACE INTO user_channel_settings VALUES
|
"""INSERT OR REPLACE INTO user_channel_settings VALUES
|
||||||
(?, ?, ?, ?, ?)""",
|
(?, ?, ?, ?)""",
|
||||||
[server_id, channel.lower(), nickname.lower(), setting.lower(),
|
[user_id, channel_id, setting.lower(), json.dumps(value)])
|
||||||
json.dumps(value)])
|
def get(self, user_id, channel_id, setting, default=None):
|
||||||
def get(self, server_id, channel, nickname,
|
|
||||||
setting, default=None):
|
|
||||||
value = self.database.execute_fetchone(
|
value = self.database.execute_fetchone(
|
||||||
"""SELECT value FROM user_channel_settings WHERE
|
"""SELECT value FROM user_channel_settings WHERE
|
||||||
server_id=? AND channel=? AND nickname=? and setting=?""",
|
user_id=? AND channel_id=? AND setting=?""",
|
||||||
[server_id, channel.lower(), nickname.lower(), setting.lower()])
|
[user_id, channel_id, setting.lower()])
|
||||||
if value:
|
if value:
|
||||||
return json.loads(value[0])
|
return json.loads(value[0])
|
||||||
return default
|
return default
|
||||||
def find(self, server_id, channel, nickname,
|
def find(self, user_id, channel_id, pattern, default=[]):
|
||||||
pattern, default=[]):
|
|
||||||
values = self.database.execute_fetchall(
|
values = self.database.execute_fetchall(
|
||||||
"""SELECT setting, value FROM user_channel_settings WHERE
|
"""SELECT setting, value FROM user_channel_settings WHERE
|
||||||
server_id=? AND channel=? AND nickname=? AND setting LIKE '?'""",
|
user_id=? AND channel_id=? AND setting LIKE '?'""",
|
||||||
[server_id, channel.lower(), nickname.lower(), pattern.lower()])
|
[user_id, channel_id, pattern.lower()])
|
||||||
if values:
|
if values:
|
||||||
for i, value in enumerate(values):
|
for i, value in enumerate(values):
|
||||||
values[i] = value[0], json.loads(value[1])
|
values[i] = value[0], json.loads(value[1])
|
||||||
return values
|
return values
|
||||||
return default
|
return default
|
||||||
def find_prefix(self, server_id, channel, nickname,
|
def find_prefix(self, user_id, channel_id, prefix, default=[]):
|
||||||
prefix, default=[]):
|
return self.find_user_settings(user_id, channel_id, "%s%" % prefix,
|
||||||
return self.find_user_settings(server_id, nickname, "%s%" % prefix,
|
|
||||||
default)
|
default)
|
||||||
def find_by_setting(self, server_id, nickname,
|
def find_by_setting(self, server_id, user_id, setting, default=[]):
|
||||||
setting, default=[]):
|
|
||||||
values = self.database.execute_fetchall(
|
values = self.database.execute_fetchall(
|
||||||
"""SELECT channel, value FROM user_channel_settings WHERE
|
"""SELECT channels.name, user_channel_settings.value FROM
|
||||||
server_id=? AND nickname=? AND setting=?""",
|
user_channel_settings INNER JOIN channels ON
|
||||||
[server_id, nickname.lower(), setting])
|
user_channel_settings.channel_id=channels.channel_id
|
||||||
|
WHERE channels.server_id=? AND
|
||||||
|
user_channel_settings.user_id=?""", [server_id, user_id])
|
||||||
if values:
|
if values:
|
||||||
for i, value in enumerate(values):
|
for i, value in enumerate(values):
|
||||||
values[i] = value[0], json.loads(value[1])
|
values[i] = value[0], json.loads(value[1])
|
||||||
return values
|
return values
|
||||||
return default
|
return default
|
||||||
def delete(self, server_id, channel, nickname, setting):
|
def delete(self, user_id, channel_id, setting):
|
||||||
self.database.execute(
|
self.database.execute(
|
||||||
"""DELETE FROM user_channel_settings WHERE
|
"""DELETE FROM user_channel_settings WHERE
|
||||||
server_id=? AND channel=? AND nickname=? AND setting=?""",
|
user_id=? AND channel_id=? AND setting=?""",
|
||||||
[server_id, channel.lower(), nickname.lower(), setting.lower()])
|
[user_id, channel_id, setting.lower()])
|
||||||
|
|
||||||
class Database(object):
|
class Database(object):
|
||||||
def __init__(self, bot, location="bot.db"):
|
def __init__(self, bot, location="bot.db"):
|
||||||
|
@ -221,6 +237,8 @@ class Database(object):
|
||||||
self._cursor = None
|
self._cursor = None
|
||||||
|
|
||||||
self.make_servers_table()
|
self.make_servers_table()
|
||||||
|
self.make_channels_table()
|
||||||
|
self.make_users_table()
|
||||||
self.make_bot_settings_table()
|
self.make_bot_settings_table()
|
||||||
self.make_server_settings_table()
|
self.make_server_settings_table()
|
||||||
self.make_channel_settings_table()
|
self.make_channel_settings_table()
|
||||||
|
@ -228,6 +246,8 @@ class Database(object):
|
||||||
self.make_user_channel_settings_table()
|
self.make_user_channel_settings_table()
|
||||||
|
|
||||||
self.servers = Servers(self)
|
self.servers = Servers(self)
|
||||||
|
self.channels = Channels(self)
|
||||||
|
self.users = Users(self)
|
||||||
self.bot_settings = BotSettings(self)
|
self.bot_settings = BotSettings(self)
|
||||||
self.server_settings = ServerSettings(self)
|
self.server_settings = ServerSettings(self)
|
||||||
self.channel_settings = ChannelSettings(self)
|
self.channel_settings = ChannelSettings(self)
|
||||||
|
@ -274,10 +294,27 @@ class Database(object):
|
||||||
def make_servers_table(self):
|
def make_servers_table(self):
|
||||||
if not self.has_table("servers"):
|
if not self.has_table("servers"):
|
||||||
self.execute("""CREATE TABLE servers
|
self.execute("""CREATE TABLE servers
|
||||||
(server_id INTEGER PRIMARY KEY,
|
(server_id INTEGER PRIMARY KEY, hostname TEXT,
|
||||||
hostname TEXT, port INTEGER, password TEXT,
|
port INTEGER,password TEXT,ipv4 BOOLEAN, tls BOOLEAN,
|
||||||
ipv4 BOOLEAN, tls BOOLEAN, nickname TEXT,
|
nickname TEXT, username TEXT, realname TEXT)""")
|
||||||
username TEXT, realname TEXT)""")
|
def make_channels_table(self):
|
||||||
|
if not self.has_table("channels"):
|
||||||
|
self.execute("""CREATE TABLE channels
|
||||||
|
(channel_id INTEGER PRIMARY KEY, server_id INTEGER,
|
||||||
|
name TEXT, FOREIGN KEY (server_id) REFERENCES
|
||||||
|
servers (server_id) ON DELETE CASCADE,
|
||||||
|
UNIQUE (server_id, name))""")
|
||||||
|
self.execute("""CREATE INDEX channels_index
|
||||||
|
on channels (server_id, name)""")
|
||||||
|
def make_users_table(self):
|
||||||
|
if not self.has_table("users"):
|
||||||
|
self.execute("""CREATE TABLE users
|
||||||
|
(user_id INTEGER PRIMARY KEY, server_id INTEGER,
|
||||||
|
nickname TEXT, FOREIGN KEY (server_id) REFERENCES
|
||||||
|
servers (server_id) ON DELETE CASCADE,
|
||||||
|
UNIQUE (server_id, nickname))""")
|
||||||
|
self.execute("""CREATE INDEX users_index
|
||||||
|
on users (server_id, nickname)""")
|
||||||
def make_bot_settings_table(self):
|
def make_bot_settings_table(self):
|
||||||
if not self.has_table("bot_settings"):
|
if not self.has_table("bot_settings"):
|
||||||
self.execute("""CREATE TABLE bot_settings
|
self.execute("""CREATE TABLE bot_settings
|
||||||
|
@ -296,28 +333,27 @@ class Database(object):
|
||||||
def make_channel_settings_table(self):
|
def make_channel_settings_table(self):
|
||||||
if not self.has_table("channel_settings"):
|
if not self.has_table("channel_settings"):
|
||||||
self.execute("""CREATE TABLE channel_settings
|
self.execute("""CREATE TABLE channel_settings
|
||||||
(server_id INTEGER, channel TEXT, setting TEXT,
|
(channel_id INTEGER, setting TEXT, value TEXT,
|
||||||
value TEXT, FOREIGN KEY (server_id) REFERENCES
|
FOREIGN KEY (channel_id) REFERENCES channels(channel_id)
|
||||||
servers(server_id) ON DELETE CASCADE,
|
ON DELETE CASCADE, PRIMARY KEY (channel_id, setting))""")
|
||||||
PRIMARY KEY (server_id, channel, setting))""")
|
|
||||||
self.execute("""CREATE INDEX channel_settings_index
|
self.execute("""CREATE INDEX channel_settings_index
|
||||||
ON channel_settings (server_id, channel, setting)""")
|
ON channel_settings (channel_id, setting)""")
|
||||||
def make_user_settings_table(self):
|
def make_user_settings_table(self):
|
||||||
if not self.has_table("user_settings"):
|
if not self.has_table("user_settings"):
|
||||||
self.execute("""CREATE TABLE user_settings
|
self.execute("""CREATE TABLE user_settings
|
||||||
(server_id INTEGER, nickname TEXT, setting TEXT,
|
(user_id INTEGER, setting TEXT, value TEXT,
|
||||||
value TEXT, FOREIGN KEY (server_id) REFERENCES
|
FOREIGN KEY (user_id) REFERENCES users(user_id)
|
||||||
servers(server_id) ON DELETE CASCADE,
|
ON DELETE CASCADE, PRIMARY KEY (user_id, setting))""")
|
||||||
PRIMARY KEY (server_id, nickname, setting))""")
|
|
||||||
self.execute("""CREATE INDEX user_settings_index ON
|
self.execute("""CREATE INDEX user_settings_index ON
|
||||||
user_settings (server_id, nickname, setting)""")
|
user_settings (user_id, setting)""")
|
||||||
def make_user_channel_settings_table(self):
|
def make_user_channel_settings_table(self):
|
||||||
if not self.has_table("user_channel_settings"):
|
if not self.has_table("user_channel_settings"):
|
||||||
self.execute("""CREATE TABLE user_channel_settings
|
self.execute("""CREATE TABLE user_channel_settings
|
||||||
(server_id INTEGER, channel TEXT, nickname TEXT,
|
(user_id INTEGER, channel_id INTEGER, setting TEXT,
|
||||||
setting TEXT, value TEXT, FOREIGN KEY (server_id)
|
value TEXT, FOREIGN KEY (user_id) REFERENCES
|
||||||
REFERENCES servers(server_id) ON DELETE CASCADE,
|
users(user_id) ON DELETE CASCADE, FOREIGN KEY
|
||||||
PRIMARY KEY (server_id, nickname, channel, setting))""")
|
(channel_id) REFERENCES channels(channel_id) ON
|
||||||
|
DELETE CASCADE, PRIMARY KEY (user_id, channel_id,
|
||||||
|
setting))""")
|
||||||
self.execute("""CREATE INDEX user_channel_settings_index
|
self.execute("""CREATE INDEX user_channel_settings_index
|
||||||
ON user_channel_settings (server_id, nickname, channel,
|
ON user_channel_settings (user_id, channel_id, setting)""")
|
||||||
setting)""")
|
|
||||||
|
|
|
@ -2,9 +2,9 @@ import uuid
|
||||||
import IRCLog
|
import IRCLog
|
||||||
|
|
||||||
class Channel(object):
|
class Channel(object):
|
||||||
def __init__(self, name, server, bot):
|
def __init__(self, name, id, server, bot):
|
||||||
self.id = uuid.uuid1().hex
|
|
||||||
self.name = name.lower()
|
self.name = name.lower()
|
||||||
|
self.id = id
|
||||||
self.server = server
|
self.server = server
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.topic = ""
|
self.topic = ""
|
||||||
|
@ -67,36 +67,34 @@ class Channel(object):
|
||||||
channel=self, mode=mode, args=args, remove=True)
|
channel=self, mode=mode, args=args, remove=True)
|
||||||
|
|
||||||
def set_setting(self, setting, value):
|
def set_setting(self, setting, value):
|
||||||
self.bot.database.channel_settings.set(self.server.id,
|
self.bot.database.channel_settings.set(self.id, setting, value)
|
||||||
self.name, setting, value)
|
|
||||||
def get_setting(self, setting, default=None):
|
def get_setting(self, setting, default=None):
|
||||||
return self.bot.database.channel_settings.get(
|
return self.bot.database.channel_settings.get(self.id, setting,
|
||||||
self.server.id, self.name, setting, default)
|
default)
|
||||||
def find_settings(self, pattern, default=[]):
|
def find_settings(self, pattern, default=[]):
|
||||||
return self.bot.database.channel_settings.find(
|
return self.bot.database.channel_settings.find(self.id, pattern,
|
||||||
self.server.id, self.name, pattern, default)
|
default)
|
||||||
def find_settings_prefix(self, prefix, default=[]):
|
def find_settings_prefix(self, prefix, default=[]):
|
||||||
return self.bot.database.channel_settings.find_prefix(
|
return self.bot.database.channel_settings.find_prefix(self.id,
|
||||||
self.server.id, self.name, prefix, default)
|
prefix, default)
|
||||||
def del_setting(self, setting):
|
def del_setting(self, setting):
|
||||||
self.bot.database.channel_settings.delete(self.server.id,
|
self.bot.database.channel_settings.delete(self.id, setting)
|
||||||
self.name, setting)
|
|
||||||
|
|
||||||
def set_user_setting(self, nickname, setting, value):
|
def set_user_setting(self, user_id, setting, value):
|
||||||
self.bot.database.user_channel_settings.set(self.server.id,
|
self.bot.database.user_channel_settings.set(user_id, self.id,
|
||||||
self.name, nickname, setting, value)
|
setting, value)
|
||||||
def get_user_setting(self, nickname, setting, default=None):
|
def get_user_setting(self, user_id, setting, default=None):
|
||||||
return self.bot.database.user_channel_settings.get(
|
return self.bot.database.user_channel_settings.get(user_id,
|
||||||
self.server.id, self.name, nickname, setting, default)
|
self.id, setting, default)
|
||||||
def find_user_settings(self, nickname, pattern, default=[]):
|
def find_user_settings(self, user_i, pattern, default=[]):
|
||||||
return self.bot.database.user_channel_settings.find(
|
return self.bot.database.user_channel_settings.find(user_id,
|
||||||
self.server.id, self.name, nickname, pattern, default)
|
self.id, pattern, default)
|
||||||
def find_user_settings_prefix(self, nickname, prefix, default=[]):
|
def find_user_settings_prefix(self, user_id, prefix, default=[]):
|
||||||
return self.bot.database.user_channel_settings.find_prefix(
|
return self.bot.database.user_channel_settings.find_prefix(
|
||||||
self.server.id, self.name, nickname, prefix, default)
|
user_id, self.id, prefix, default)
|
||||||
def del_user_setting(self, nickname, setting):
|
def del_user_setting(self, user_id, setting):
|
||||||
self.bot.database.user_channel_settings.delete(self.server.id,
|
self.bot.database.user_channel_settings.delete(user_id, self.id,
|
||||||
self.name, nickname, setting)
|
setting)
|
||||||
|
|
||||||
def send_message(self, text, prefix=None):
|
def send_message(self, text, prefix=None):
|
||||||
self.server.send_message(self.name, text, prefix=prefix)
|
self.server.send_message(self.name, text, prefix=prefix)
|
||||||
|
|
14
IRCServer.py
14
IRCServer.py
|
@ -116,12 +116,16 @@ class Server(object):
|
||||||
return nickname.lower() in self.users
|
return nickname.lower() in self.users
|
||||||
def get_user(self, nickname):
|
def get_user(self, nickname):
|
||||||
if not self.has_user(nickname):
|
if not self.has_user(nickname):
|
||||||
new_user = IRCUser.User(nickname, self, self.bot)
|
user_id = self.get_user_id(nickname)
|
||||||
|
new_user = IRCUser.User(nickname, user_id, self, self.bot)
|
||||||
self.bot.events.on("new").on("user").call(
|
self.bot.events.on("new").on("user").call(
|
||||||
user=new_user, server=self)
|
user=new_user, server=self)
|
||||||
self.users[new_user.nickname_lower] = new_user
|
self.users[new_user.nickname_lower] = new_user
|
||||||
self.new_users.add(new_user)
|
self.new_users.add(new_user)
|
||||||
return self.users[nickname.lower()]
|
return self.users[nickname.lower()]
|
||||||
|
def get_user_id(self, nickname):
|
||||||
|
self.bot.database.users.add(self.id, nickname)
|
||||||
|
return self.bot.database.users.get_id(self.id, nickname)
|
||||||
def remove_user(self, user):
|
def remove_user(self, user):
|
||||||
del self.users[user.nickname_lower]
|
del self.users[user.nickname_lower]
|
||||||
for channel in user.channels:
|
for channel in user.channels:
|
||||||
|
@ -134,12 +138,16 @@ class Server(object):
|
||||||
) in self.channels
|
) in self.channels
|
||||||
def get_channel(self, channel_name):
|
def get_channel(self, channel_name):
|
||||||
if not self.has_channel(channel_name):
|
if not self.has_channel(channel_name):
|
||||||
new_channel = IRCChannel.Channel(channel_name, self,
|
channel_id = self.get_channel_id(channel_name)
|
||||||
self.bot)
|
new_channel = IRCChannel.Channel(channel_name, channel_id,
|
||||||
|
self, self.bot)
|
||||||
self.bot.events.on("new").on("channel").call(
|
self.bot.events.on("new").on("channel").call(
|
||||||
channel=new_channel, server=self)
|
channel=new_channel, server=self)
|
||||||
self.channels[new_channel.name] = new_channel
|
self.channels[new_channel.name] = new_channel
|
||||||
return self.channels[channel_name.lower()]
|
return self.channels[channel_name.lower()]
|
||||||
|
def get_channel_id(self, channel_name):
|
||||||
|
self.bot.database.channels.add(self.id, channel_name)
|
||||||
|
return self.bot.database.channels.get_id(self.id, channel_name)
|
||||||
def remove_channel(self, channel):
|
def remove_channel(self, channel):
|
||||||
for user in channel.users:
|
for user in channel.users:
|
||||||
user.part_channel(channel)
|
user.part_channel(channel)
|
||||||
|
|
23
IRCUser.py
23
IRCUser.py
|
@ -2,8 +2,9 @@ import uuid
|
||||||
import IRCLog
|
import IRCLog
|
||||||
|
|
||||||
class User(object):
|
class User(object):
|
||||||
def __init__(self, nickname, server, bot):
|
def __init__(self, nickname, id, server, bot):
|
||||||
self.set_nickname(nickname)
|
self.set_nickname(nickname)
|
||||||
|
self.id = id
|
||||||
self.username = None
|
self.username = None
|
||||||
self.hostname = None
|
self.hostname = None
|
||||||
self.realname = None
|
self.realname = None
|
||||||
|
@ -20,23 +21,21 @@ class User(object):
|
||||||
def part_channel(self, channel):
|
def part_channel(self, channel):
|
||||||
self.channels.remove(channel)
|
self.channels.remove(channel)
|
||||||
def set_setting(self, setting, value):
|
def set_setting(self, setting, value):
|
||||||
self.bot.database.user_settings.set(self.server.id, self.nickname,
|
self.bot.database.user_settings.set(self.id, setting, value)
|
||||||
setting, value)
|
|
||||||
def get_setting(self, setting, default=None):
|
def get_setting(self, setting, default=None):
|
||||||
return self.bot.database.user_settings.get(self.server.id,
|
return self.bot.database.user_settings.get(self.id, setting,
|
||||||
self.nickname, setting, default)
|
default)
|
||||||
def find_settings(self, pattern, default=[]):
|
def find_settings(self, pattern, default=[]):
|
||||||
return self.bot.database.user_settings.find(self.server.id,
|
return self.bot.database.user_settings.find(self.id, pattern,
|
||||||
self.nickname, pattern, default)
|
default)
|
||||||
def find_settings_prefix(self, prefix, default=[]):
|
def find_settings_prefix(self, prefix, default=[]):
|
||||||
return self.bot.database.user_settings.find_prefix(
|
return self.bot.database.user_settings.find_prefix(self.id,
|
||||||
self.server.id, self.nickname, prefix, default)
|
prefix, default)
|
||||||
def del_setting(self, setting):
|
def del_setting(self, setting):
|
||||||
self.bot.database.user_settings.delete(self.server.id, self.nickname,
|
self.bot.database.user_settings.delete(self.id, setting)
|
||||||
setting)
|
|
||||||
def get_channel_settings_per_setting(self, setting, default=[]):
|
def get_channel_settings_per_setting(self, setting, default=[]):
|
||||||
return self.bot.database.user_channel_settings.find_by_setting(
|
return self.bot.database.user_channel_settings.find_by_setting(
|
||||||
self.server.id, self.nickname, setting, default)
|
self.server.id, self.id, setting, default)
|
||||||
|
|
||||||
def send_message(self, message, prefix=None):
|
def send_message(self, message, prefix=None):
|
||||||
self.server.send_message(self.nickname, message, prefix=prefix)
|
self.server.send_message(self.nickname, message, prefix=prefix)
|
||||||
|
|
|
@ -11,20 +11,20 @@ class Module(object):
|
||||||
channel_only=True, usage="<username> <message>")
|
channel_only=True, usage="<username> <message>")
|
||||||
|
|
||||||
def channel_message(self, event):
|
def channel_message(self, event):
|
||||||
messages = event["channel"].get_user_setting(
|
messages = event["channel"].get_user_setting(event["user"].id,
|
||||||
event["user"].nickname, "to", [])
|
"to", [])
|
||||||
for nickname, message in messages:
|
for nickname, message in messages:
|
||||||
event["channel"].send_message("%s: <%s> %s" % (
|
event["channel"].send_message("%s: <%s> %s" % (
|
||||||
event["user"].nickname, nickname, message))
|
event["user"].nickname, nickname, message))
|
||||||
if messages:
|
if messages:
|
||||||
event["channel"].del_user_setting(
|
event["channel"].del_user_setting(event["user"].id, "to")
|
||||||
event["user"].nickname, "to")
|
|
||||||
|
|
||||||
def to(self, event):
|
def to(self, event):
|
||||||
messages = event["target"].get_user_setting(
|
target_user = event["server"].get_user(event["args_split"][0])
|
||||||
event["args_split"][0], "to", [])
|
messages = event["target"].get_user_setting(target_user.id,
|
||||||
|
"to", [])
|
||||||
messages.append([event["user"].nickname,
|
messages.append([event["user"].nickname,
|
||||||
" ".join(event["args_split"][1:])])
|
" ".join(event["args_split"][1:])])
|
||||||
event["target"].set_user_setting(
|
event["target"].set_user_setting(target_user.id,
|
||||||
event["args_split"][0], "to", messages)
|
"to", messages)
|
||||||
event["stdout"].write("Message saved")
|
event["stdout"].write("Message saved")
|
||||||
|
|
|
@ -24,10 +24,10 @@ class Module(object):
|
||||||
words = list(filter(None, event["message_split"]))
|
words = list(filter(None, event["message_split"]))
|
||||||
word_count = len(words)
|
word_count = len(words)
|
||||||
|
|
||||||
user_words = event["channel"].get_user_setting(
|
user_words = event["channel"].get_user_setting(user.id,
|
||||||
user.nickname, "words", 0)
|
"words", 0)
|
||||||
user_words += word_count
|
user_words += word_count
|
||||||
event["channel"].set_user_setting(user.nickname,
|
event["channel"].set_user_setting(user.id,
|
||||||
"words", user_words)
|
"words", user_words)
|
||||||
|
|
||||||
tracked_words = set(event["server"].get_setting(
|
tracked_words = set(event["server"].get_setting(
|
||||||
|
|
Loading…
Reference in a new issue