-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallsmusic.py
More file actions
120 lines (83 loc) · 3.34 KB
/
Copy pathcallsmusic.py
File metadata and controls
120 lines (83 loc) · 3.34 KB
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
# Calls Music 1 - Telegram bot for streaming audio in group calls
# Copyright (C) 2021 Roj Serbest
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Dict
from pytgcalls import GroupCallFactory
from AlvinMusicRobot.services.callsmusic import client
from AlvinMusicRobot.services.queues import queues
instances: Dict[int, GroupCallFactory] = {}
active_chats: Dict[int, Dict[str, bool]] = {}
def init_instance(chat_id: int):
if chat_id not in instances:
instances[chat_id] = GroupCallFactory(client,outgoing_audio_bitrate_kbit=320).get_file_group_call()
instance = instances[chat_id]
@instance.on_playout_ended
async def ___(__, _):
queues.task_done(chat_id)
if queues.is_empty(chat_id):
await stop(chat_id)
else:
instance.input_filename = queues.get(chat_id)["file_path"]
def remove(chat_id: int):
if chat_id in instances:
del instances[chat_id]
if not queues.is_empty(chat_id):
queues.clear(chat_id)
if chat_id in active_chats:
del active_chats[chat_id]
def get_instance(chat_id: int) -> GroupCallFactory:
init_instance(chat_id)
return instances[chat_id]
async def start(chat_id: int):
await get_instance(chat_id).start(chat_id)
active_chats[chat_id] = {"playing": True, "muted": False}
async def stop(chat_id: int):
await get_instance(chat_id).stop()
if chat_id in active_chats:
del active_chats[chat_id]
async def set_stream(chat_id: int, file: str):
if chat_id not in active_chats:
await start(chat_id)
get_instance(chat_id).input_filename = file
def pause(chat_id: int) -> bool:
if chat_id not in active_chats:
return False
elif not active_chats[chat_id]["playing"]:
return False
get_instance(chat_id).pause_playout()
active_chats[chat_id]["playing"] = False
return True
def resume(chat_id: int) -> bool:
if chat_id not in active_chats:
return False
elif active_chats[chat_id]["playing"]:
return False
get_instance(chat_id).resume_playout()
active_chats[chat_id]["playing"] = True
return True
async def mute(chat_id: int) -> int:
if chat_id not in active_chats:
return 2
elif active_chats[chat_id]["muted"]:
return 1
await get_instance(chat_id).set_is_mute(True)
active_chats[chat_id]["muted"] = True
return 0
async def unmute(chat_id: int) -> int:
if chat_id not in active_chats:
return 2
elif not active_chats[chat_id]["muted"]:
return 1
await get_instance(chat_id).set_is_mute(False)
active_chats[chat_id]["muted"] = False
return 0