karma.py 7.2 KB

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