""" MIT License Copyright (c) 2024 TheHamkerCat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import re from pyrogram import filters from wbb import app from wbb.core.decorators.errors import capture_err from wbb.core.decorators.permissions import adminsOnly from wbb.core.sections import section from wbb.services.member_identity import user_display_name from wbb.utils.dbfunctions import ( alpha_to_int, get_karma, get_karmas, int_to_alpha, is_karma_on, karma_off, karma_on, update_karma, ) from wbb.utils.dbpoints import award_upvote from wbb.utils.filter_groups import karma_negative_group, karma_positive_group __MODULE__ = "声望" __HELP__ = """回复消息发送“+”“+1”“谢谢”或 👍 可点赞。 回复消息发送“-”“-1”或 👎 可点踩。 /karma_toggle [开启|关闭] - 开启或关闭本群声望系统。 回复成员消息发送 /karma 可查看该成员声望。 直接发送 /karma、“声望”或“声望排行”可查看本群声望前 10 名。""" regex_upvote = r"^(\++|\+1|thx|tnx|tq|ty|thankyou|thank you|thanx|thanks|pro|cool|good|agree|👍|\++ .+)$" regex_downvote = r"^(-+|-1|not cool|disagree|worst|bad|👎|-+ .+)$" @app.on_message( filters.text & filters.group & filters.incoming & filters.reply & filters.regex(regex_upvote, re.IGNORECASE) & ~filters.via_bot & ~filters.bot, group=karma_positive_group, ) @capture_err async def upvote(_, message): if not message.reply_to_message.from_user: return if not message.from_user: return if message.reply_to_message.from_user.id == message.from_user.id: return chat_id = message.chat.id user_id = message.reply_to_message.from_user.id user_mention = message.reply_to_message.from_user.mention point_account, point_created = await award_upvote( chat_id=chat_id, voter_id=message.from_user.id, target_id=user_id, message_id=message.id, username=message.reply_to_message.from_user.username, first_name=message.reply_to_message.from_user.first_name, display_name=user_display_name(message.reply_to_message.from_user), ) if not await is_karma_on(message.chat.id): if point_created and point_account: await message.reply_text( f"已奖励 {user_mention} 积分,当前余额:" f"{point_account['balance']}。" ) return current_karma = await get_karma(chat_id, await int_to_alpha(user_id)) if current_karma: current_karma = current_karma["karma"] karma = current_karma + 1 new_karma = {"karma": karma} await update_karma(chat_id, await int_to_alpha(user_id), new_karma) else: karma = 1 new_karma = {"karma": karma} await update_karma(chat_id, await int_to_alpha(user_id), new_karma) await message.reply_text( f"{user_mention} 的声望 +1\n当前声望:{karma}" ) @app.on_message( filters.text & filters.group & filters.incoming & filters.reply & filters.regex(regex_downvote, re.IGNORECASE) & ~filters.via_bot & ~filters.bot, group=karma_negative_group, ) @capture_err async def downvote(_, message): if not await is_karma_on(message.chat.id): return if not message.reply_to_message.from_user: return if not message.from_user: return if message.reply_to_message.from_user.id == message.from_user.id: return chat_id = message.chat.id user_id = message.reply_to_message.from_user.id user_mention = message.reply_to_message.from_user.mention current_karma = await get_karma(chat_id, await int_to_alpha(user_id)) if current_karma: current_karma = current_karma["karma"] karma = current_karma - 1 new_karma = {"karma": karma} await update_karma(chat_id, await int_to_alpha(user_id), new_karma) else: karma = -1 new_karma = {"karma": karma} await update_karma(chat_id, await int_to_alpha(user_id), new_karma) await message.reply_text( f"{user_mention} 的声望 -1\n当前声望:{karma}" ) @app.on_message( filters.group & ( filters.command("karma") | filters.regex(r"^(?:声望|声望排行)$") ) ) @capture_err async def command_karma(_, message): chat_id = message.chat.id if not message.reply_to_message: m = await message.reply_text("正在统计声望...") try: karma = await get_karmas(chat_id) if not karma: return await m.edit("本群尚无声望记录。") karma_dicc = {} for i in karma: try: user_id = await alpha_to_int(i) user_karma = karma[i]["karma"] karma_dicc[str(user_id)] = user_karma except Exception: continue if not karma_dicc: return await m.edit("本群尚无声望记录。") karma_sorted = sorted( karma_dicc.items(), key=lambda item: item[1], reverse=True ) try: user_ids_needed = [int(uid) for uid, _ in karma_sorted[:50]] fetched = await app.get_users(user_ids_needed) users = fetched if isinstance(fetched, list) else [fetched] userdb = {int(user.id): user_display_name(user) for user in users} except Exception: return await m.edit("获取成员资料失败,请稍后重试。") karma_display = {} limit = 0 for user_id_str, karma_count in karma_sorted: if limit >= 15: break user_id_int = int(user_id_str) if user_id_int not in userdb: continue label = userdb[user_id_int] karma_display[f"{label} · {user_id_int}"] = [f"**{karma_count}**"] limit += 1 if not karma_display: return await m.edit("没有可显示的声望成员。") msg = f"{message.chat.title} 的声望排行" await m.edit(section(msg, karma_display)) except Exception: await m.edit("声望统计失败,请稍后重试。") return else: if not message.reply_to_message.from_user: return await message.reply("匿名身份没有声望记录。") user_id = message.reply_to_message.from_user.id try: karma = await get_karma(chat_id, await int_to_alpha(user_id)) karma_value = karma["karma"] if karma else 0 await message.reply_text(f"**当前声望:** __{karma_value}__") except Exception: await message.reply_text("获取声望失败,请稍后重试。") @app.on_message(filters.command("karma_toggle") & ~filters.private) @adminsOnly("can_change_info") async def captcha_state(_, message): usage = "**用法:**\n/karma_toggle [开启|关闭]" if len(message.command) != 2: return await message.reply_text(usage) chat_id = message.chat.id state = message.text.split(None, 1)[1].strip() state = state.lower() state = { "开启": "enable", "打开": "enable", "关闭": "disable", }.get(state, state) if state == "enable": await karma_on(chat_id) await message.reply_text("本群声望系统已开启。") elif state == "disable": await karma_off(chat_id) await message.reply_text("本群声望系统已关闭。") else: await message.reply_text(usage)