Skip to content

Commit

Permalink
Refactor authorized user
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirarism authored Sep 26, 2024
1 parent cd5ab46 commit b3c8baa
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 80 deletions.
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"description": "Allowed user in to use sudo command.",
"value": ""
},
"OWNER_ID": {
"description": "OWNER OF THIS BOT.",
"value": ""
},
"GOOGLE_AI_KEY": {
"description": "Your Gemini AI Key",
"value": "",
Expand Down
4 changes: 2 additions & 2 deletions database/feds_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytz

from database import dbname
from misskaty.vars import SUDO
from misskaty.vars import SUDO, OWNER_ID

fedsdb = dbname["federation"]

Expand Down Expand Up @@ -64,7 +64,7 @@ async def is_user_fed_owner(fed_id, user_id: int):
if not getfed:
return False
owner_id = getfed["owner_id"]
return user_id == owner_id or user_id not in SUDO
return user_id == owner_id or user_id not in SUDO or user_id != OWNER_ID


async def search_fed_by_id(fed_id):
Expand Down
18 changes: 6 additions & 12 deletions misskaty/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from misskaty.plugins import ALL_MODULES
from misskaty.plugins.web_scraper import web
from misskaty.vars import SUDO, USER_SESSION
from misskaty.vars import OWNER_ID, USER_SESSION
from utils import auto_clean

LOGGER = getLogger("MissKaty")
Expand Down Expand Up @@ -59,17 +59,11 @@ async def start_bot():
LOGGER.info("[INFO]: BOT STARTED AS @%s!", BOT_USERNAME)
try:
LOGGER.info("[INFO]: SENDING ONLINE STATUS")
for i in SUDO:
if USER_SESSION:
await app.send_message(
i,
f"USERBOT AND BOT STARTED with Pyrogram v{__version__}..\nUserBot: {UBOT_NAME}\nBot: {BOT_NAME}\n\nwith Pyrogram v{__version__} (Layer {layer}) started on @{BOT_USERNAME}.\n\n<code>{bot_modules}</code>",
)
else:
await app.send_message(
i,
f"BOT STARTED with Pyrogram v{__version__} as {BOT_NAME}\n\nwith Pyrogram v{__version__} (Layer {layer}) started on @{BOT_USERNAME}.\n\n<code>{bot_modules}</code>",
)
if USER_SESSION:
await app.send_message(
OWNER_ID,
f"USERBOT AND BOT STARTED with Pyrogram v{__version__}..\nUserBot: {UBOT_NAME}\nBot: {BOT_NAME}\n\nwith Pyrogram v{__version__} (Layer {layer}) started on @{BOT_USERNAME}.\n\n<code>{bot_modules}</code>",
)
except Exception as e:
LOGGER.error(str(e))
scheduler.start()
Expand Down
4 changes: 2 additions & 2 deletions misskaty/core/decorator/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from misskaty import app
from misskaty.helper.sqlite_helper import Cache
from misskaty.vars import SUDO
from misskaty.vars import SUDO, OWNER_ID

from ...helper.localization import (
default_language,
Expand Down Expand Up @@ -165,7 +165,7 @@ async def subFunc2(client, message: Message, *args, **kwargs):
# For admins and sudo users
userID = message.from_user.id
permissions = await member_permissions(chatID, userID)
if userID not in SUDO and permission not in permissions:
if userID not in SUDO or userID != OWNER_ID and permission not in permissions:
return await unauthorised(message, permission, subFunc2)
return await authorised(func, subFunc2, client, message, *args, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions misskaty/core/pyro_cooldown.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pyrogram import filters
from pyrogram.errors import MessageDeleteForbidden

from misskaty.vars import SUDO
from misskaty.vars import SUDO, OWNER_ID

data = {}

Expand All @@ -28,7 +28,7 @@ async def task(msg, warn=False, sec=None):
def wait(sec):
async def ___(flt, _, msg):
user_id = msg.from_user.id if msg.from_user else msg.sender_chat.id
if user_id in SUDO:
if user_id in SUDO or user_id == OWNER_ID:
return True
if user_id in data:
if msg.date.timestamp() >= data[user_id]["timestamp"] + flt.data:
Expand Down
18 changes: 9 additions & 9 deletions misskaty/plugins/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
time_converter,
)
from misskaty.helper.localization import use_chat_lang
from misskaty.vars import COMMAND_HANDLER, SUDO
from misskaty.vars import COMMAND_HANDLER, SUDO, OWNER_ID

LOGGER = getLogger("MissKaty")

Expand Down Expand Up @@ -172,7 +172,7 @@ async def kickFunc(client: Client, ctx: Message, strings) -> "Message":
return await ctx.reply_msg(strings("user_not_found"))
if user_id == client.me.id:
return await ctx.reply_msg(strings("kick_self_err"))
if user_id in SUDO:
if user_id in SUDO or user_id == OWNER_ID:
return await ctx.reply_msg(strings("kick_sudo_err"))
if user_id in (await list_admins(ctx.chat.id)):
return await ctx.reply_msg(strings("kick_admin_err"))
Expand Down Expand Up @@ -213,7 +213,7 @@ async def banFunc(client, message, strings):
return await message.reply_text(strings("user_not_found"))
if user_id == client.me.id:
return await message.reply_text(strings("ban_self_err"))
if user_id in SUDO:
if user_id in SUDO or user_id == OWNER_ID:
return await message.reply_text(strings("ban_sudo_err"))
if user_id in (await list_admins(message.chat.id)):
return await message.reply_text(strings("ban_admin_err"))
Expand Down Expand Up @@ -299,7 +299,7 @@ async def unban_func(_, message, strings):

# Ban users listed in a message
@app.on_message(
filters.user(SUDO) & filters.command("listban", COMMAND_HANDLER) & filters.group
(filters.user(SUDO) | filters.user(OWNER_ID)) & filters.command("listban", COMMAND_HANDLER) & filters.group
)
@use_chat_lang()
async def list_ban_(c, message, strings):
Expand All @@ -319,7 +319,7 @@ async def list_ban_(c, message, strings):

if userid == c.me.id:
return await message.reply_text(strings("ban_self_err"))
if userid in SUDO:
if userid in SUDO or user_id == OWNER_ID:
return await message.reply_text(strings("ban_sudo_err"))
splitted = messagelink.split("/")
uname, mid = splitted[-2], int(splitted[-1])
Expand Down Expand Up @@ -353,7 +353,7 @@ async def list_ban_(c, message, strings):

# Unban users listed in a message
@app.on_message(
filters.user(SUDO) & filters.command("listunban", COMMAND_HANDLER) & filters.group
(filters.user(SUDO) | filters.user(OWNER_ID)) & filters.command("listunban", COMMAND_HANDLER) & filters.group
)
@use_chat_lang()
async def list_unban(_, message, strings):
Expand Down Expand Up @@ -469,7 +469,7 @@ async def demote(client, message, strings):
return await message.reply_text(strings("user_not_found"))
if user_id == client.me.id:
return await message.reply_text(strings("demote_self_err"))
if user_id in SUDO:
if user_id in SUDO or user_id == OWNER_ID:
return await message.reply_text(strings("demote_sudo_err"))
try:
await message.chat.promote_member(
Expand Down Expand Up @@ -535,7 +535,7 @@ async def mute(client, message, strings):
return await message.reply_text(strings("user_not_found"))
if user_id == client.me.id:
return await message.reply_text(strings("mute_self_err"))
if user_id in SUDO:
if user_id in SUDO or user_id == OWNER_ID:
return await message.reply_text(strings("mute_sudo_err"))
if user_id in (await list_admins(message.chat.id)):
return await message.reply_text(strings("mute_admin_err"))
Expand Down Expand Up @@ -606,7 +606,7 @@ async def warn_user(client, message, strings):
return await message.reply_text(strings("user_not_found"))
if user_id == client.me.id:
return await message.reply_text(strings("warn_self_err"))
if user_id in SUDO:
if user_id in SUDO or user_id == OWNER_ID:
return await message.reply_text(strings("warn_sudo_err"))
if user_id in (await list_admins(chat_id)):
return await message.reply_text(strings("warn_admin_err"))
Expand Down
4 changes: 2 additions & 2 deletions misskaty/plugins/autoapprove.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from database import dbname
from misskaty import app
from misskaty.core.decorator.permissions import adminsOnly, member_permissions
from misskaty.vars import COMMAND_HANDLER, SUDO
from misskaty.vars import COMMAND_HANDLER, SUDO, OWNER_ID

approvaldb = dbname["autoapprove"]

Expand Down Expand Up @@ -53,7 +53,7 @@ async def approval_cb(_, cb: CallbackQuery):
permissions = await member_permissions(chat_id, from_user.id)
permission = "can_restrict_members"
if permission not in permissions:
if from_user.id not in SUDO:
if from_user.id not in SUDO or from_user.id != OWNER_ID:
return await cb.answer(
f"You don't have the required permission.\n Permission: {permission}",
show_alert=True,
Expand Down
10 changes: 5 additions & 5 deletions misskaty/plugins/ban_user_or_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from database.users_chats_db import db
from misskaty import app
from misskaty.helper.localization import use_chat_lang
from misskaty.vars import COMMAND_HANDLER, LOG_CHANNEL, SUDO, SUPPORT_CHAT
from misskaty.vars import COMMAND_HANDLER, LOG_CHANNEL, SUDO, OWNER_ID, SUPPORT_CHAT


@app.on_message(filters.incoming & filters.private, group=-5)
Expand Down Expand Up @@ -62,7 +62,7 @@ async def grp_bd(self: Client, ctx: Message, strings):
await ctx.stop_propagation()


@app.on_message(filters.command("banuser", COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command("banuser", COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
async def ban_a_user(bot, message):
if len(message.command) == 1:
return await message.reply("Give me a user id / username")
Expand Down Expand Up @@ -99,7 +99,7 @@ async def ban_a_user(bot, message):
)


@app.on_message(filters.command("unbanuser", COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command("unbanuser", COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
async def unban_a_user(bot, message):
if len(message.command) == 1:
return await message.reply("Give me a user id / username")
Expand Down Expand Up @@ -127,7 +127,7 @@ async def unban_a_user(bot, message):
await message.reply(f"Successfully unbanned user {k.mention}!!!")


@app.on_message(filters.command("disablechat", COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command("disablechat", COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
async def disable_chat(bot, message):
if len(message.command) == 1:
return await message.reply("Give me a chat id")
Expand Down Expand Up @@ -166,7 +166,7 @@ async def disable_chat(bot, message):
await message.reply(f"Error - {e}")


@app.on_message(filters.command("enablechat", COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command("enablechat", COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
async def re_enable_chat(_, ctx: Message):
if len(ctx.command) == 1:
return await ctx.reply("Give me a chat id")
Expand Down
4 changes: 2 additions & 2 deletions misskaty/plugins/blacklist_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from misskaty import app
from misskaty.core.decorator.errors import capture_err
from misskaty.core.decorator.permissions import adminsOnly, list_admins
from misskaty.vars import SUDO
from misskaty.vars import SUDO, OWNER_ID

__MODULE__ = "Blacklist"
__HELP__ = """
Expand Down Expand Up @@ -98,7 +98,7 @@ async def blacklist_filters_re(self, message):
user = message.from_user
if not user:
return
if user.id in SUDO:
if user.id in SUDO or user.id == OWNER_ID:
return
list_of_filters = await get_blacklisted_words(chat_id)
for word in list_of_filters:
Expand Down
4 changes: 2 additions & 2 deletions misskaty/plugins/broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from pyrogram.types import Message

from misskaty import DATABASE_URI, app
from misskaty.vars import SUDO
from misskaty.vars import OWNER_ID
from utils import broadcast_messages


@app.on_message(filters.command("broadcast") & filters.user(SUDO) & filters.reply)
@app.on_message(filters.command("broadcast") & filters.user(OWNER_ID) & filters.reply)
async def broadcast(_, ctx: Message):
mongo = AsyncClient(DATABASE_URI)
userdb = mongo["MissKatyBot"]["peers"]
Expand Down
6 changes: 3 additions & 3 deletions misskaty/plugins/chatbot_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from misskaty import app
from misskaty.core import pyro_cooldown
from misskaty.helper import check_time_gap, post_to_telegraph, use_chat_lang
from misskaty.vars import COMMAND_HANDLER, GOOGLEAI_KEY, OPENAI_KEY, SUDO
from misskaty.vars import COMMAND_HANDLER, GOOGLEAI_KEY, OPENAI_KEY, OWNER_ID

__MODULE__ = "ChatBot"
__HELP__ = """
Expand Down Expand Up @@ -123,15 +123,15 @@ async def openai_chatbot(self, ctx: Message, strings):
return await ctx.reply_msg("OPENAI_KEY env is missing!!!")
uid = ctx.from_user.id if ctx.from_user else ctx.sender_chat.id
is_in_gap, _ = await check_time_gap(uid)
if is_in_gap and (uid not in SUDO):
if is_in_gap and (uid not == OWNER_ID):
return await ctx.reply_msg(strings("dont_spam"), del_in=5)
pertanyaan = ctx.input
msg = await ctx.reply_msg(strings("find_answers_str"), quote=True)
if uid not in gptai_conversations:
gptai_conversations[uid] = [{"role": "system", "content": "Kamu adalah AI dengan karakter mirip kucing bernama MissKaty AI yang diciptakan oleh Yasir untuk membantu manusia mencari informasi."}, {"role": "user", "content": pertanyaan}]
else:
gptai_conversations[uid].append({"role": "user", "content": pertanyaan})
ai_response = await get_openai_stream_response(True, OPENAI_KEY, "https://models.inference.ai.azure.com" if uid in SUDO else "https://duckai.yasirapi.eu.org/v1", "gpt-4o" if uid in SUDO else "gpt-4o-mini", gptai_conversations[uid], msg, strings)
ai_response = await get_openai_stream_response(True, OPENAI_KEY, "https://models.inference.ai.azure.com" if uid == OWNER_ID else "https://duckai.yasirapi.eu.org/v1", "gpt-4o" if uid == OWNER_ID else "gpt-4o-mini", gptai_conversations[uid], msg, strings)
if not ai_response:
gptai_conversations[uid].pop()
if len(gptai_conversations[uid]) == 1:
Expand Down
22 changes: 11 additions & 11 deletions misskaty/plugins/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
from misskaty.helper.human_read import get_readable_file_size, get_readable_time
from misskaty.helper.localization import use_chat_lang
from database.payment_db import autopay_update
from misskaty.vars import AUTO_RESTART, COMMAND_HANDLER, LOG_CHANNEL, SUDO, PAYDISINI_CHANNEL_ID, PAYDISINI_KEY
from misskaty.vars import AUTO_RESTART, COMMAND_HANDLER, LOG_CHANNEL, SUDO, OWNER_ID PAYDISINI_CHANNEL_ID, PAYDISINI_KEY

__MODULE__ = "DevCommand"
__HELP__ = """
Expand Down Expand Up @@ -142,7 +142,7 @@ async def refund_star_payment(client: Client, message: Message):
await message.reply_msg(e)


@app.on_message(filters.command(["logs"], COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command(["logs"], COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
@use_chat_lang()
async def log_file(_, ctx: Message, strings):
"""Send log file"""
Expand Down Expand Up @@ -244,7 +244,7 @@ async def donate(self: Client, ctx: Message):


@app.on_message(
filters.command(["balas"], COMMAND_HANDLER) & filters.user(SUDO) & filters.reply
filters.command(["balas"], COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)) & filters.reply
)
async def balas(_, ctx: Message) -> "str":
pesan = ctx.input
Expand Down Expand Up @@ -346,7 +346,7 @@ def draw_progressbar(coordinate, progress):


# Gban
@app.on_message(filters.command("gban", COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command("gban", COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
async def ban_globally(self: Client, ctx: Message):
user_id, reason = await extract_user_and_reason(ctx)
if not user_id:
Expand All @@ -364,7 +364,7 @@ async def ban_globally(self: Client, ctx: Message):

from_user = ctx.from_user

if user_id in [from_user.id, self.me.id] or user_id in SUDO:
if user_id in [from_user.id, self.me.id] or user_id in SUDO or user_id == OWNER_ID:
return await ctx.reply_text("I can't ban that user.")
served_chats = await db.get_all_chats()
m = await ctx.reply_text(
Expand Down Expand Up @@ -412,7 +412,7 @@ async def ban_globally(self: Client, ctx: Message):


# Ungban
@app.on_message(filters.command("ungban", COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command("ungban", COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
async def unban_globally(_, ctx: Message):
user_id = await extract_user(ctx)
if not user_id:
Expand All @@ -434,11 +434,11 @@ async def unban_globally(_, ctx: Message):


@app.on_message(
filters.command(["shell", "sh", "term"], COMMAND_HANDLER) & filters.user(SUDO)
filters.command(["shell", "sh", "term"], COMMAND_HANDLER) & filters.user(OWNER_ID)
)
@app.on_edited_message(
filters.command(["shell", "sh", "term"], COMMAND_HANDLER)
& filters.user(SUDO)
& filters.user(OWNER_ID)
& ~filters.react
)
@user.on_message(filters.command(["shell", "sh", "term"], ".") & filters.me)
Expand Down Expand Up @@ -501,14 +501,14 @@ async def shell_cmd(self: Client, ctx: Message, strings):
filters.command(["ev", "run", "myeval"], COMMAND_HANDLER)
| filters.regex(r"app.run\(\)$")
)
& filters.user(SUDO)
& filters.user(OWNER_ID)
)
@app.on_edited_message(
(
filters.command(["ev", "run", "meval"], COMMAND_HANDLER)
| filters.regex(r"app.run\(\)$")
)
& filters.user(SUDO)
& filters.user(OWNER_ID)
& ~filters.react
)
@user.on_message(filters.command(["ev", "run", "meval"], ".") & filters.me)
Expand Down Expand Up @@ -649,7 +649,7 @@ def _help(*args: Any, **kwargs: Any) -> None:


# Update and restart bot
@app.on_message(filters.command(["restart"], COMMAND_HANDLER) & filters.user(SUDO))
@app.on_message(filters.command(["restart"], COMMAND_HANDLER) & (filters.user(SUDO) | filters.user(OWNER_ID)))
@use_chat_lang()
async def update_restart(_, ctx: Message, strings):
msg = await ctx.reply_msg(strings("up_and_rest"))
Expand Down
Loading

0 comments on commit b3c8baa

Please sign in to comment.