mirror of
https://github.com/nichkara/InfinitumBotty.git
synced 2026-06-10 22:26:23 +02:00
Initalize repo
This commit is contained in:
38
FaustBot/Model/BlockedUsers.py
Normal file
38
FaustBot/Model/BlockedUsers.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class BlockProvider(object):
|
||||
_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS blockedusers (id INTEGER PRIMARY KEY, \
|
||||
user TEXT)'
|
||||
_IS_BLOCKED = 'SELECT user FROM blockedusers'
|
||||
_BLOCK = 'INSERT INTO blockedusers (id, user) VALUES (?, ?)'
|
||||
_DELETE_BLOCK = 'DELETE FROM blockedusers WHERE user = ?'
|
||||
|
||||
def __init__(self):
|
||||
self._database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(BlockProvider._CREATE_TABLE)
|
||||
self._database_connection.commit()
|
||||
|
||||
def is_blocked(self, user: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(BlockProvider._IS_BLOCKED)
|
||||
answer = cursor.fetchall()
|
||||
for ans in answer:
|
||||
if user.lower().find(ans[0])!=-1:
|
||||
return True
|
||||
return False
|
||||
|
||||
def block(self, user: str):
|
||||
data = (None, user.lower())
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(BlockProvider._BLOCK, data)
|
||||
self._database_connection.commit()
|
||||
|
||||
def delete_block(self, user: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(BlockProvider._DELETE_BLOCK, (user.lower(),))
|
||||
self._database_connection.commit()
|
||||
|
||||
def __exit__(self):
|
||||
self._database_connection.close()
|
||||
78
FaustBot/Model/Config.py
Normal file
78
FaustBot/Model/Config.py
Normal file
@@ -0,0 +1,78 @@
|
||||
class Config(object):
|
||||
CONFIG_PATH = 'config_path'
|
||||
|
||||
def __init__(self, path):
|
||||
"""
|
||||
|
||||
:param path:
|
||||
"""
|
||||
self._config_dict = {}
|
||||
if path:
|
||||
self._config_dict[Config.CONFIG_PATH] = path
|
||||
self.read_config(path)
|
||||
|
||||
def __getitem__(self, item: str):
|
||||
if item in self._config_dict:
|
||||
return self._config_dict[item]
|
||||
else:
|
||||
return None
|
||||
|
||||
def __setitem__(self, key: str, value: str):
|
||||
print (key +' '+ value+'\n\r')
|
||||
self._config_dict[key] = value
|
||||
|
||||
def read_config(self, path: str, append=True):
|
||||
f = open(path, 'r')
|
||||
if not append:
|
||||
self._config_dict = {}
|
||||
for l in f.readlines():
|
||||
kv_pair = l.split(':')
|
||||
if len(kv_pair) == 2:
|
||||
self._config_dict[kv_pair[0].strip()] = kv_pair[1][:-1].strip()
|
||||
mods = self._config_dict['mods'].split(',')
|
||||
self._config_dict['mods'] = []
|
||||
for mod in mods:
|
||||
self._config_dict['mods'].append(mod.strip())
|
||||
# If no idle_time value is given, we set it to five hours ( == 18000 seconds )
|
||||
if 'idle_time' not in self._config_dict:
|
||||
self._config_dict['idle_time'] = 18000
|
||||
self._config_dict['idle_time'] = int(self._config_dict['idle_time'])
|
||||
if 'blacklist' not in self._config_dict:
|
||||
self._config_dict['blacklist'] = []
|
||||
else:
|
||||
blacklist=self._config_dict['blacklist'].split(',')
|
||||
self._config_dict['blacklist'] = []
|
||||
for module in blacklist:
|
||||
self._config_dict['blacklist'].append(module.strip())
|
||||
|
||||
@property
|
||||
def lang(self):
|
||||
return self._config_dict["lang"]
|
||||
|
||||
@lang.setter
|
||||
def lang(self, value):
|
||||
self._config_dict["lang"] = value
|
||||
|
||||
@property
|
||||
def mods(self):
|
||||
return self._config_dict["mods"]
|
||||
|
||||
@mods.setter
|
||||
def mods(self, value):
|
||||
self._config_dict["mods"] = value
|
||||
|
||||
@property
|
||||
def idle_time(self):
|
||||
return self._config_dict["idle_time"]
|
||||
|
||||
@idle_time.setter
|
||||
def idle_time(self, value: int):
|
||||
self._config_dict["idle_time"] = value
|
||||
|
||||
@property
|
||||
def blacklist(self):
|
||||
return self._config_dict['blacklist']
|
||||
|
||||
@property
|
||||
def pwd(self):
|
||||
return self._config_dict['pwd']
|
||||
38
FaustBot/Model/ConnectionDetails.py
Normal file
38
FaustBot/Model/ConnectionDetails.py
Normal file
@@ -0,0 +1,38 @@
|
||||
class ConnectionDetails(object):
|
||||
def get_server(self):
|
||||
"""
|
||||
:return: the server to connect to
|
||||
"""
|
||||
return self._data['server']
|
||||
|
||||
def get_nick(self):
|
||||
"""
|
||||
:return: own nick
|
||||
"""
|
||||
return self._data['nick']
|
||||
|
||||
def get_channel(self):
|
||||
"""
|
||||
:return: the channel connected into
|
||||
"""
|
||||
return self._data['channel']
|
||||
|
||||
def get_port(self):
|
||||
return int(self._data['port'])
|
||||
|
||||
def get_lang(self):
|
||||
return self._data['lang']
|
||||
|
||||
def change_lang(self, lang):
|
||||
self._data['lang'] = lang
|
||||
|
||||
def get_mods(self):
|
||||
return self._data['mods']
|
||||
|
||||
def get_pwd(self):
|
||||
if self._data['pwd'] is None:
|
||||
return ''
|
||||
return self._data['pwd']
|
||||
|
||||
def __init__(self, config):
|
||||
self._data = config
|
||||
47
FaustBot/Model/GlossaryProvider.py
Normal file
47
FaustBot/Model/GlossaryProvider.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class GlossaryProvider(object):
|
||||
_CREATE_GLOSSARY_TABLE = 'CREATE TABLE IF NOT EXISTS glossary (id INTEGER PRIMARY KEY, \
|
||||
abbreviation TEXT, explanation TEXT)'
|
||||
_GET_EXPLANATION = 'SELECT id, explanation FROM glossary WHERE abbreviation = ?'
|
||||
_SAVE_OR_OVERWRITE = 'REPLACE INTO glossary (id, abbreviation, explanation) VALUES (?, ?, ?)'
|
||||
_DELETE_EXPLANATION = 'DELETE FROM glossary WHERE abbreviation = ?'
|
||||
|
||||
def __init__(self):
|
||||
self._database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(GlossaryProvider._CREATE_GLOSSARY_TABLE)
|
||||
self._database_connection.commit()
|
||||
|
||||
def get_explanation(self, abbreviation: str):
|
||||
"""
|
||||
|
||||
:param abbreviation:
|
||||
:return:
|
||||
"""
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(GlossaryProvider._GET_EXPLANATION, (abbreviation.lower(),))
|
||||
return cursor.fetchone()
|
||||
|
||||
def save_or_replace(self, abbreviation: str, explanation: str):
|
||||
"""
|
||||
|
||||
:param abbreviation:
|
||||
:param explanation:
|
||||
:return:
|
||||
"""
|
||||
existing = self.get_explanation(abbreviation)
|
||||
_id = existing[0] if existing is not None else None
|
||||
data = (_id, abbreviation.lower(), explanation)
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(GlossaryProvider._SAVE_OR_OVERWRITE, data)
|
||||
self._database_connection.commit()
|
||||
|
||||
def delete_explanation(self, abbreviation: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(GlossaryProvider._DELETE_EXPLANATION, (abbreviation.strip(),))
|
||||
self._database_connection.commit()
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self._database_connection.close()
|
||||
57
FaustBot/Model/HanDatabaseProvider.py
Normal file
57
FaustBot/Model/HanDatabaseProvider.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class HanDatabaseProvider(object):
|
||||
_CREATE_HANDB_TABLE = 'CREATE TABLE IF NOT EXISTS handb (id INTEGER PRIMARY KEY, \
|
||||
hanword TEXT)'
|
||||
_GET_RANDOM_WORD = 'SELECT hanword FROM handb ORDER BY RANDOM() LIMIT 1'
|
||||
_INSERT_WORD = 'REPLACE INTO handb(id, hanword) VALUES (?,?)'
|
||||
_DELETE_WORD = 'DELETE FROM handb WHERE hanword = ?'
|
||||
_GET_WORD = 'SELECT id, hanword FROM handb WHERE hanword = ?'
|
||||
def __init__(self):
|
||||
self._database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(HanDatabaseProvider._CREATE_HANDB_TABLE)
|
||||
self._database_connection.commit()
|
||||
|
||||
def get_random_word(self):
|
||||
"""
|
||||
|
||||
:param abbreviation:
|
||||
:return:
|
||||
"""
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(HanDatabaseProvider._GET_RANDOM_WORD)
|
||||
return cursor.fetchone()
|
||||
|
||||
def get_hanWord(self, HanWord):
|
||||
"""
|
||||
|
||||
:param abbreviation:
|
||||
:return:
|
||||
"""
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(HanDatabaseProvider._GET_WORD, (HanWord.upper(),))
|
||||
return cursor.fetchone()
|
||||
|
||||
def addWord(self, HanWord):
|
||||
"""
|
||||
|
||||
:param abbreviation:
|
||||
:param explanation:
|
||||
:return:
|
||||
"""
|
||||
existing = self.get_hanWord(HanWord)
|
||||
_id = existing[0] if existing is not None else None
|
||||
data = (_id, HanWord)
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(HanDatabaseProvider._INSERT_WORD, data)
|
||||
self._database_connection.commit()
|
||||
|
||||
def delete_hanWord(self, HanWord):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(HanDatabaseProvider._DELETE_WORD, (HanWord.strip(),))
|
||||
self._database_connection.commit()
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self._database_connection.close()
|
||||
36
FaustBot/Model/Introduction.py
Normal file
36
FaustBot/Model/Introduction.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class IntroductionProvider(object):
|
||||
_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS introduction (id INTEGER PRIMARY KEY, \
|
||||
user TEXT, intro TEXT)'
|
||||
_GET_INTRO = 'SELECT id, intro FROM introduction WHERE user = ?'
|
||||
_SAVE_OR_OVERWRITE = 'REPLACE INTO introduction (id, user, intro) VALUES (?, ?, ?)'
|
||||
_DELETE_INTRO = 'DELETE FROM introduction WHERE user = ?'
|
||||
|
||||
def __init__(self):
|
||||
self._database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(IntroductionProvider._CREATE_TABLE)
|
||||
self._database_connection.commit()
|
||||
|
||||
def get_intro(self, user: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(IntroductionProvider._GET_INTRO, (user.lower(),))
|
||||
return cursor.fetchone()
|
||||
|
||||
def save_or_replace(self, user: str, intro: str):
|
||||
existing = self.get_intro(user)
|
||||
_id = existing[0] if existing is not None else None
|
||||
data = (_id, user.lower(), intro)
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(IntroductionProvider._SAVE_OR_OVERWRITE, data)
|
||||
self._database_connection.commit()
|
||||
|
||||
def delete_intro(self, user: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(IntroductionProvider._DELETE_INTRO, (user.lower(),))
|
||||
self._database_connection.commit()
|
||||
|
||||
def __exit__(self):
|
||||
self._database_connection.close()
|
||||
9
FaustBot/Model/RemoteUser.py
Normal file
9
FaustBot/Model/RemoteUser.py
Normal file
@@ -0,0 +1,9 @@
|
||||
class RemoteUser(object):
|
||||
"""
|
||||
Holds information about another user on IRC (nick!user@host)
|
||||
"""
|
||||
|
||||
def __init__(self, nick, user, host):
|
||||
self.nick = nick
|
||||
self.user = user
|
||||
self.host = host
|
||||
36
FaustBot/Model/ScoreProvider.py
Normal file
36
FaustBot/Model/ScoreProvider.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class ScoreProvider(object):
|
||||
_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS score (id INTEGER PRIMARY KEY, \
|
||||
user TEXT, score INTEGER)'
|
||||
_GET_SCORE = 'SELECT id, score FROM score WHERE user = ?'
|
||||
_SAVE_OR_OVERWRITE = 'REPLACE INTO score (id, user, score) VALUES (?, ?, ?)'
|
||||
_DELETE_SCORE = 'DELETE FROM score WHERE user = ?'
|
||||
|
||||
def __init__(self):
|
||||
self._database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(ScoreProvider._CREATE_TABLE)
|
||||
self._database_connection.commit()
|
||||
|
||||
def get_score(self, user: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(ScoreProvider._GET_SCORE, (user.lower(),))
|
||||
return cursor.fetchone()
|
||||
|
||||
def save_or_replace(self, user: str, score: int):
|
||||
existing = self.get_score(user)
|
||||
_id = existing[0] if existing is not None else None
|
||||
data = (_id, user.lower(), score)
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(ScoreProvider._SAVE_OR_OVERWRITE, data)
|
||||
self._database_connection.commit()
|
||||
|
||||
def delete_score(self, user: str):
|
||||
cursor = self._database_connection.cursor()
|
||||
cursor.execute(ScoreProvider._DELETE_SCORE, (user.lower(),))
|
||||
self._database_connection.commit()
|
||||
|
||||
def __exit__(self):
|
||||
self._database_connection.close()
|
||||
18
FaustBot/Model/TextProvider.py
Normal file
18
FaustBot/Model/TextProvider.py
Normal file
@@ -0,0 +1,18 @@
|
||||
class TextProvider(object):
|
||||
"""
|
||||
Provides different Texts
|
||||
"""
|
||||
|
||||
def get_random_fortune(self):
|
||||
"""
|
||||
:return: a random sentence
|
||||
"""
|
||||
return "Das macht Spa<70>"
|
||||
|
||||
def get_definiton(self, name):
|
||||
"""
|
||||
|
||||
:param name: name of definition to get
|
||||
:return: the definition
|
||||
"""
|
||||
return "Konnte Definition nicht finden"
|
||||
107
FaustBot/Model/UserProvider.py
Normal file
107
FaustBot/Model/UserProvider.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import sqlite3
|
||||
import time
|
||||
|
||||
|
||||
class UserProvider(object):
|
||||
"""
|
||||
Provides information about the users
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = self.database_connection.cursor()
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY , name TEXT)''')
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS user_stats(id INTEGER PRIMARY KEY, characters INT)''')
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS last_seen (id INTEGER PRIMARY KEY, last_seen REAL)''')
|
||||
self.database_connection.commit()
|
||||
|
||||
def get_characters(self, name):
|
||||
"""
|
||||
|
||||
:param name: name of user whom characters are to get
|
||||
:return: total number of characters written
|
||||
"""
|
||||
cursor = self.database_connection.cursor()
|
||||
id = self._get_id(name)
|
||||
if id is None:
|
||||
return 0
|
||||
for characters in cursor.execute("SELECT characters FROM user_stats WHERE id = ?", (id,)):
|
||||
return characters[0]
|
||||
return 0
|
||||
|
||||
def get_activity(self, name):
|
||||
"""
|
||||
|
||||
:param name: name of user whom activity to get
|
||||
:return: last activity by user
|
||||
"""
|
||||
cursor = self.database_connection.cursor()
|
||||
id = self._get_id(name)
|
||||
if id is None:
|
||||
return 0
|
||||
for time in cursor.execute("SELECT last_seen FROM last_seen WHERE id = ?", (id,)):
|
||||
return time[0]
|
||||
return 0
|
||||
|
||||
def add_characters(self, name, number):
|
||||
"""
|
||||
|
||||
:param name: User to Add Characters to
|
||||
:param number: Number of Characters to add
|
||||
:return: nothing
|
||||
"""
|
||||
cursor = self.database_connection.cursor()
|
||||
id = self._get_id(name)
|
||||
if id is None:
|
||||
self._create_user(name)
|
||||
id = self._get_id(name)
|
||||
for chars in cursor.execute("SELECT characters FROM user_stats WHERE id = ?", (id,)):
|
||||
chars = chars[0]
|
||||
chars += number
|
||||
cursor.execute("UPDATE user_stats SET characters = ? WHERE id = ?", (chars, id,))
|
||||
self.database_connection.commit()
|
||||
return None
|
||||
|
||||
def set_active(self, name):
|
||||
"""
|
||||
|
||||
:param name: set this user active at the moment
|
||||
:return: Nothing
|
||||
"""
|
||||
cursor = self.database_connection.cursor()
|
||||
id = self._get_id(name)
|
||||
ntime = time.time()
|
||||
if id is None:
|
||||
self._create_user(name)
|
||||
id = self._get_id(name)
|
||||
cursor.execute("UPDATE last_seen SET last_seen = ? WHERE id = ?", (ntime, id,))
|
||||
self.database_connection.commit()
|
||||
|
||||
def permission(self, user, percent):
|
||||
"""
|
||||
|
||||
:param user: user to ask permission for
|
||||
:param percent: percent needed for permission
|
||||
:return: True or False
|
||||
http://stackoverflow.com/questions/1682920/determine-if-a-user-is-idented-on-irc
|
||||
"""
|
||||
return True
|
||||
|
||||
def _get_id(self, name):
|
||||
cursor = self.database_connection.cursor()
|
||||
try:
|
||||
for id in cursor.execute("SELECT id FROM user WHERE name = ?", (name,)):
|
||||
return id[0]
|
||||
except:
|
||||
return None
|
||||
|
||||
def _create_user(self, name):
|
||||
cursor = self.database_connection.cursor()
|
||||
cursor.execute("INSERT INTO user(name) VALUES (?)", (name,))
|
||||
id = self._get_id(name)
|
||||
cursor.execute("INSERT INTO user_stats(id, characters) VALUES (?, 0)", (id,))
|
||||
cursor.execute("INSERT INTO last_seen (id, last_seen) VALUES (?, 0)", (id,))
|
||||
self.database_connection.commit()
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self.database_connection.close()
|
||||
1
FaustBot/Model/__init__.py
Normal file
1
FaustBot/Model/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__author__ = 'Pups'
|
||||
23
FaustBot/Model/i18n.py
Normal file
23
FaustBot/Model/i18n.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import sqlite3
|
||||
|
||||
|
||||
class i18n(object):
|
||||
def get_text(self, name, replacements=None, lang='de-DE'):
|
||||
"""
|
||||
|
||||
:param replacements:
|
||||
:param name: name of text
|
||||
:param lang: language to get text in
|
||||
:return: the text
|
||||
"""
|
||||
if replacements is None:
|
||||
replacements = {}
|
||||
database_connection = sqlite3.connect('faust_bot.db')
|
||||
cursor = database_connection.cursor()
|
||||
ltext = ""
|
||||
print(replacements)
|
||||
for longText in cursor.execute("SELECT longText FROM i18n WHERE lang = ? AND ident = ?", (lang, name,)):
|
||||
ltext = longText[0]
|
||||
for (key, value) in replacements.items():
|
||||
ltext = ltext.replace('$' + key, value)
|
||||
return ltext
|
||||
Reference in New Issue
Block a user