diff --git a/ubot/command.py b/ubot/command.py index 4a00ba4..2fa47a2 100644 --- a/ubot/command.py +++ b/ubot/command.py @@ -17,7 +17,7 @@ class Command: self.moderation = args.get("moderation", False) self.owner = args.get("owner", False) self.sudo = args.get("sudo", False) - self.admin = args.get("admin", False) or self.moderation + self.admin = args.get("admin", False) self.nsfw = args.get("nsfw", False) self.nsfw_warning = args.get("nsfw_warning", None) self.pass_nsfw = args.get("pass_nsfw", False) diff --git a/ubot/fixes/utils.py b/ubot/fixes/utils.py index de2a924..a7ddf84 100644 --- a/ubot/fixes/utils.py +++ b/ubot/fixes/utils.py @@ -1,17 +1,31 @@ from datetime import timedelta -from telethon.tl.types import Channel, Chat +from telethon.tl.types import Channel, Chat, MessageEntityMentionName async def get_user(event, allow_channel=False): - if event.args: + if mention_entities := [i for i in event.get_entities_text() if isinstance(i, MessageEntityMentionName)]: + if len(mention_entities) > 1: + await event.reply("You provided too many arguments!") + return + + try: + user = await event.client.get_input_entity(mention_entities[0].user_id) + + if isinstance(user, Chat) or (isinstance(user, Channel) and not allow_channel): + raise TypeError + + return user + except (ValueError, TypeError): + await event.reply("There was an error getting the user's ID!") + elif event.args: if event.args.isnumeric(): user_id = int(event.args) else: user_id = None try: - user = await event.client.get_entity(user_id or event.args) + user = await event.client.get_input_entity(user_id or event.args) if isinstance(user, Chat) or (isinstance(user, Channel) and not allow_channel): raise TypeError @@ -19,13 +33,12 @@ async def get_user(event, allow_channel=False): return user except (ValueError, TypeError): await event.reply("The ID or username you provided was invalid!") - return elif event.is_reply: reply = await event.get_reply_message() if reply and reply.sender_id: try: - user = await event.client.get_entity(reply.sender_id) + user = await event.client.get_input_entity(reply.sender_id) if isinstance(user, Chat) or (isinstance(user, Channel) and not allow_channel): raise TypeError @@ -39,7 +52,6 @@ async def get_user(event, allow_channel=False): return else: await event.reply("Give me a user ID, username or reply!") - return def parse_time(time_num: int, unit: str) -> timedelta: diff --git a/ubot/modules/_sudo.py b/ubot/modules/_sudo.py index 7c66347..d1625bf 100644 --- a/ubot/modules/_sudo.py +++ b/ubot/modules/_sudo.py @@ -172,7 +172,7 @@ async def shutdown(event): @ldr.add("blacklist", sudo=True, hide_help=True) async def add_blacklist(event): - user_entity = await get_user(event) + user_entity = await event.client.get_entity(await get_user(event)) if not user_entity: return @@ -186,7 +186,7 @@ async def add_blacklist(event): @ldr.add("unblacklist", sudo=True, hide_help=True) async def rem_blacklist(event): - user_entity = await get_user(event) + user_entity = await event.client.get_entity(await get_user(event)) if not user_entity: return @@ -207,7 +207,7 @@ async def show_blacklist(event): @ldr.add("sudo", owner=True, hide_help=True) async def add_sudo(event): - user_entity = await get_user(event) + user_entity = await event.client.get_entity(await get_user(event)) if not user_entity: return @@ -221,7 +221,7 @@ async def add_sudo(event): @ldr.add("unsudo", owner=True, hide_help=True) async def rem_sudo(event): - user_entity = await get_user(event) + user_entity = await event.client.get_entity(await get_user(event)) if not user_entity: return diff --git a/ubot/modules/fun.py b/ubot/modules/fun.py index 6199fa2..60256f9 100644 --- a/ubot/modules/fun.py +++ b/ubot/modules/fun.py @@ -78,16 +78,6 @@ async def moo(event): await event.reply(choice(moo_answers)) -@ldr.add("kickme", fun=True) -async def kickme(event): - await event.reply("LOLE") - - try: - await event.client.kick_participant(event.chat, await event.get_sender()) - except: - pass - - @ldr.add("kys", fun=True) async def kys(event): await event.reply("Keep yourself safe!") diff --git a/ubot/modules/moderation.py b/ubot/modules/moderation.py index 2d00a76..f17a63e 100644 --- a/ubot/modules/moderation.py +++ b/ubot/modules/moderation.py @@ -6,16 +6,23 @@ from telethon.errors import UserAdminInvalidError from ubot import ldr from ubot.fixes.utils import get_user, parse_time +bot_name = ldr.settings.get_config("bot_name") or "bot" time_regex = compile(r"(?:^| )(?:for )?(\d+) ?(m(?:ins?|inutes?)?|h(?:rs?|ours?)?|d(?:ays?)?)$", IGNORECASE) @ldr.add("kick", moderation=True, help="Kick a user.") +@ldr.add(f"{bot_name}(,|) kick", moderation=True, simple_pattern=True, hide_help=True) async def kick_user(event): if not (await event.client.get_permissions(event.chat, "me")).ban_users: await event.reply("I can't kick users in this chat.") return - user_to_kick = await get_user(event, allow_channel=True) + if time_regex.sub("", event.args).strip().lower() == "me": + self_harm = True + user_to_kick = await event.get_sender() + else: + self_harm = False + user_to_kick = await get_user(event, allow_channel=True) try: if user_to_kick: @@ -30,18 +37,19 @@ async def kick_user(event): await event.reply("I won't kick an admin.") return - if not admin_perms.ban_users: + if not admin_perms.ban_users and not self_harm: await event.reply("You don't have the rights to kick users.") return await event.client.edit_permissions(event.chat, user_to_kick, view_messages=False) await event.client.edit_permissions(event.chat, user_to_kick, view_messages=True) - await event.reply(f"Successfully kicked {user_to_kick.id}!") + await event.reply("Cya!" if self_harm else f"Successfully kicked {user_to_kick.id}!") except UserAdminInvalidError: await event.reply("I can't kick them!") @ldr.add("ban", moderation=True, help="Ban a user forever, or for a certain amount of time given at the end of the command like 30m, 12h or 5d.") +@ldr.add(f"{bot_name}(,|) ban", moderation=True, simple_pattern=True, hide_help=True) async def ban_user(event): if not (await event.client.get_permissions(event.chat, "me")).ban_users: await event.reply("I can't ban users in this chat.") @@ -50,6 +58,10 @@ async def ban_user(event): if time_match := time_regex.search(event.args): event.args = time_regex.sub("", event.args).strip() + if time_regex.sub("", event.args).strip().lower() == "me": + await event.reply("I don't think I should do that…") + return + user_to_ban = await get_user(event, allow_channel=True) try: @@ -82,6 +94,7 @@ async def ban_user(event): @ldr.add("unban", moderation=True, help="Unban a user.") +@ldr.add(f"{bot_name}(,|) unban", moderation=True, simple_pattern=True, hide_help=True) async def unban_user(event): if not (await event.client.get_permissions(event.chat, "me")).ban_users: await event.reply("I can't unban users in this chat.") @@ -104,6 +117,7 @@ async def unban_user(event): @ldr.add("mute", moderation=True, help="Mute a user forever, or for a certain amount of time given at the end of the command like 30m, 12h or 5d.") +@ldr.add(f"{bot_name}(,|) mute", moderation=True, simple_pattern=True, hide_help=True) async def mute_user(event): if not (await event.client.get_permissions(event.chat, "me")).ban_users: await event.reply("I can't mute users in this chat.") @@ -112,7 +126,12 @@ async def mute_user(event): if time_match := time_regex.search(event.args): event.args = time_regex.sub("", event.args).strip() - user_to_mute = await get_user(event, allow_channel=True) + if time_regex.sub("", event.args).strip().lower() == "me": + self_harm = True + user_to_mute = await event.get_sender() + else: + self_harm = False + user_to_mute = await get_user(event, allow_channel=True) try: if user_to_mute: @@ -127,24 +146,37 @@ async def mute_user(event): await event.reply("I won't mute an admin.") return - if not admin_perms.ban_users: + if not admin_perms.ban_users and not self_harm: await event.reply("You don't have the rights to mute users.") return if time_match: mute_length = parse_time(int(time_match.group(1)), time_match.group(2)[0]) await event.client.edit_permissions(event.chat, user_to_mute, send_messages=False, until_date=mute_length) - await event.reply(f"Successfully muted {user_to_mute.id} until {(datetime.now(timezone.utc) + mute_length).strftime('%H:%M %b %d, %Y UTC')}!") + + if self_harm: + await event.reply(f"Since you asked nicely, I've muted you until {(datetime.now(timezone.utc) + mute_length).strftime('%H:%M %b %d, %Y UTC')}!") + else: + await event.reply(f"Successfully muted {user_to_mute.id} until {(datetime.now(timezone.utc) + mute_length).strftime('%H:%M %b %d, %Y UTC')}!") + return - await event.client.edit_permissions(event.chat, user_to_mute, send_messages=False) - await event.reply(f"Successfully muted {user_to_mute.id} for all of eternity!") + if self_harm: + await event.reply("I don't think I should do that…") + else: + await event.client.edit_permissions(event.chat, user_to_mute, send_messages=False) + await event.reply(f"Successfully muted {user_to_mute.id} for all of eternity!") except UserAdminInvalidError: await event.reply("I can't mute them!") @ldr.add("unmute", moderation=True, help="Unmute a user.") +@ldr.add(f"{bot_name}(,|) unmute", moderation=True, simple_pattern=True, hide_help=True) async def unmute_user(event): + if event.args.lower() == "me": + await event.reply("You probably aren't muted.") + return + if not (await event.client.get_permissions(event.chat, "me")).ban_users: await event.reply("I can't unmute users in this chat.") return