2023-04-17 21:46:34 -04:00
|
|
|
import rapidjson
|
2021-03-24 17:17:49 -04:00
|
|
|
from peewee import (BigIntegerField, BooleanField, IntegrityError, Model,
|
|
|
|
SqliteDatabase, TextField)
|
2020-08-24 17:02:08 -04:00
|
|
|
|
2023-10-12 19:22:06 -04:00
|
|
|
CHAT_WRAPPER_CACHE_LIMIT = 200
|
2021-03-24 11:18:00 -04:00
|
|
|
DATABASE = SqliteDatabase("database.sqlite", pragmas={
|
|
|
|
"journal_mode": "wal",
|
|
|
|
"cache_size": -1024 * 16}
|
|
|
|
)
|
2021-03-23 19:16:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
class BaseDB(Model):
|
|
|
|
class Meta:
|
|
|
|
database = DATABASE
|
|
|
|
|
|
|
|
|
|
|
|
class BlacklistedUser(BaseDB):
|
2021-03-24 09:10:32 -04:00
|
|
|
user_id = BigIntegerField(unique=True, primary_key=True)
|
2021-03-23 19:16:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SudoUser(BaseDB):
|
2021-03-24 09:10:32 -04:00
|
|
|
user_id = BigIntegerField(unique=True, primary_key=True)
|
2021-03-23 19:16:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Chat(BaseDB):
|
2021-03-24 09:10:32 -04:00
|
|
|
chat_id = BigIntegerField(unique=True, primary_key=True)
|
2021-03-23 19:16:21 -04:00
|
|
|
fun_enabled = BooleanField(default=True)
|
|
|
|
nsfw_enabled = BooleanField(default=True)
|
2023-02-05 09:46:06 -05:00
|
|
|
spoiler_nsfw = BooleanField(default=False)
|
2021-03-23 19:16:21 -04:00
|
|
|
disabled_commands = TextField(default="[]")
|
2021-03-23 20:01:27 -04:00
|
|
|
custom_prefix = TextField(default="/")
|
2021-06-22 15:13:00 -04:00
|
|
|
lang = TextField(default="en")
|
|
|
|
modmode_enabled = BooleanField(default=False)
|
2021-03-23 19:16:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
DATABASE.connect()
|
|
|
|
DATABASE.create_tables([
|
|
|
|
Chat,
|
|
|
|
BlacklistedUser,
|
|
|
|
SudoUser
|
|
|
|
])
|
2020-08-24 12:07:23 -04:00
|
|
|
|
|
|
|
|
2021-03-23 20:55:54 -04:00
|
|
|
class ChatWrapper():
|
|
|
|
def __init__(self, chat: Chat):
|
2023-10-12 19:22:06 -04:00
|
|
|
self.disabled_commands: list = rapidjson.loads(chat.disabled_commands)
|
2021-03-23 20:55:54 -04:00
|
|
|
self.chat = chat
|
|
|
|
|
|
|
|
# custom prefix functions
|
2021-03-24 16:16:13 -04:00
|
|
|
@property
|
|
|
|
def prefix(self) -> str:
|
2021-03-23 20:55:54 -04:00
|
|
|
return self.chat.custom_prefix
|
|
|
|
|
2021-03-24 16:16:13 -04:00
|
|
|
@prefix.setter
|
|
|
|
def prefix(self, prefix: str):
|
2021-03-23 20:55:54 -04:00
|
|
|
self.chat.custom_prefix = prefix
|
|
|
|
self.chat.save()
|
|
|
|
|
2021-06-22 15:13:00 -04:00
|
|
|
# language functions
|
|
|
|
@property
|
|
|
|
def lang(self) -> str:
|
|
|
|
return self.chat.lang
|
|
|
|
|
2021-06-22 15:45:23 -04:00
|
|
|
@lang.setter
|
2021-06-22 15:13:00 -04:00
|
|
|
def lang(self, lang: str):
|
|
|
|
self.chat.lang = lang
|
|
|
|
self.chat.save()
|
|
|
|
|
|
|
|
# modmode command functions
|
|
|
|
@property
|
2023-10-16 18:13:41 -04:00
|
|
|
def modmode_enabled(self) -> bool:
|
2021-06-22 15:13:00 -04:00
|
|
|
return self.chat.modmode_enabled
|
|
|
|
|
2021-06-22 15:45:23 -04:00
|
|
|
@modmode_enabled.setter
|
2021-06-22 15:13:00 -04:00
|
|
|
def modmode_enabled(self, enabled: bool):
|
|
|
|
self.chat.modmode_enabled = enabled
|
|
|
|
self.chat.save()
|
|
|
|
|
2021-03-23 20:55:54 -04:00
|
|
|
# fun command functions
|
2021-03-24 16:16:13 -04:00
|
|
|
@property
|
2021-03-23 20:55:54 -04:00
|
|
|
def fun_enabled(self) -> bool:
|
|
|
|
return self.chat.fun_enabled
|
|
|
|
|
2021-03-24 16:16:13 -04:00
|
|
|
@fun_enabled.setter
|
|
|
|
def fun_enabled(self, enabled: bool):
|
2021-03-23 20:55:54 -04:00
|
|
|
self.chat.fun_enabled = enabled
|
|
|
|
self.chat.save()
|
|
|
|
|
|
|
|
# nsfw command functions
|
2021-03-24 16:16:13 -04:00
|
|
|
@property
|
2021-03-23 20:55:54 -04:00
|
|
|
def nsfw_enabled(self) -> bool:
|
|
|
|
return self.chat.nsfw_enabled
|
|
|
|
|
2021-03-24 16:16:13 -04:00
|
|
|
@nsfw_enabled.setter
|
|
|
|
def nsfw_enabled(self, enabled: bool):
|
2021-03-23 20:55:54 -04:00
|
|
|
self.chat.nsfw_enabled = enabled
|
|
|
|
self.chat.save()
|
|
|
|
|
2023-02-05 09:46:06 -05:00
|
|
|
@property
|
|
|
|
def spoiler_nsfw(self) -> bool:
|
|
|
|
return self.chat.spoiler_nsfw
|
|
|
|
|
|
|
|
@spoiler_nsfw.setter
|
|
|
|
def spoiler_nsfw(self, enabled: bool):
|
|
|
|
self.chat.spoiler_nsfw = enabled
|
|
|
|
self.chat.save()
|
|
|
|
|
2021-03-23 20:55:54 -04:00
|
|
|
# disable/enable command functions
|
|
|
|
def enable_command(self, command: str):
|
2023-10-12 19:22:06 -04:00
|
|
|
if command in self.disabled_commands:
|
|
|
|
self.disabled_commands.remove(command)
|
|
|
|
self.chat.disabled_commands = rapidjson.dumps(self.disabled_commands)
|
2021-03-23 20:55:54 -04:00
|
|
|
self.chat.save()
|
|
|
|
|
|
|
|
def disable_command(self, command: str):
|
2023-10-12 19:22:06 -04:00
|
|
|
if command not in self.disabled_commands:
|
|
|
|
self.disabled_commands.append(command)
|
|
|
|
self.chat.disabled_commands = rapidjson.dumps(self.disabled_commands)
|
2021-03-23 20:55:54 -04:00
|
|
|
self.chat.save()
|
|
|
|
|
|
|
|
|
2020-08-24 12:07:23 -04:00
|
|
|
class Database():
|
2023-10-12 19:22:06 -04:00
|
|
|
cached_chat_wrappers = {}
|
2023-10-12 19:45:15 -04:00
|
|
|
chat_table = Chat
|
|
|
|
blacklisted_user_table = BlacklistedUser
|
|
|
|
sudo_user_table = SudoUser
|
2021-03-23 19:16:21 -04:00
|
|
|
db = DATABASE
|
2020-08-24 17:02:08 -04:00
|
|
|
|
2023-10-12 21:54:01 -04:00
|
|
|
try:
|
|
|
|
blacklisted_users = [user.user_id for user in BlacklistedUser.select().execute()]
|
|
|
|
except BlacklistedUser.DoesNotExist:
|
|
|
|
blacklisted_users = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
sudo_users = [user.user_id for user in SudoUser.select().execute()]
|
|
|
|
except SudoUser.DoesNotExist:
|
|
|
|
sudo_users = []
|
|
|
|
|
2023-10-12 18:05:28 -04:00
|
|
|
# returns a ChatWrapper for a given chat ID
|
2023-10-12 19:22:06 -04:00
|
|
|
def get_chat(self, chat_id: int) -> ChatWrapper:
|
|
|
|
if chat_id in self.cached_chat_wrappers:
|
|
|
|
# new events raise wrappers back to the top
|
|
|
|
self.cached_chat_wrappers[chat_id] = self.cached_chat_wrappers.pop(chat_id)
|
|
|
|
return self.cached_chat_wrappers[chat_id]
|
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
try:
|
2023-10-12 19:22:06 -04:00
|
|
|
chat = Chat.get_by_id(chat_id)
|
2021-03-23 19:16:21 -04:00
|
|
|
except Chat.DoesNotExist:
|
|
|
|
chat = Chat.create(chat_id=chat_id)
|
|
|
|
chat.save()
|
2023-10-12 19:22:06 -04:00
|
|
|
|
|
|
|
while len(self.cached_chat_wrappers) >= CHAT_WRAPPER_CACHE_LIMIT:
|
|
|
|
self.cached_chat_wrappers.pop(next(iter(self.cached_chat_wrappers)))
|
|
|
|
|
|
|
|
self.cached_chat_wrappers[chat_id] = (chat_db := ChatWrapper(chat))
|
|
|
|
return chat_db
|
2020-08-24 17:02:08 -04:00
|
|
|
|
2021-03-23 19:16:21 -04:00
|
|
|
# sudo functions
|
2023-10-12 21:54:01 -04:00
|
|
|
def sudo_user(self, user_id: int):
|
2021-03-24 17:17:49 -04:00
|
|
|
try:
|
|
|
|
SudoUser.create(user_id=user_id)
|
2023-10-12 21:54:01 -04:00
|
|
|
|
|
|
|
if user_id not in self.sudo_users:
|
|
|
|
self.sudo_users.append(user_id)
|
2021-03-24 17:17:49 -04:00
|
|
|
except IntegrityError:
|
|
|
|
pass
|
2021-03-23 19:16:21 -04:00
|
|
|
|
2023-10-12 21:54:01 -04:00
|
|
|
def unsudo_user(self, user_id: int):
|
2021-03-23 19:16:21 -04:00
|
|
|
SudoUser.delete_by_id(user_id)
|
2021-03-23 20:55:54 -04:00
|
|
|
|
2023-10-12 21:54:01 -04:00
|
|
|
if user_id in self.sudo_users:
|
|
|
|
self.sudo_users.remove(user_id)
|
2021-03-23 19:16:21 -04:00
|
|
|
|
2023-10-12 21:54:01 -04:00
|
|
|
# blacklist functions
|
|
|
|
def blacklist_user(self, user_id: int):
|
2021-03-24 17:17:49 -04:00
|
|
|
try:
|
|
|
|
BlacklistedUser.create(user_id=user_id)
|
2023-10-12 21:54:01 -04:00
|
|
|
|
|
|
|
if user_id not in self.blacklisted_users:
|
|
|
|
self.blacklisted_users.append(user_id)
|
2021-03-24 17:17:49 -04:00
|
|
|
except IntegrityError:
|
|
|
|
pass
|
2021-03-23 19:16:21 -04:00
|
|
|
|
2023-10-12 21:54:01 -04:00
|
|
|
def unblacklist_user(self, user_id: int):
|
2021-03-23 19:16:21 -04:00
|
|
|
BlacklistedUser.delete_by_id(user_id)
|
2023-10-12 21:54:01 -04:00
|
|
|
|
|
|
|
if user_id in self.blacklisted_users:
|
|
|
|
self.blacklisted_users.remove(user_id)
|