karma.py 7.0 KB

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