-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathChatVoiceMod.py
223 lines (202 loc) · 9.32 KB
/
ChatVoiceMod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# .------.------.------.------.------.------.------.------.------.------.
# |D.--. |4.--. |N.--. |1.--. |3.--. |L.--. |3.--. |K.--. |0.--. |0.--. |
# | :/\: | :/\: | :(): | :/\: | :(): | :/\: | :(): | :/\: | :/\: | :/\: |
# | (__) | :\/: | ()() | (__) | ()() | (__) | ()() | :\/: | :\/: | :\/: |
# | '--'D| '--'4| '--'N| '--'1| '--'3| '--'L| '--'3| '--'K| '--'0| '--'0|
# `------`------`------`------`------`------`------`------`------`------'
#
# Copyright 2023 t.me/D4n13l3k00
# Licensed under the Creative Commons CC BY-NC-ND 4.0
#
# Full license text can be found at:
# https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode
#
# Human-friendly one:
# https://creativecommons.org/licenses/by-nc-nd/4.0
import contextlib
import os
import re
from typing import *
import pytgcalls
import youtube_dl
from pytgcalls import PyTgCalls, StreamType
from pytgcalls.types.input_stream import AudioPiped, AudioVideoPiped
from pytgcalls.types.input_stream.quality import HighQualityAudio, HighQualityVideo
from telethon import types
from .. import loader, utils # type: ignore
# meta developer: @D4n13l3k00
# requires: py-tgcalls youtube-dl
@loader.tds
class ChatVoiceMod(loader.Module):
"""Module for working with voicechat"""
strings = {
"name": "ChatVoiceMod",
"downloading": "<b>[ChatVoiceMod]</b> Downloading...",
"playing": "<b>[ChatVoiceMod]</b> Playing...",
"notjoined": "<b>[ChatVoiceMod]</b> You are not joined",
"stop": "<b>[ChatVoiceMod]</b> Playing stopped!",
"leave": "<b>[ChatVoiceMod]</b> Leaved!",
"pause": "<b>[ChatVoiceMod]</b> Paused!",
"resume": "<b>[ChatVoiceMod]</b> Resumed!",
"mute": "<b>[ChatVoiceMod]</b> Muted!",
"unmute": "<b>[ChatVoiceMod]</b> Unmuted!",
"error": "<b>[ChatVoiceMod]</b> Error: <code>{}</code>",
"noargs": "<b>[ChatVoiceMod]</b> No args",
"noreply": "<b>[ChatVoiceMod]</b> No reply",
"nofile": "<b>[ChatVoiceMod]</b> No file",
"nofiles": "<b>[ChatVoiceMod]</b> No files",
"deleted": "<b>[ChatVoiceMod]</b> <code>{}</code> successfully deleted",
"downloaded": "<b>[ChatVoiceMod]</b> Downloaded to <code>dl/{0}</code>. For playing use:\n<code>.cplaya dl/{0}</code>\n<code>.cplayv dl/{0}</code>",
}
async def client_ready(self, client, _):
self.client = client
self.call = PyTgCalls(client)
@self.call.on_stream_end()
async def _(_, update):
with contextlib.suppress(Exception):
await self.call.leave_group_call(update.chat_id)
await self.call.start()
async def parse_args(self, args):
if not args or not re.match(
r"http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?",
args,
):
return args
with youtube_dl.YoutubeDL({"format": "best"}) as ydl:
info = ydl.extract_info(args, download=False)
return info["formats"][0]["url"]
async def cdlcmd(self, m: types.Message):
"<reply_to_media> <name: optional> - Download media to server in `dl` folder"
args = utils.get_args_raw(m)
reply = await m.get_reply_message()
if not reply:
return await utils.answer(m, self.strings("noreply"))
name = args or reply.file.name
try:
m = await utils.answer(m, self.strings("downloading"))
await reply.download_media(f"dl/{name}")
await utils.answer(m, self.strings("downloaded").format(name))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def clscmd(self, m: types.Message):
"List all files in `dl` folder"
if not os.path.isdir("dl") or not os.listdir("dl"):
return await utils.answer(m, self.strings("nofiles"))
files = [f"<code>dl/{f}</code>" for f in os.listdir("dl")]
await utils.answer(m, "\n".join(files))
# command for deleting file from dl folder
async def cdelcmd(self, m: types.Message):
"<name> - Delete file from `dl` folder"
args = utils.get_args_raw(m)
if not args:
return await utils.answer(m, self.strings("noargs"))
if not args.startswith("dl/"):
args = f"dl/{args}"
if not os.path.isfile(f"{args}"):
return await utils.answer(m, self.strings("nofile"))
try:
os.remove(f"{args}")
await utils.answer(m, self.strings("deleted").format(args))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cplayvcmd(self, m: types.Message):
"<link/path/reply_to_video> - Play video in voice chat"
try:
reply = await m.get_reply_message()
path = await self.parse_args(utils.get_args_raw(m))
chat = m.chat.id
if not path:
if not reply:
return await utils.answer(m, self.strings("noargs"))
m = await utils.answer(m, self.strings("downloading"))
path = await reply.download_media()
with contextlib.suppress(pytgcalls.exceptions.GroupCallNotFound):
self.call.get_active_call(chat)
await self.call.leave_group_call(chat)
await self.call.join_group_call(
chat,
AudioVideoPiped(
path,
HighQualityAudio(),
HighQualityVideo(),
),
stream_type=StreamType().pulse_stream,
)
await utils.answer(m, self.strings("playing"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cplayacmd(self, m: types.Message):
"<link/path/reply_to_audio> - Play audio in voice chat"
try:
reply = await m.get_reply_message()
path = await self.parse_args(utils.get_args_raw(m))
chat = m.chat.id
if not path:
if not reply:
return await utils.answer(m, self.strings("noargs"))
m = await utils.answer(m, self.strings("downloading"))
path = await reply.download_media()
with contextlib.suppress(pytgcalls.exceptions.GroupCallNotFound):
self.call.get_active_call(chat)
await self.call.leave_group_call(chat)
await self.call.join_group_call(
chat,
AudioPiped(
path,
HighQualityAudio(),
),
stream_type=StreamType().pulse_stream,
)
await utils.answer(m, self.strings("playing"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cleavecmd(self, m: types.Message):
"Leave"
try:
self.call.get_active_call(m.chat.id)
await self.call.leave_group_call(m.chat.id)
await utils.answer(m, self.strings("leave"))
except pytgcalls.exceptions.GroupCallNotFound:
await utils.answer(m, self.strings("notjoined"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cmutecmd(self, m: types.Message):
"Mute"
try:
self.call.get_active_call(m.chat.id)
await self.call.mute_stream(m.chat.id)
await utils.answer(m, self.strings("mute"))
except pytgcalls.exceptions.GroupCallNotFound:
await utils.answer(m, self.strings("notjoined"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cunmutecmd(self, m: types.Message):
"Unmute"
try:
self.call.get_active_call(m.chat.id)
await self.call.unmute_stream(m.chat.id)
await utils.answer(m, self.strings("unmute"))
except pytgcalls.exceptions.GroupCallNotFound:
await utils.answer(m, self.strings("notjoined"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cpausecmd(self, m: types.Message):
"Pause"
try:
self.call.get_active_call(m.chat.id)
await self.call.pause_stream(m.chat.id)
await utils.answer(m, self.strings("pause"))
except pytgcalls.exceptions.GroupCallNotFound:
await utils.answer(m, self.strings("notjoined"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))
async def cresumecmd(self, m: types.Message):
"Resume"
try:
self.call.get_active_call(m.chat.id)
await self.call.resume_stream(m.chat.id)
await utils.answer(m, self.strings("resume"))
except pytgcalls.exceptions.GroupCallNotFound:
await utils.answer(m, self.strings("notjoined"))
except Exception as e:
await utils.answer(m, self.strings("error").format(str(e)))