iShotaBot/modules/banme.py

91 lines
2.9 KiB
Python
Raw Normal View History

2023-05-31 14:27:03 +00:00
import contextlib
2021-11-29 05:11:21 +00:00
import re
import random
2022-07-25 09:35:54 +00:00
from datetime import datetime, timedelta
2021-11-29 05:11:21 +00:00
from pyrogram import Client, filters
2022-07-25 09:35:54 +00:00
from pyrogram.enums import ChatMemberStatus
2023-07-16 09:49:21 +00:00
from pyrogram.types import (
Message,
ChatPermissions,
CallbackQuery,
InlineKeyboardMarkup,
InlineKeyboardButton,
)
from init import bot
2023-05-31 14:10:42 +00:00
from scheduler import reply_message
2021-11-29 05:11:21 +00:00
2023-05-31 14:27:03 +00:00
def gen_cancel_button(uid: int):
return InlineKeyboardMarkup(
2023-07-16 09:49:21 +00:00
[[InlineKeyboardButton(text="别口球我!", callback_data=f"banme_cancel_{uid}")]]
2023-05-31 14:27:03 +00:00
)
@bot.on_message(
2023-01-12 13:19:54 +00:00
filters.incoming
& filters.group
& filters.command(["banme", f"banme@{bot.me.username}"])
2023-01-12 13:19:54 +00:00
)
2021-11-29 05:11:21 +00:00
async def ban_me_command(client: Client, message: Message):
args = str(message.text).strip()
# 检查是否有倍数参数
2023-01-12 13:19:54 +00:00
if multiple_text := re.search(r"^(\d+)倍$", args):
2021-11-29 05:11:21 +00:00
multiple = int(multiple_text.groups()[0])
else:
multiple = 1
2023-05-31 14:10:42 +00:00
if multiple > 5 or multiple < 1:
multiple = 1
2021-11-29 05:11:21 +00:00
# 检查bot和用户身份
2023-01-12 13:19:54 +00:00
if (
2023-07-16 09:49:21 +00:00
await client.get_chat_member(message.chat.id, "self")
2023-01-12 13:19:54 +00:00
).status != ChatMemberStatus.ADMINISTRATOR:
await message.reply("Bot非群管理员, 无法执行禁言操作QAQ")
2021-11-29 05:11:21 +00:00
return
if not message.from_user:
# 频道
2023-05-31 14:10:42 +00:00
await reply_message(message, "你是个频道, 别来凑热闹OvO")
2021-11-29 05:11:21 +00:00
return
2022-07-25 09:35:54 +00:00
2023-01-12 13:19:54 +00:00
member = (
await client.get_chat_member(message.chat.id, message.from_user.id)
).status
2022-07-25 09:35:54 +00:00
if member in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]:
2023-05-31 14:10:42 +00:00
await reply_message(message, "你也是个管理, 别来凑热闹OvO")
2021-11-29 05:11:21 +00:00
return
# 随机禁言时间
random_time = 2 * int(random.gauss(128 * multiple, 640 * multiple // 10))
2022-07-25 09:35:54 +00:00
act_time = 60 if random_time < 60 else min(random_time, 2591940)
2023-01-12 13:19:54 +00:00
msg = f"既然你那么想被口球的话, 那我就成全你吧!\n送你一份{act_time // 60}{act_time % 60}秒禁言套餐哦, 谢谢惠顾~"
2021-11-29 05:11:21 +00:00
2023-01-12 13:19:54 +00:00
await client.restrict_chat_member(
message.chat.id,
message.from_user.id,
ChatPermissions(),
datetime.now() + timedelta(seconds=act_time),
)
2023-07-16 09:49:21 +00:00
await reply_message(
message, msg, reply_markup=gen_cancel_button(message.from_user.id)
)
2023-05-31 14:27:03 +00:00
2023-07-16 09:49:21 +00:00
@bot.on_callback_query(filters.regex(r"^banme_cancel_(\d+)$"))
2023-05-31 14:27:03 +00:00
async def ban_me_cancel(client: Client, callback_query: CallbackQuery):
if not callback_query.from_user:
return
uid = int(callback_query.data.split("_")[-1])
if callback_query.from_user.id != uid:
await callback_query.answer("这不是属于你的按钮 ~")
return
await callback_query.answer("已撤销,请等待 60s 后发言")
with contextlib.suppress(Exception):
await callback_query.message.delete()
await client.restrict_chat_member(
callback_query.message.chat.id,
callback_query.from_user.id,
ChatPermissions(),
datetime.now() + timedelta(seconds=60),
)