karma.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import re
  21. from pyrogram import filters
  22. from wbb import app
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.core.decorators.permissions import adminsOnly
  25. from wbb.core.sections import section
  26. from wbb.services.member_identity import user_display_name
  27. from wbb.utils.dbfunctions import (
  28. alpha_to_int,
  29. get_karma,
  30. get_karmas,
  31. int_to_alpha,
  32. is_karma_on,
  33. karma_off,
  34. karma_on,
  35. update_karma,
  36. )
  37. from wbb.utils.dbpoints import award_upvote
  38. from wbb.utils.filter_groups import karma_negative_group, karma_positive_group
  39. __MODULE__ = "声望"
  40. __HELP__ = """回复消息发送“+”“+1”“谢谢”或 👍 可点赞。
  41. 回复消息发送“-”“-1”或 👎 可点踩。
  42. /karma_toggle [开启|关闭] - 开启或关闭本群声望系统。
  43. 回复成员消息发送 /karma 可查看该成员声望。
  44. 直接发送 /karma、“声望”或“声望排行”可查看本群声望前 10 名。"""
  45. regex_upvote = r"^(\++|\+1|thx|tnx|tq|ty|thankyou|thank you|thanx|thanks|pro|cool|good|agree|👍|\++ .+)$"
  46. regex_downvote = r"^(-+|-1|not cool|disagree|worst|bad|👎|-+ .+)$"
  47. @app.on_message(
  48. filters.text
  49. & filters.group
  50. & filters.incoming
  51. & filters.reply
  52. & filters.regex(regex_upvote, re.IGNORECASE)
  53. & ~filters.via_bot
  54. & ~filters.bot,
  55. group=karma_positive_group,
  56. )
  57. @capture_err
  58. async def upvote(_, message):
  59. if not message.reply_to_message.from_user:
  60. return
  61. if not message.from_user:
  62. return
  63. if message.reply_to_message.from_user.id == message.from_user.id:
  64. return
  65. chat_id = message.chat.id
  66. user_id = message.reply_to_message.from_user.id
  67. user_mention = message.reply_to_message.from_user.mention
  68. point_account, point_created = await award_upvote(
  69. chat_id=chat_id,
  70. voter_id=message.from_user.id,
  71. target_id=user_id,
  72. message_id=message.id,
  73. username=message.reply_to_message.from_user.username,
  74. first_name=message.reply_to_message.from_user.first_name,
  75. display_name=user_display_name(message.reply_to_message.from_user),
  76. )
  77. if not await is_karma_on(message.chat.id):
  78. if point_created and point_account:
  79. await message.reply_text(
  80. f"已奖励 {user_mention} 积分,当前余额:"
  81. f"{point_account['balance']}。"
  82. )
  83. return
  84. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  85. if current_karma:
  86. current_karma = current_karma["karma"]
  87. karma = current_karma + 1
  88. new_karma = {"karma": karma}
  89. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  90. else:
  91. karma = 1
  92. new_karma = {"karma": karma}
  93. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  94. await message.reply_text(
  95. f"{user_mention} 的声望 +1\n当前声望:{karma}"
  96. )
  97. @app.on_message(
  98. filters.text
  99. & filters.group
  100. & filters.incoming
  101. & filters.reply
  102. & filters.regex(regex_downvote, re.IGNORECASE)
  103. & ~filters.via_bot
  104. & ~filters.bot,
  105. group=karma_negative_group,
  106. )
  107. @capture_err
  108. async def downvote(_, message):
  109. if not await is_karma_on(message.chat.id):
  110. return
  111. if not message.reply_to_message.from_user:
  112. return
  113. if not message.from_user:
  114. return
  115. if message.reply_to_message.from_user.id == message.from_user.id:
  116. return
  117. chat_id = message.chat.id
  118. user_id = message.reply_to_message.from_user.id
  119. user_mention = message.reply_to_message.from_user.mention
  120. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  121. if current_karma:
  122. current_karma = current_karma["karma"]
  123. karma = current_karma - 1
  124. new_karma = {"karma": karma}
  125. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  126. else:
  127. karma = -1
  128. new_karma = {"karma": karma}
  129. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  130. await message.reply_text(
  131. f"{user_mention} 的声望 -1\n当前声望:{karma}"
  132. )
  133. @app.on_message(
  134. filters.group
  135. & (
  136. filters.command("karma")
  137. | filters.regex(r"^(?:声望|声望排行)$")
  138. )
  139. )
  140. @capture_err
  141. async def command_karma(_, message):
  142. chat_id = message.chat.id
  143. if not message.reply_to_message:
  144. m = await message.reply_text("正在统计声望...")
  145. try:
  146. karma = await get_karmas(chat_id)
  147. if not karma:
  148. return await m.edit("本群尚无声望记录。")
  149. karma_dicc = {}
  150. for i in karma:
  151. try:
  152. user_id = await alpha_to_int(i)
  153. user_karma = karma[i]["karma"]
  154. karma_dicc[str(user_id)] = user_karma
  155. except Exception:
  156. continue
  157. if not karma_dicc:
  158. return await m.edit("本群尚无声望记录。")
  159. karma_sorted = sorted(
  160. karma_dicc.items(),
  161. key=lambda item: item[1],
  162. reverse=True
  163. )
  164. try:
  165. user_ids_needed = [int(uid) for uid, _ in karma_sorted[:50]]
  166. fetched = await app.get_users(user_ids_needed)
  167. users = fetched if isinstance(fetched, list) else [fetched]
  168. userdb = {int(user.id): user_display_name(user) for user in users}
  169. except Exception:
  170. return await m.edit("获取成员资料失败,请稍后重试。")
  171. karma_display = {}
  172. limit = 0
  173. for user_id_str, karma_count in karma_sorted:
  174. if limit >= 15:
  175. break
  176. user_id_int = int(user_id_str)
  177. if user_id_int not in userdb:
  178. continue
  179. label = userdb[user_id_int]
  180. karma_display[f"{label} · {user_id_int}"] = [f"**{karma_count}**"]
  181. limit += 1
  182. if not karma_display:
  183. return await m.edit("没有可显示的声望成员。")
  184. msg = f"{message.chat.title} 的声望排行"
  185. await m.edit(section(msg, karma_display))
  186. except Exception:
  187. await m.edit("声望统计失败,请稍后重试。")
  188. return
  189. else:
  190. if not message.reply_to_message.from_user:
  191. return await message.reply("匿名身份没有声望记录。")
  192. user_id = message.reply_to_message.from_user.id
  193. try:
  194. karma = await get_karma(chat_id, await int_to_alpha(user_id))
  195. karma_value = karma["karma"] if karma else 0
  196. await message.reply_text(f"**当前声望:** __{karma_value}__")
  197. except Exception:
  198. await message.reply_text("获取声望失败,请稍后重试。")
  199. @app.on_message(filters.command("karma_toggle") & ~filters.private)
  200. @adminsOnly("can_change_info")
  201. async def captcha_state(_, message):
  202. usage = "**用法:**\n/karma_toggle [开启|关闭]"
  203. if len(message.command) != 2:
  204. return await message.reply_text(usage)
  205. chat_id = message.chat.id
  206. state = message.text.split(None, 1)[1].strip()
  207. state = state.lower()
  208. state = {
  209. "开启": "enable",
  210. "打开": "enable",
  211. "关闭": "disable",
  212. }.get(state, state)
  213. if state == "enable":
  214. await karma_on(chat_id)
  215. await message.reply_text("本群声望系统已开启。")
  216. elif state == "disable":
  217. await karma_off(chat_id)
  218. await message.reply_text("本群声望系统已关闭。")
  219. else:
  220. await message.reply_text(usage)