-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbroadcast.py
88 lines (76 loc) · 2.87 KB
/
broadcast.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
from utils.database import db
class BroadcastManager:
@staticmethod
async def broadcast_message_to_sub_members(client, message, button=None):
"""
Sends a message to all users in the broadcast list.
"""
user_ids = await db.get_subscribed_user_ids()
for user_id in user_ids:
try:
await client.send_message(user_id, message, buttons=button)
except Exception as e:
print(f"Failed to send message to user {user_id}: {e}")
# Optionally, retry sending the message or log the failure for later review
@staticmethod
async def broadcast_message_to_temp_members(client, message):
"""
Sends a message to all users in the broadcast list.
"""
user_ids = await db.get_temporary_subscribed_user_ids()
for user_id in user_ids:
try:
await client.send_message(user_id, message)
except Exception as e:
print(f"Failed to send message to user {user_id}: {e}")
# Optionally, retry sending the message or log the failure for later review
@staticmethod
async def add_sub_user(user_id): # check
"""
Adds a user to the broadcast list.
"""
await db.add_subscribed_user(user_id) # Removed 'await'
@staticmethod
async def remove_sub_user(user_id): # check
"""
Removes a user from the broadcast list.
"""
await db.remove_subscribed_user(user_id) # Removed 'await'
@staticmethod
async def get_all_sub_user_ids():
"""
Returns all user IDs in the broadcast list.
"""
return await db.get_subscribed_user_ids()
@staticmethod
async def clear_user_ids():
"""
Clears the broadcast list by removing all user IDs.
"""
await db.clear_subscribed_users()
@staticmethod
async def get_temporary_subscribed_user_ids(): # check
"""
Returns all user IDs in the subscriptions list that are marked as temporarily subscribed.
"""
return await db.get_temporary_subscribed_user_ids()
@staticmethod
async def add_all_users_to_temp():
"""
Adds all users from the database to the broadcast list.
"""
# Mark the current subscribed users as temporarily added for the broadcast
await db.mark_temporary_subscriptions()
@staticmethod
async def remove_all_users_from_temp():
"""
Remove all users from the database to the broadcast list.
"""
# Mark the current subscribed users as temporarily added for the broadcast
await db.mark_temporary_unsubscriptions()
@staticmethod
async def add_user_to_temp(user_id):
"""
add a user from the database to the broadcast list.
"""
await db.add_user_to_temp(user_id)