karma.py 6.3 KB

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