karma.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """
  2. MIT License
  3. Copyright (c) 2023 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 (
  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. from wbb.utils.functions import get_user_id_and_usernames
  38. __MODULE__ = "Karma"
  39. __HELP__ = """[UPVOTE] - Use upvote keywords like "+", "+1", "thanks", etc to upvote a message.
  40. [DOWNVOTE] - Use downvote keywords like "-", "-1", etc to downvote a message.
  41. /karma_toggle [ENABLE|DISABLE] - Enable or Disable Karma System In Your Chat.
  42. Reply to a message with /karma to check a user's karma
  43. Send /karma without replying to any message to check karma list of top 10 users"""
  44. regex_upvote = r"^(\+|\+\+|\+1|thx|tnx|tq|ty|thankyou|thank you|thanx|thanks|pro|cool|good|agree|👍|\+\+ .+)$"
  45. regex_downvote = r"^(-|--|-1|not cool|disagree|worst|bad|👎|-- .+)$"
  46. @app.on_message(
  47. filters.text
  48. & filters.group
  49. & filters.incoming
  50. & filters.reply
  51. & filters.regex(regex_upvote, re.IGNORECASE)
  52. & ~filters.via_bot
  53. & ~filters.bot,
  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. group=karma_negative_group,
  91. )
  92. @capture_err
  93. async def downvote(_, message):
  94. if not await is_karma_on(message.chat.id):
  95. return
  96. if not message.reply_to_message.from_user:
  97. return
  98. if not message.from_user:
  99. return
  100. if message.reply_to_message.from_user.id == message.from_user.id:
  101. return
  102. chat_id = message.chat.id
  103. user_id = message.from_user.id
  104. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  105. if current_karma:
  106. current_karma = current_karma["karma"]
  107. karma = current_karma - 1
  108. new_karma = {"karma": karma}
  109. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  110. else:
  111. karma = 1
  112. new_karma = {"karma": karma}
  113. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  114. user_id = message.reply_to_message.from_user.id
  115. user_mention = message.reply_to_message.from_user.mention
  116. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  117. if current_karma:
  118. current_karma = current_karma["karma"]
  119. karma = current_karma - 1
  120. new_karma = {"karma": karma}
  121. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  122. else:
  123. karma = 1
  124. new_karma = {"karma": karma}
  125. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  126. await message.reply_text(
  127. f"Decremented Karma of {user_mention} By 1 \nTotal Points: {karma}"
  128. )
  129. @app.on_message(filters.command("karma") & filters.group)
  130. @capture_err
  131. async def command_karma(_, message):
  132. chat_id = message.chat.id
  133. if not message.reply_to_message:
  134. m = await message.reply_text("Analyzing Karma...")
  135. karma = await get_karmas(chat_id)
  136. if not karma:
  137. return await m.edit("No karma in DB for this chat.")
  138. msg = f"Karma list of {message.chat.title}"
  139. limit = 0
  140. karma_dicc = {}
  141. for i in karma:
  142. user_id = await alpha_to_int(i)
  143. user_karma = karma[i]["karma"]
  144. karma_dicc[str(user_id)] = user_karma
  145. karma_arranged = dict(
  146. sorted(
  147. karma_dicc.items(),
  148. key=lambda item: item[1],
  149. reverse=True,
  150. )
  151. )
  152. if not karma_dicc:
  153. return await m.edit("No karma in DB for this chat.")
  154. userdb = await get_user_id_and_usernames(app)
  155. karma = {}
  156. for user_idd, karma_count in karma_arranged.items():
  157. if limit > 15:
  158. break
  159. if int(user_idd) not in list(userdb.keys()):
  160. continue
  161. username = userdb[int(user_idd)]
  162. karma["@" + username] = ["**" + str(karma_count) + "**"]
  163. limit += 1
  164. await m.edit(section(msg, karma))
  165. else:
  166. if not message.reply_to_message.from_user:
  167. return await message.reply("Anon user has no karma.")
  168. user_id = message.reply_to_message.from_user.id
  169. karma = await get_karma(chat_id, await int_to_alpha(user_id))
  170. if karma:
  171. karma = karma["karma"]
  172. await message.reply_text(f"**Total Points**: __{karma}__")
  173. else:
  174. karma = 0
  175. await message.reply_text(f"**Total Points**: __{karma}__")
  176. @app.on_message(filters.command("karma_toggle") & ~filters.private)
  177. @adminsOnly("can_change_info")
  178. async def captcha_state(_, message):
  179. usage = "**Usage:**\n/karma_toggle [ENABLE|DISABLE]"
  180. if len(message.command) != 2:
  181. return await message.reply_text(usage)
  182. chat_id = message.chat.id
  183. state = message.text.split(None, 1)[1].strip()
  184. state = state.lower()
  185. if state == "enable":
  186. await karma_on(chat_id)
  187. await message.reply_text("Enabled Karma System for this chat.")
  188. elif state == "disable":
  189. await karma_off(chat_id)
  190. await message.reply_text("Disabled Karma System for this chat.")
  191. else:
  192. await message.reply_text(usage)