karma.py 6.7 KB

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