Skip to content

Commit b4dc7c2

Browse files
committed
feat: DashBoard
1 parent 3a130a5 commit b4dc7c2

File tree

4 files changed

+280
-90
lines changed

4 files changed

+280
-90
lines changed

App/Controller.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from telebot import util, types
99
from telebot.async_telebot import AsyncTeleBot
1010
from telebot.asyncio_storage import StateMemoryStorage
11-
from App import Event
11+
from App import Event, DashBoard
1212
from App.JoinRequest import JoinRequest
1313
from utils.Tool import cal_md5
1414

@@ -39,10 +39,10 @@ def run(self):
3939
async def handle_command(message: types.Message):
4040
await Event.start(bot, message)
4141

42-
@bot.message_handler(commands=["pin_vote_msg"])
43-
async def handle_command_pin_msg(message: types.Message):
42+
@bot.message_handler(commands=["setting"])
43+
async def handle_command_setting(message: types.Message):
4444
if message.chat.type in ["group", "supergroup"]:
45-
await Event.set_pin_message(bot, message, self.db, self.bot_id)
45+
await DashBoard.homepage(bot, message, self.db)
4646
else:
4747
await bot.reply_to(message, "Please use this command in the group.")
4848

@@ -53,26 +53,25 @@ async def handle_command_set_vote_time(message: types.Message):
5353
else:
5454
await bot.reply_to(message, "Please use this command in the group.")
5555

56-
@bot.message_handler(commands=["clean_pin_msg"])
57-
async def handle_command_clean_pin_msg(message: types.Message):
58-
if message.chat.type in ["group", "supergroup"]:
59-
await Event.set_clean_pin_msg(bot, message, self.db, self.bot_id)
60-
else:
61-
await bot.reply_to(message, "Please use this command in the group.")
62-
6356
@bot.message_handler(content_types=['pinned_message'])
6457
async def delete_pinned_message(message: types.Message):
6558
status = self.db.get(str(message.chat.id))
6659
if not status:
6760
return
68-
if status.get("clean_service_msg", False):
61+
if status.get("clean_pinned_message", False):
6962
try:
7063
await bot.delete_message(message.chat.id, message.message_id)
7164
except Exception as e:
7265
logger.error(f"Delete pinned message failed: {e}")
7366

7467
@bot.chat_join_request_handler()
7568
async def handle_new_chat_members(request: types.ChatJoinRequest):
69+
chat_dict = self.db.get(str(request.chat.id))
70+
if chat_dict is None:
71+
chat_dict = {}
72+
vote_to_join = chat_dict.get("vote_to_join", True)
73+
if not vote_to_join:
74+
return
7675
join_request_id = cal_md5(f"{request.chat.id}@{request.from_user.id}")
7776
if join_request_id in self.join_tasks:
7877
join_task = self.join_tasks[join_request_id]
@@ -88,17 +87,22 @@ async def handle_new_chat_members(request: types.ChatJoinRequest):
8887

8988
@bot.callback_query_handler(lambda c: True)
9089
async def handle_callback_query(callback_query: types.CallbackQuery):
91-
action = callback_query.data.split()[0]
92-
join_request_id = callback_query.data.split()[1]
93-
join_tasks = self.join_tasks.get(join_request_id, None)
94-
if join_tasks is None:
95-
return
96-
await join_tasks.handle_button(bot, callback_query, action)
97-
if join_tasks.check_up_status():
98-
try:
99-
del self.join_tasks[join_request_id]
100-
except KeyError:
101-
pass
90+
requests_type = callback_query.data.split()[0]
91+
if requests_type == "JR":
92+
action = callback_query.data.split()[0]
93+
join_request_id = callback_query.data.split()[1]
94+
if join_request_id in self.join_tasks:
95+
join_task = self.join_tasks[join_request_id]
96+
else:
97+
return
98+
await join_task.handle_button(bot, callback_query, action)
99+
if join_task.check_up_status():
100+
try:
101+
del self.join_tasks[join_request_id]
102+
except KeyError:
103+
pass
104+
elif requests_type == "Setting":
105+
await DashBoard.command_handler(bot, callback_query, self.db, self.bot_id)
102106

