karma.py 6.2 KB

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