video-stream/program/assistant_settings.py

178 lines
5.9 KiB
Python
Raw Normal View History

2022-02-22 23:41:07 +00:00
"""
Video + Music Stream Telegram Bot
Copyright (c) 2022-present levina=lab <https://github.com/levina-lab>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/licenses.html>
"""
2022-01-31 12:41:47 +00:00
import asyncio
2022-02-11 15:40:15 +00:00
2022-01-31 12:41:47 +00:00
from config import BOT_USERNAME, SUDO_USERS
2022-02-12 04:40:23 +00:00
2022-02-21 14:00:37 +00:00
from program.utils.function import get_calls
2022-02-24 04:56:39 +00:00
from driver.queues import QUEUE
2022-02-15 04:44:58 +00:00
from driver.core import user, me_bot
2022-01-31 12:41:47 +00:00
from driver.filters import command, other_filters
from driver.database.dbchat import remove_served_chat
2022-02-10 22:39:38 +00:00
from driver.database.dbqueue import remove_active_chat
from driver.decorators import authorized_users_only, bot_creator, check_blacklist
2022-01-31 12:41:47 +00:00
2022-02-12 04:40:23 +00:00
from pyrogram.types import Message
from pyrogram import Client, filters
2022-02-21 04:03:13 +00:00
from pyrogram.raw.types import InputPeerChannel
2022-02-21 14:00:37 +00:00
from pyrogram.raw.functions.phone import CreateGroupCall, DiscardGroupCall
2022-02-21 05:18:37 +00:00
from pyrogram.errors import UserAlreadyParticipant, UserNotParticipant, ChatAdminRequired
2022-02-12 04:40:23 +00:00
2022-01-31 12:41:47 +00:00
@Client.on_message(
command(["userbotjoin", f"userbotjoin@{BOT_USERNAME}"]) & other_filters
)
@check_blacklist()
2022-01-31 12:41:47 +00:00
@authorized_users_only
async def join_chat(c: Client, m: Message):
chat_id = m.chat.id
try:
2022-02-12 04:05:44 +00:00
invitelink = (await c.get_chat(chat_id)).invite_link
if not invitelink:
await c.export_chat_invite_link(chat_id)
invitelink = (await c.get_chat(chat_id)).invite_link
2022-01-31 12:41:47 +00:00
if invitelink.startswith("https://t.me/+"):
invitelink = invitelink.replace(
"https://t.me/+", "https://t.me/joinchat/"
)
2022-02-12 04:05:44 +00:00
await user.join_chat(invitelink)
await remove_active_chat(chat_id)
2022-03-02 13:01:45 +00:00
return await user.send_message(chat_id, "✅ userbot 加入了对话")
2022-01-31 12:41:47 +00:00
except UserAlreadyParticipant:
2022-03-02 13:01:45 +00:00
return await user.send_message(chat_id, "✅ userbot 已经加入了对话")
2022-01-31 12:41:47 +00:00
@Client.on_message(
command(["userbotleave", f"userbotleave@{BOT_USERNAME}"]) & other_filters
)
@check_blacklist()
2022-01-31 12:41:47 +00:00
@authorized_users_only
async def leave_chat(_, m: Message):
chat_id = m.chat.id
try:
await user.leave_chat(chat_id)
2022-02-10 22:39:38 +00:00
await remove_active_chat(chat_id)
2022-01-31 12:41:47 +00:00
return await _.send_message(
chat_id,
2022-03-02 13:01:45 +00:00
"✅ userbot 离开了对话",
2022-01-31 12:41:47 +00:00
)
except UserNotParticipant:
return await _.send_message(
chat_id,
2022-03-02 13:01:45 +00:00
"❌ userbot 未在对话中",
2022-01-31 12:41:47 +00:00
)
2022-02-21 04:01:48 +00:00
@Client.on_message(command(["leaveall", f"leaveall@{BOT_USERNAME}"]) & ~filters.edited)
2022-02-10 04:16:41 +00:00
@bot_creator
2022-01-31 12:41:47 +00:00
async def leave_all(client, message):
if message.from_user.id not in SUDO_USERS:
return
left = 0
failed = 0
2022-03-02 13:01:45 +00:00
msg = await message.reply("🔄 Userbot 开始退出所有群组")
2022-01-31 12:41:47 +00:00
async for dialog in user.iter_dialogs():
try:
await user.leave_chat(dialog.chat.id)
2022-02-10 22:39:38 +00:00
await remove_active_chat(dialog.chat.id)
2022-01-31 12:41:47 +00:00
left += 1
await msg.edit(
2022-03-02 13:01:45 +00:00
f"Userbot 退出所有群组中...\n\n已经退出了 {left} 个群组\n退出失败 {failed} 个群组"
2022-01-31 12:41:47 +00:00
)
except BaseException:
failed += 1
await msg.edit(
2022-03-02 13:01:45 +00:00
f"Userbot 退出所有群组中...\n\n已经退出了 {left} 个群组\n退出失败 {failed} 个群组"
2022-01-31 12:41:47 +00:00
)
await asyncio.sleep(0.7)
await msg.delete()
await client.send_message(
2022-03-02 13:01:45 +00:00
message.chat.id, f"✅ 退出了 {left} 个群组\n退出失败 {failed} 个群组"
2022-01-31 12:41:47 +00:00
)
2022-02-21 04:01:48 +00:00
@Client.on_message(command(["startvc", f"startvc@{BOT_USERNAME}"]) & other_filters)
@check_blacklist()
@authorized_users_only
async def start_group_call(c: Client, m: Message):
chat_id = m.chat.id
2022-03-02 13:01:45 +00:00
msg = await c.send_message(chat_id, "`尝试开启视频聊天中...`")
2022-02-21 04:01:48 +00:00
try:
2022-03-02 14:55:29 +00:00
group_call = (
await get_calls(c, m, err_msg="")
)
if group_call:
raise FileExistsError
2022-02-21 04:01:48 +00:00
peer = await user.resolve_peer(chat_id)
await user.send(
CreateGroupCall(
peer=InputPeerChannel(
channel_id=peer.channel_id,
access_hash=peer.access_hash,
),
random_id=user.rnd_id() // 9000000000,
)
)
2022-03-02 13:01:45 +00:00
await msg.edit_text("✅ 视频聊天开启成功")
2022-02-21 05:18:37 +00:00
except ChatAdminRequired:
await msg.edit_text(
2022-03-02 13:01:45 +00:00
"❌ 您需要首先赋予 userbot `管理语音聊天` 权限"
2022-02-21 04:01:48 +00:00
)
2022-03-02 14:55:29 +00:00
except FileExistsError:
await msg.edit_text("✅ 视频聊天未关闭")
2022-02-21 04:01:48 +00:00
2022-02-21 14:00:37 +00:00
@Client.on_message(command(["stopvc", f"stopvc@{BOT_USERNAME}"]) & other_filters)
@check_blacklist()
@authorized_users_only
async def stop_group_call(c: Client, m: Message):
chat_id = m.chat.id
2022-03-02 13:01:45 +00:00
msg = await c.send_message(chat_id, "`尝试关闭视频聊天中...`")
2022-03-02 14:55:29 +00:00
group_call = (
2022-03-02 13:01:45 +00:00
await get_calls(c, m, err_msg="group call not active")
2022-02-21 14:00:37 +00:00
)
2022-03-02 14:55:29 +00:00
if not group_call:
return await msg.edit_text("❌ 未开启视频聊天")
2022-02-21 14:00:37 +00:00
await user.send(
DiscardGroupCall(
call=group_call
)
)
2022-03-02 13:01:45 +00:00
await msg.edit_text("✅ 视频聊天结束成功")
2022-02-21 14:00:37 +00:00
2022-01-31 12:41:47 +00:00
@Client.on_message(filters.left_chat_member)
async def bot_kicked(c: Client, m: Message):
2022-02-15 04:44:58 +00:00
bot_id = me_bot.id
2022-01-31 12:41:47 +00:00
chat_id = m.chat.id
left_member = m.left_chat_member
if left_member.id == bot_id:
2022-02-24 04:56:39 +00:00
if chat_id in QUEUE:
await remove_active_chat(chat_id)
return
try:
await user.leave_chat(chat_id)
await remove_served_chat(chat_id)
except BaseException as err:
print(err)