103107
async def main():
104108
await asyncio.gather(bot.polling(non_stop=True, allowed_updates=util.update_types))

App/DashBoard.py

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time: 2024/2/8 9:53
3+
# @FileName: DashBoard.py
4+
# @Software: PyCharm
5+
# @GitHub: KimmyXYC
6+
from telebot import types
7+
from loguru import logger
8+
9+
FORMAT = {
10+
True: "✅",
11+
False: "❌"
12+
}
13+
ADDITION = "If you want to change the settings, please click the button below."
14+
15+
16+
def db_analyzer(db, chat_id):
17+
chat_dict = db.get(str(chat_id))
18+
if chat_dict is None:
19+
chat_dict = {}
20+
vote_to_join = chat_dict.get("vote_to_join", True)
21+
pin_msg = chat_dict.get("pin_msg", False)
22+
vote_time = chat_dict.get("vote_time", 600)
23+
clean_pinned_message = chat_dict.get("clean_pinned_message", False)
24+
return vote_to_join, pin_msg, vote_time, clean_pinned_message, chat_dict
25+
26+
27+
def message_creator(chat_id, db, addition=ADDITION):
28+
vote_to_join, pin_msg, vote_time, clean_pinned_message, _ = db_analyzer(db, chat_id)
29+
# Time format
30+
minutes = vote_time // 60
31+
seconds = vote_time % 60
32+
if minutes == 0:
33+
_time = f"{seconds} seconds"
34+
elif seconds == 0:
35+
_time = f"{minutes} minutes"
36+
else:
37+
_time = f"{minutes} minutes and {seconds} seconds"
38+
39+
reply_message = f"""
40+
<b>Group Setting</b>
41+
42+
<b>Vote To Join</b>: {vote_to_join}
43+
<b>Vote Time</b>: {_time}
44+
<b>Pin Vote Message</b>: {pin_msg}
45+
<b>Clean Pinned Message</b>: {clean_pinned_message}
46+
"""
47+
if addition:
48+
reply_message += "\n"
49+
reply_message += addition
50+
51+
buttons = button_creator(vote_to_join, pin_msg, clean_pinned_message, chat_id)
52+
53+
return reply_message, buttons
54+
55+
56+
def button_creator(vote_to_join, pin_msg, clean_pinned_message, chat_id):
57+
buttons = types.InlineKeyboardMarkup()
58+
buttons.add(types.InlineKeyboardButton(f"{FORMAT.get(vote_to_join)} Vote To Join",
59+
callback_data=f"Setting vote_to_join {chat_id}"))
60+
buttons.add(types.InlineKeyboardButton(f"Set Vote Time",
61+
callback_data=f"Setting vote_time {chat_id}"))
62+
buttons.add(types.InlineKeyboardButton(f"{FORMAT.get(pin_msg)} Pin Vote Message",
63+
callback_data=f"Setting pin_msg {chat_id}"),
64+
types.InlineKeyboardButton(f"{FORMAT.get(clean_pinned_message)} Clean Pinned Message",
65+
callback_data=f"Setting clean_pinned_message {chat_id}"))
66+
buttons.add(types.InlineKeyboardButton("Close", callback_data="Setting close"))
67+
return buttons
68+
69+
70+
async def homepage(bot, message: types.Message, db):
71+
chat_member = await bot.get_chat_member(message.chat.id, message.from_user.id)
72+
if chat_member.status == 'administrator' or chat_member.status == 'creator':
73+
reply_message, buttons = message_creator(message.chat.id, db)
74+
await bot.reply_to(
75+
message,
76+
reply_message,
77+
parse_mode="HTML",
78+
reply_markup=buttons,
79+
disable_web_page_preview=True
80+
)
81+
82+
83+
async def homepage_back(bot, callback_query, db, chat_member):
84+
if chat_member.status == 'administrator' or chat_member.status == 'creator':
85+
reply_message, buttons = message_creator(callback_query.message.chat.id, db)
86+
await bot.edit_message_text(
87+
reply_message,
88+
callback_query.message.chat.id,
89+
callback_query.message.message_id,
90+
parse_mode="HTML",
91+
reply_markup=buttons,
92+
disable_web_page_preview=True
93+
)
94+
else:
95+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
96+
return
97+
98+
99+
async def command_handler(bot, callback_query: types.CallbackQuery, db, bot_id):
100+
requests_type = callback_query.data.split()[1]
101+
chat_member = await bot.get_chat_member(callback_query.message.chat.id, callback_query.from_user.id)
102+
bot_member = await bot.get_chat_member(callback_query.message.chat.id, bot_id)
103+
if chat_member.status != 'administrator' and chat_member.status != 'creator':
104+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
105+
return
106+
if requests_type == "vote_to_join":
107+
await vote_to_join_handler(bot, callback_query, db, chat_member)
108+
elif requests_type == "vote_time":
109+
await vote_time_handler(bot, callback_query, db, chat_member)
110+
elif requests_type == "pin_msg":
111+
await pin_msg_handler(bot, callback_query, db, chat_member, bot_member)
112+
elif requests_type == "clean_pinned_message":
113+
await clean_pinned_message_handler(bot, callback_query, db, chat_member, bot_member)
114+
elif requests_type == "close":
115+
chat_member = await bot.get_chat_member(callback_query.message.chat.id, callback_query.from_user.id)
116+
if chat_member.status == 'creator' or chat_member.status == 'administrator':
117+
await bot.delete_message(callback_query.message.chat.id, callback_query.message.message_id)
118+
elif requests_type == "edit_vote_time":
119+
await edit_vote_time_handler(bot, callback_query, db, chat_member)
120+
elif requests_type == "back":
121+
await homepage_back(bot, callback_query, db, chat_member)
122+
else:
123+
await bot.answer_callback_query(callback_query.id, "Unknown request.")
124+
logger.error(f"Unknown request: {callback_query.data}")
125+
126+
127+
async def vote_to_join_handler(bot, callback_query: types.CallbackQuery, db, chat_member):
128+
if chat_member.status != 'creator' and (chat_member.status != 'administrator' or not chat_member.can_invite_users):
129+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
130+
return
131+
chat_id = int(callback_query.data.split()[2])
132+
vote_to_join, _, _, _, chat_dict = db_analyzer(db, chat_id)
133+
if vote_to_join:
134+
chat_dict["vote_to_join"] = False
135+
db.set(str(chat_id), chat_dict)
136+
else:
137+
chat_dict["vote_to_join"] = True
138+
db.set(str(chat_id), chat_dict)
139+
reply_message, buttons = message_creator(chat_id, db)
140+
await bot.edit_message_text(
141+
reply_message,
142+
callback_query.message.chat.id,
143+
callback_query.message.message_id,
144+
parse_mode="HTML",
145+
reply_markup=buttons,
146+
disable_web_page_preview=True
147+
)
148+
149+
150+
async def vote_time_handler(bot, callback_query: types.CallbackQuery, db, chat_member):
151+
if chat_member.status != 'creator' and (chat_member.status != 'administrator' or not chat_member.can_change_info):
152+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
153+
return
154+
chat_id = int(callback_query.data.split()[2])
155+
addition = "If you want to change the vote time precisely, please use the command /set_vote_time"
156+
reply_message, _ = message_creator(chat_id, db, addition)
157+
buttons = types.InlineKeyboardMarkup()
158+
buttons.add(types.InlineKeyboardButton(f"1 min", callback_data=f"Setting edit_vote_time {chat_id} 60"),
159+
types.InlineKeyboardButton(f"2 min", callback_data=f"Setting edit_vote_time {chat_id} 120"),
160+
types.InlineKeyboardButton(f"3 min", callback_data=f"Setting edit_vote_time {chat_id} 180"))
161+
buttons.add(types.InlineKeyboardButton(f"5min", callback_data=f"Setting edit_vote_time {chat_id} 300"),
162+
types.InlineKeyboardButton(f"10min", callback_data=f"Setting edit_vote_time {chat_id} 600"),
163+
types.InlineKeyboardButton(f"15min", callback_data=f"Setting edit_vote_time {chat_id} 900"))
164+
buttons.add(types.InlineKeyboardButton(f"20min", callback_data=f"Setting edit_vote_time {chat_id} 1200"),
165+
types.InlineKeyboardButton(f"30min", callback_data=f"Setting edit_vote_time {chat_id} 1800"),
166+
types.InlineKeyboardButton(f"60min", callback_data=f"Setting edit_vote_time {chat_id} 3600"))
167+
buttons.add(types.InlineKeyboardButton("⬅️ Go Back", callback_data="Setting back"))
168+
await bot.edit_message_text(
169+
reply_message,
170+
callback_query.message.chat.id,
171+
callback_query.message.message_id,
172+
parse_mode="HTML",
173+
reply_markup=buttons,
174+
disable_web_page_preview=True
175+
)
176+
177+
178+
async def pin_msg_handler(bot, callback_query: types.CallbackQuery, db, chat_member, bot_member):
179+
if bot_member.status != 'administrator' or not bot_member.can_pin_messages:
180+
await bot.answer_callback_query(callback_query.id, "I don't have permission to pin messages.")
181+
return
182+
if chat_member.status != 'creator' and (chat_member.status != 'administrator' or not chat_member.can_pin_messages):
183+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
184+
return
185+
chat_id = int(callback_query.data.split()[2])
186+
_, pin_msg, _, _, chat_dict = db_analyzer(db, chat_id)
187+
if pin_msg:
188+
chat_dict["pin_msg"] = False
189+
db.set(str(chat_id), chat_dict)
190+
else:
191+
chat_dict["pin_msg"] = True
192+
db.set(str(chat_id), chat_dict)
193+
reply_message, buttons = message_creator(chat_id, db)
194+
await bot.edit_message_text(
195+
reply_message,
196+
callback_query.message.chat.id,
197+
callback_query.message.message_id,
198+
parse_mode="HTML",
199+
reply_markup=buttons,
200+
disable_web_page_preview=True
201+
)
202+
203+
204+
async def clean_pinned_message_handler(bot, callback_query: types.CallbackQuery, db, chat_member, bot_member):
205+
if bot_member.status != 'administrator' or not bot_member.can_delete_messages:
206+
await bot.answer_callback_query(callback_query.id, "I don't have permission to delete messages.")
207+
return
208+
if chat_member.status != 'creator' and (
209+
chat_member.status != 'administrator' or not chat_member.can_delete_messages):
210+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
211+
return
212+
chat_id = int(callback_query.data.split()[2])
213+
_, _, _, clean_pinned_message, chat_dict = db_analyzer(db, chat_id)
214+
if clean_pinned_message:
215+
chat_dict["clean_pinned_message"] = False
216+
db.set(str(chat_id), chat_dict)
217+
else:
218+
chat_dict["clean_pinned_message"] = True
219+
db.set(str(chat_id), chat_dict)
220+
reply_message, buttons = message_creator(chat_id, db)
221+
await bot.edit_message_text(
222+
reply_message,
223+
callback_query.message.chat.id,
224+
callback_query.message.message_id,
225+
parse_mode="HTML",
226+
reply_markup=buttons,
227+
disable_web_page_preview=True
228+
)
229+
230+
231+
async def edit_vote_time_handler(bot, callback_query: types.CallbackQuery, db, chat_member):
232+
if chat_member.status != 'creator' and (chat_member.status != 'administrator' or not chat_member.can_change_info):
233+
await bot.answer_callback_query(callback_query.id, "You don't have permission to do this.")
234+
return
235+
chat_id = int(callback_query.data.split()[2])
236+
vote_time = int(callback_query.data.split()[3])
237+
chat_dict = db.get(str(chat_id))
238+
if chat_dict is None:
239+
chat_dict = {}
240+
chat_dict["vote_time"] = vote_time
241+
db.set(str(chat_id), chat_dict)
242+
reply_message, buttons = message_creator(chat_id, db)
243+
await bot.edit_message_text(
244+
reply_message,
245+
callback_query.message.chat.id,
246+
callback_query.message.message_id,
247+
parse_mode="HTML",
248+
reply_markup=buttons,
249+
disable_web_page_preview=True
250+
)

0 commit comments

Comments
 (0)