Skip to content

Commit

Permalink
added temp ban support (UsergeTeam#226)
Browse files Browse the repository at this point in the history
* added temp ban support

* 🤙

* fixed filtered args

* update

* hmm

Co-authored-by: rking32 <[email protected]>
  • Loading branch information
Krishna-Singhal and rking32 authored Jan 7, 2021
1 parent 7fcee77 commit 1f67cdd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
6 changes: 3 additions & 3 deletions userge/core/types/bound/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ def extract_user_and_text(self) -> Tuple[Optional[Union[str, int]], Optional[str
if self.reply_to_message:
if self.reply_to_message.from_user:
user_e = self.reply_to_message.from_user.id
text = self.input_str
text = self.filtered_input_str
return user_e, text
if self.input_str:
data = self.input_str.split(maxsplit=1)
if self.filtered_input_str:
data = self.filtered_input_str.split(maxsplit=1)
# Grab First Word and Process it.
if len(data) == 2:
user, text = data
Expand Down
44 changes: 36 additions & 8 deletions userge/plugins/admin/gadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,50 @@ async def demote_usr(message: Message):
'header': "use this to ban group members",
'description': "Ban member from supergroup.\n"
"[NOTE: Requires proper admin rights in the chat!!!]",
'examples': "{tr}ban [username | userid] or [reply to user] :reason (optional)"},
'flags': {
'-m': "minutes",
'-h': "hours",
'-d': "days"},
'examples': "{tr}ban [flag] [username | userid] or [reply to user] :reason (optional)"},
allow_channels=False, check_restrict_perm=True)
async def ban_usr(message: Message):
async def ban_user(message: Message):
""" ban user from tg group """
reason = ""
chat_id = message.chat.id
await message.edit("`Trying to Ban User.. Hang on!! ⏳`")
reason = ""
user_id, reason = message.extract_user_and_text
if not user_id:
await message.edit(
text="`no valid user_id or message specified,`"
"`do .help ban for more info`", del_in=5)
"`do .help ban for more info`", del_in=5)
return

chat_id = message.chat.id
flags = message.flags
minutes = int(flags.get('-m', 0))
hours = int(flags.get('-h', 0))
days = int(flags.get('-d', 0))

ban_period = 0
_time = "forever"
if minutes:
ban_period = time.time() + minutes * 60
_time = f"{minutes}m"
elif hours:
ban_period = time.time() + hours * 3600
_time = f"{hours}h"
elif days:
ban_period = time.time() + days * 86400
_time = f"{days}d"

try:
get_mem = await message.client.get_chat_member(chat_id, user_id)
await message.client.kick_chat_member(chat_id, user_id)
await message.client.kick_chat_member(chat_id, user_id, int(ban_period))
await message.edit(
"#BAN\n\n"
f"USER: [{get_mem.user.first_name}](tg://user?id={get_mem.user.id}) "
f"(`{get_mem.user.id}`)\n"
f"CHAT: `{message.chat.title}` (`{chat_id}`)\n"
f"TIME: `{_time}`\n"
f"REASON: `{reason}`", log=__name__)
except UsernameInvalid:
await message.edit("`invalid username, try again with valid info ⚠`", del_in=5)
Expand All @@ -150,7 +173,9 @@ async def ban_usr(message: Message):
except UserIdInvalid:
await message.edit("`invalid userid, try again with valid info ⚠`", del_in=5)
except Exception as e_f:
await message.edit(f"`something went wrong! 🤔`\n\n**ERROR:** `{e_f}`", del_in=5)
await message.edit(
"`something went wrong 🤔, do .help ban for more info`\n\n"
f"**ERROR**: `{e_f}`", del_in=5)


@userge.on_cmd("unban", about={
Expand Down Expand Up @@ -252,10 +277,13 @@ async def mute_usr(message: Message):
return
if minutes:
mute_period = int(minutes) * 60
_time = f"{int(minutes)}m"
elif hours:
mute_period = int(hours) * 3600
_time = f"{int(hours)}h"
elif days:
mute_period = int(days) * 86400
_time = f"{int(days)}d"
if flags:
try:
get_mem = await message.client.get_chat_member(chat_id, user_id)
Expand All @@ -268,7 +296,7 @@ async def mute_usr(message: Message):
f"USER: [{get_mem.user.first_name}](tg://user?id={get_mem.user.id}) "
f"(`{get_mem.user.id}`)\n"
f"CHAT: `{message.chat.title}` (`{chat_id}`)\n"
f"MUTE UNTIL: `{minutes} minutes`\n"
f"MUTE UNTIL: `{_time}`\n"
f"REASON: `{reason}`", log=__name__)
except UsernameInvalid:
await message.edit("`invalid username, try again with valid info ⚠`", del_in=5)
Expand Down
3 changes: 2 additions & 1 deletion userge/plugins/utils/pmpermit.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,9 @@ async def pm_callback_block(_, c_q: CallbackQuery):
owner = await userge.get_me()
if c_q.from_user.id == owner.id:
userID = int(c_q.matches[0].group(1))
user_dict = await userge.get_user_dict(userID)
await userge.send_message(
userID, f"{owner.mention} `decided you to block, Sorry.`")
userID, blocked_message.format_map(SafeDict(**user_dict)))
await userge.block_user(userID)
if userID in pmCounter:
del pmCounter[userID]
Expand Down

0 comments on commit 1f67cdd

Please sign in to comment.