2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-22 18:19:16 +00:00
microbot/ubot/database.py

181 lines
4.6 KiB
Python
Raw Normal View History

2023-04-17 21:46:34 -04:00
import rapidjson
from peewee import (BigIntegerField, BooleanField, IntegrityError, Model,
SqliteDatabase, TextField)
2020-08-24 17:02:08 -04:00
2021-03-24 11:18:00 -04:00
DATABASE = SqliteDatabase("database.sqlite", pragmas={
"journal_mode": "wal",
"cache_size": -1024 * 16}
)
class BaseDB(Model):
class Meta:
database = DATABASE
class BlacklistedUser(BaseDB):
user_id = BigIntegerField(unique=True, primary_key=True)
class SudoUser(BaseDB):
user_id = BigIntegerField(unique=True, primary_key=True)
class Chat(BaseDB):
chat_id = BigIntegerField(unique=True, primary_key=True)
fun_enabled = BooleanField(default=True)
nsfw_enabled = BooleanField(default=True)
2023-02-05 09:46:06 -05:00
spoiler_nsfw = BooleanField(default=False)
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)
DATABASE.connect()
DATABASE.create_tables([
Chat,
BlacklistedUser,
SudoUser
])
2020-08-24 12:07:23 -04:00
class ChatWrapper():
def __init__(self, chat: Chat):
self.chat = chat
# custom prefix functions
@property
def prefix(self) -> str:
return self.chat.custom_prefix
@prefix.setter
def prefix(self, prefix: str):
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
def modmode_enabled(self) -> str:
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()
# fun command functions
@property
def fun_enabled(self) -> bool:
return self.chat.fun_enabled
@fun_enabled.setter
def fun_enabled(self, enabled: bool):
self.chat.fun_enabled = enabled
self.chat.save()
# nsfw command functions
@property
def nsfw_enabled(self) -> bool:
return self.chat.nsfw_enabled
@nsfw_enabled.setter
def nsfw_enabled(self, enabled: bool):
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()
# disable/enable command functions
@staticmethod
def _get_disabled_commands(chat: Chat) -> list:
2023-04-17 21:46:34 -04:00
return rapidjson.loads(chat.disabled_commands)
def disabled_commands(self) -> list:
return self._get_disabled_commands(self.chat)
def enable_command(self, command: str):
disabled_commands = self._get_disabled_commands(self.chat)
if command in disabled_commands:
disabled_commands.remove(command)
2023-04-17 21:46:34 -04:00
self.chat.disabled_commands = rapidjson.dumps(disabled_commands)
self.chat.save()
def disable_command(self, command: str):
disabled_commands = self._get_disabled_commands(self.chat)
if command not in disabled_commands:
disabled_commands.append(command)
2023-04-17 21:46:34 -04:00
self.chat.disabled_commands = rapidjson.dumps(disabled_commands)
self.chat.save()
2020-08-24 12:07:23 -04:00
class Database():
db = DATABASE
2020-08-24 17:02:08 -04:00
2023-10-12 18:05:28 -04:00
# returns a ChatWrapper for a given chat ID
@staticmethod
2023-10-12 18:05:28 -04:00
def get_chat(chat_id: int) -> ChatWrapper:
try:
2023-10-12 18:05:28 -04:00
return ChatWrapper(Chat.get_by_id(chat_id))
except Chat.DoesNotExist:
chat = Chat.create(chat_id=chat_id)
chat.save()
2023-10-12 18:05:28 -04:00
return ChatWrapper(chat)
2020-08-24 17:02:08 -04:00
# sudo functions
@staticmethod
def get_sudo_list() -> list:
try:
return [user.user_id for user in SudoUser.select().execute()]
except SudoUser.DoesNotExist:
return []
@staticmethod
def sudo_user(user_id: int):
try:
SudoUser.create(user_id=user_id)
except IntegrityError:
pass
@staticmethod
def unsudo_user(user_id: int):
SudoUser.delete_by_id(user_id)
#blacklist functions
@staticmethod
def get_blacklist_list() -> list:
try:
return [user.user_id for user in BlacklistedUser.select().execute()]
except BlacklistedUser.DoesNotExist:
return []
@staticmethod
def blacklist_user(user_id: int):
try:
BlacklistedUser.create(user_id=user_id)
except IntegrityError:
pass
@staticmethod
def unblacklist_user(user_id: int):
BlacklistedUser.delete_by_id(user_id)