karma.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 asyncio
  21. from pyrogram import filters
  22. from wbb import app
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.modules.admin import member_permissions
  25. from wbb.utils.dbfunctions import (alpha_to_int, get_karma, get_karmas,
  26. int_to_alpha, is_karma_on, karma_off,
  27. karma_on, update_karma)
  28. from wbb.utils.filter_groups import karma_negative_group, karma_positive_group
  29. __MODULE__ = "Karma"
  30. __HELP__ = """[UPVOTE] - Use upvote keywords like "+", "+1", "thanks" etc to upvote a message.
  31. [DOWNVOTE] - Use downvote keywords like "-", "-1", etc to downvote a message.
  32. /karma_toggle [ON|OFF] - Enable or Disable Karma System In Your Chat.
  33. Reply to a message with /karma to check a user's karma
  34. Send /karma without replying to any message to chek karma list of top 10 users"""
  35. regex_upvote = r"^((?i)\+|\+\+|\+1|thx|tnx|ty|thank you|thanx|thanks|pro|cool|good|👍)$"
  36. regex_downvote = r"^(\-|\-\-|\-1|👎)$"
  37. @app.on_message(
  38. filters.text
  39. & filters.group
  40. & filters.incoming
  41. & filters.reply
  42. & filters.regex(regex_upvote)
  43. & ~filters.via_bot
  44. & ~filters.bot
  45. & ~filters.edited,
  46. group=karma_positive_group,
  47. )
  48. @capture_err
  49. async def upvote(_, message):
  50. if not await is_karma_on(message.chat.id):
  51. return
  52. if not message.reply_to_message.from_user:
  53. return
  54. if not message.from_user:
  55. return
  56. if message.reply_to_message.from_user.id == message.from_user.id:
  57. return
  58. chat_id = message.chat.id
  59. user_id = message.reply_to_message.from_user.id
  60. user_mention = message.reply_to_message.from_user.mention
  61. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  62. if current_karma:
  63. current_karma = current_karma["karma"]
  64. karma = current_karma + 1
  65. new_karma = {"karma": karma}
  66. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  67. else:
  68. karma = 1
  69. new_karma = {"karma": karma}
  70. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  71. await message.reply_text(
  72. f"Incremented Karma of {user_mention} By 1 \nTotal Points: {karma}"
  73. )
  74. @app.on_message(
  75. filters.text
  76. & filters.group
  77. & filters.incoming
  78. & filters.reply
  79. & filters.regex(regex_downvote)
  80. & ~filters.via_bot
  81. & ~filters.bot
  82. & ~filters.edited,
  83. group=karma_negative_group,
  84. )
  85. @capture_err
  86. async def downvote(_, message):
  87. if not await is_karma_on(message.chat.id):
  88. return
  89. if not message.reply_to_message.from_user:
  90. return
  91. if not message.from_user:
  92. return
  93. if message.reply_to_message.from_user.id == message.from_user.id:
  94. return
  95. chat_id = message.chat.id
  96. user_id = message.reply_to_message.from_user.id
  97. user_mention = message.reply_to_message.from_user.mention
  98. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  99. if current_karma:
  100. current_karma = current_karma["karma"]
  101. karma = current_karma - 1
  102. new_karma = {"karma": karma}
  103. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  104. else:
  105. karma = 1
  106. new_karma = {"karma": karma}
  107. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  108. await message.reply_text(
  109. f"Decremented Karma Of {user_mention} By 1 \nTotal Points: {karma}"
  110. )
  111. @app.on_message(filters.command("karma") & filters.group)
  112. @capture_err
  113. async def karma(_, message):
  114. chat_id = message.chat.id
  115. if not message.reply_to_message:
  116. m = await message.reply_text("Analyzing Karma...Will Take 20 Seconds")
  117. karma = await get_karmas(chat_id)
  118. msg = f"**Karma list of {message.chat.title}:- **\n"
  119. limit = 0
  120. karma_dicc = {}
  121. for i in karma:
  122. user_id = await alpha_to_int(i)
  123. user_karma = karma[i]["karma"]
  124. karma_dicc[str(user_id)] = user_karma
  125. karma_arranged = dict(
  126. sorted(karma_dicc.items(), key=lambda item: item[1], reverse=True)
  127. )
  128. if not karma_arranged:
  129. await m.edit("No karma in DB for this chat.")
  130. return
  131. for user_idd, karma_count in karma_arranged.items():
  132. if limit > 9:
  133. break
  134. try:
  135. user = await app.get_users(int(user_idd))
  136. await asyncio.sleep(2)
  137. except Exception:
  138. continue
  139. first_name = user.first_name
  140. if not first_name:
  141. continue
  142. username = user.username
  143. msg += (
  144. f"**{karma_count}** {(first_name[0:12] + '...') if len(first_name) > 12 else first_name} `{('@' + username) if username else user_idd}`\n"
  145. )
  146. limit += 1
  147. await m.edit(msg)
  148. else:
  149. user_id = message.reply_to_message.from_user.id
  150. karma = await get_karma(chat_id, await int_to_alpha(user_id))
  151. if karma:
  152. karma = karma["karma"]
  153. await message.reply_text(f"**Total Points**: __{karma}__")
  154. else:
  155. karma = 0
  156. await message.reply_text(f"**Total Points**: __{karma}__")
  157. @app.on_message(filters.command("karma_toggle") & ~filters.private)
  158. @capture_err
  159. async def captcha_state(_, message):
  160. usage = "**Usage:**\n/karma_toggle [ON|OFF]"
  161. if len(message.command) != 2:
  162. await message.reply_text(usage)
  163. return
  164. user_id = message.from_user.id
  165. chat_id = message.chat.id
  166. permissions = await member_permissions(chat_id, user_id)
  167. if "can_restrict_members" not in permissions:
  168. await message.reply_text("You don't have enough permissions.")
  169. return
  170. state = message.text.split(None, 1)[1].strip()
  171. state = state.lower()
  172. if state == "on":
  173. await karma_on(chat_id)
  174. await message.reply_text("Enabled karma system.")
  175. elif state == "off":
  176. await karma_off(chat_id)
  177. await message.reply_text("Disabled karma system.")
  178. else:
  179. await message.reply_text(usage)