Kaynağa Gözat

/t[ban|mute] and time converter (#90)

* time converter for t[ban|mute]

* /t[mute|ban]

* condition for checking time duration
iamajneya 5 yıl önce
ebeveyn
işleme
fc6c358cd7
2 değiştirilmiş dosya ile 78 ekleme ve 15 silme
  1. 53 15
      wbb/modules/admin.py
  2. 25 0
      wbb/utils/functions.py

+ 53 - 15
wbb/modules/admin.py

@@ -31,11 +31,12 @@ from wbb.core.decorators.errors import capture_err
 from wbb.core.keyboard import ikb
 from wbb.utils.dbfunctions import (add_warn, get_warn, int_to_alpha,
                                    remove_warns)
-from wbb.utils.functions import extract_user, extract_user_and_reason
+from wbb.utils.functions import extract_user, extract_user_and_reason, time_converter
 
 __MODULE__ = "Admin"
 __HELP__ = """/ban - Ban A User
 /dban - Delete the replied message banning its sender
+/tban - Ban A User For Specific Time
 /unban - Unban A User
 /warn - Warn A User
 /dwarn - Delete the replied message warning its sender
@@ -50,8 +51,7 @@ __HELP__ = """/ban - Ban A User
 /demote - Demote A Member
 /pin - Pin A Message
 /mute - Mute A User
-/dmute - Delete the replied message muting its sender
-/smute - Mute a user silently
+/tmute - Mute A User For Specific Time
 /unmute - Unmute A User
 /ban_ghosts - Ban Deleted Accounts
 /report | @admins - Report A Message To Admins."""
@@ -209,7 +209,7 @@ async def kickFunc(_, message: Message):
 
 
 @app.on_message(
-    filters.command(["ban", "dban"]) & ~filters.edited & ~filters.private
+    filters.command(["ban", "dban", "tban"]) & ~filters.edited & ~filters.private
 )
 @adminsOnly("can_restrict_members")
 async def banFunc(_, message: Message):
@@ -229,13 +229,30 @@ async def banFunc(_, message: Message):
             "I can't ban an admin, You know the rules, so do i."
         )
     mention = (await app.get_users(user_id)).mention
-    msg = f"""
-**Banned User:** {mention}
-**Banned By:** {message.from_user.mention if message.from_user else 'Anon'}
-**Reason:** {reason or 'No Reason Provided.'}"""
-    await message.chat.kick_member(user_id)
+    msg = f"**Banned User:** {mention}\n" \
+          f"**Banned By:** {message.from_user.mention if message.from_user else 'Anon'}\n"
     if message.command[0][0] == "d":
         await message.reply_to_message.delete()
+    if message.command[0] == "tban":
+        split = reason.split(None, 1)
+        time_value = split[0]
+        temp_reason = split[1] if len(split) > 1 else ""
+        temp_ban = await time_converter(message, time_value)
+        msg += f"**Banned For:** {time_value}\n"
+        if temp_reason:
+            msg += f"**Reason:** {temp_reason}"
+        try:
+            if len(time_value[:-1]) < 3:
+                await message.chat.kick_member(user_id, until_date=temp_ban)
+                await message.reply_text(msg)
+            else:
+                await message.reply_text("You can't use more than 99")
+        except AttributeError:
+            pass
+        return
+    if reason:
+        msg += f"**Reason:** {reason}"
+    await message.chat.kick_member(user_id)
     await message.reply_text(msg)
 
 
@@ -365,7 +382,7 @@ async def pin(_, message: Message):
 
 
 @app.on_message(
-    filters.command("mute") & ~filters.edited & ~filters.private
+    filters.command(["mute", "tmute"]) & ~filters.edited & ~filters.private
 )
 @adminsOnly("can_restrict_members")
 async def mute(_, message: Message):
@@ -387,11 +404,32 @@ async def mute(_, message: Message):
     )
     mention = (await app.get_users(user_id)).mention
     keyboard = ikb([[("🚨   Unmute   🚨", f"unmute_{user_id}")]])
-    msg = f"""
-**Muted User:** {mention}
-**Muted By:** {message.from_user.mention if message.from_user else 'Anon'}
-**Reason:** {reason or 'No Reason Provided.'}"""
-
+    msg = f"**Muted User:** {mention}\n" \
+          f"**Muted By:** {message.from_user.mention if message.from_user else 'Anon'}\n"
+    if message.command[0] == "tmute":
+        split = reason.split(None, 1)
+        time_value = split[0]
+        temp_reason = split[1] if len(split) > 1 else ""
+        temp_mute = await time_converter(message, time_value)
+        msg += f"**Muted For:** {time_value}\n"
+        if temp_reason:
+            msg += f"**Reason:** {temp_reason}"
+        try:
+            if len(time_value[:-1]) < 3:
+                await message.chat.restrict_member(
+                    user_id, permissions=ChatPermissions(), until_date=temp_mute,
+                )
+                await message.reply_text(msg, reply_markup=keyboard)
+            else:
+                await message.reply_text("You can't use more than 99")
+        except AttributeError:
+            pass
+        return
+    if reason:
+        msg += f"**Reason:** {reason}"
+    await message.chat.restrict_member(
+        user_id, permissions=ChatPermissions()
+    )
     await message.reply_text(msg, reply_markup=keyboard)
 
 

+ 25 - 0
wbb/utils/functions.py

@@ -29,6 +29,7 @@ from math import atan2, cos, radians, sin, sqrt
 from random import randint
 from re import findall
 from time import time
+from datetime import timedelta, datetime
 
 import aiofiles
 import aiohttp
@@ -195,6 +196,30 @@ def get_urls_from_text(text: str) -> bool:
     return [x[0] for x in findall(regex, text)]
 
 
+async def time_converter(message: Message, time_value: str) -> int:
+    unit = ["m", "h", "d"]  # m == minutes | h == hours | d == days
+    check_unit = ''.join(list(filter(time_value[-1].lower().endswith, unit)))
+    currunt_time = datetime.now()
+    time_digit = time_value[:-1]
+    if not time_digit.isdigit():
+        return await message.reply_text("Incorrect time specified")
+    if check_unit == "m":
+        temp_time = currunt_time + timedelta(
+            minutes=int(time_digit)
+        )
+    elif check_unit == "h":
+        temp_time = currunt_time + timedelta(
+            hours=int(time_digit)
+        )
+    elif check_unit == "d":
+        temp_time = currunt_time + timedelta(
+            days=int(time_digit)
+        )
+    else:
+        return await message.reply_text("Incorrect time specified.")
+    return int(datetime.timestamp(temp_time))
+
+
 async def extract_userid(message, text: str):
     text = text.strip()
     entities = message.entities