|
| 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