mirror of
https://github.com/Nick80835/microbot
synced 2025-09-04 16:35:35 +00:00
improve sudo/blacklist handling
This commit is contained in:
@@ -313,7 +313,7 @@ class CommandHandler():
|
|||||||
return str(event.sender_id) in self.settings.get_list("owner_id")
|
return str(event.sender_id) in self.settings.get_list("owner_id")
|
||||||
|
|
||||||
def is_sudo(self, event):
|
def is_sudo(self, event):
|
||||||
return event.sender_id in self.db.get_sudo_list()
|
return event.sender_id in self.db.sudo_users
|
||||||
|
|
||||||
def is_blacklisted(self, event, inline=False):
|
def is_blacklisted(self, event, inline=False):
|
||||||
if inline:
|
if inline:
|
||||||
@@ -321,4 +321,4 @@ class CommandHandler():
|
|||||||
else:
|
else:
|
||||||
user_id = event.sender_id
|
user_id = event.sender_id
|
||||||
|
|
||||||
return user_id in self.db.get_blacklist_list()
|
return user_id in self.db.blacklisted_users
|
||||||
|
@@ -126,6 +126,16 @@ class Database():
|
|||||||
sudo_user_table = SudoUser
|
sudo_user_table = SudoUser
|
||||||
db = DATABASE
|
db = DATABASE
|
||||||
|
|
||||||
|
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 = []
|
||||||
|
|
||||||
# returns a ChatWrapper for a given chat ID
|
# returns a ChatWrapper for a given chat ID
|
||||||
def get_chat(self, chat_id: int) -> ChatWrapper:
|
def get_chat(self, chat_id: int) -> ChatWrapper:
|
||||||
if chat_id in self.cached_chat_wrappers:
|
if chat_id in self.cached_chat_wrappers:
|
||||||
@@ -146,39 +156,33 @@ class Database():
|
|||||||
return chat_db
|
return chat_db
|
||||||
|
|
||||||
# sudo functions
|
# sudo functions
|
||||||
@staticmethod
|
def sudo_user(self, user_id: int):
|
||||||
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:
|
try:
|
||||||
SudoUser.create(user_id=user_id)
|
SudoUser.create(user_id=user_id)
|
||||||
|
|
||||||
|
if user_id not in self.sudo_users:
|
||||||
|
self.sudo_users.append(user_id)
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
def unsudo_user(self, user_id: int):
|
||||||
def unsudo_user(user_id: int):
|
|
||||||
SudoUser.delete_by_id(user_id)
|
SudoUser.delete_by_id(user_id)
|
||||||
|
|
||||||
#blacklist functions
|
if user_id in self.sudo_users:
|
||||||
@staticmethod
|
self.sudo_users.remove(user_id)
|
||||||
def get_blacklist_list() -> list:
|
|
||||||
try:
|
|
||||||
return [user.user_id for user in BlacklistedUser.select().execute()]
|
|
||||||
except BlacklistedUser.DoesNotExist:
|
|
||||||
return []
|
|
||||||
|
|
||||||
@staticmethod
|
# blacklist functions
|
||||||
def blacklist_user(user_id: int):
|
def blacklist_user(self, user_id: int):
|
||||||
try:
|
try:
|
||||||
BlacklistedUser.create(user_id=user_id)
|
BlacklistedUser.create(user_id=user_id)
|
||||||
|
|
||||||
|
if user_id not in self.blacklisted_users:
|
||||||
|
self.blacklisted_users.append(user_id)
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
def unblacklist_user(self, user_id: int):
|
||||||
def unblacklist_user(user_id: int):
|
|
||||||
BlacklistedUser.delete_by_id(user_id)
|
BlacklistedUser.delete_by_id(user_id)
|
||||||
|
|
||||||
|
if user_id in self.blacklisted_users:
|
||||||
|
self.blacklisted_users.remove(user_id)
|
||||||
|
@@ -196,7 +196,7 @@ async def rem_blacklist(event):
|
|||||||
|
|
||||||
@ldr.add("showblacklist", sudo=True, hide_help=True)
|
@ldr.add("showblacklist", sudo=True, hide_help=True)
|
||||||
async def show_blacklist(event):
|
async def show_blacklist(event):
|
||||||
blacklist_string = "\n".join([str(user_id) for user_id in ldr.db.get_blacklist_list()])
|
blacklist_string = "\n".join([str(user_id) for user_id in ldr.db.blacklisted_users])
|
||||||
|
|
||||||
await event.reply(f"**Blacklisted users:**\n\n{blacklist_string}")
|
await event.reply(f"**Blacklisted users:**\n\n{blacklist_string}")
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ async def rem_sudo(event):
|
|||||||
|
|
||||||
@ldr.add("showsudo", sudo=True, hide_help=True)
|
@ldr.add("showsudo", sudo=True, hide_help=True)
|
||||||
async def show_sudo(event):
|
async def show_sudo(event):
|
||||||
sudo_string = "\n".join([str(user_id) for user_id in ldr.db.get_sudo_list()])
|
sudo_string = "\n".join([str(user_id) for user_id in ldr.db.sudo_users])
|
||||||
await event.reply(f"**Sudo users:**\n\n{sudo_string}")
|
await event.reply(f"**Sudo users:**\n\n{sudo_string}")
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user