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 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|ty|thank you|thanx|thanks|pro|cool|good|👍|\+\+ .+)$"
  45. regex_downvote = r"^(-|--|-1|👎|-- .+)$"
  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. & ~filters.edited,
  55. group=karma_positive_group,
  56. )
  57. @capture_err
  58. async def upvote(_, message):
  59. if not await is_karma_on(message.chat.id):
  60. return
  61. if not message.reply_to_message.from_user:
  62. return
  63. if not message.from_user:
  64. return
  65. if message.reply_to_message.from_user.id == message.from_user.id:
  66. return
  67. chat_id = message.chat.id
  68. user_id = message.reply_to_message.from_user.id
  69. user_mention = message.reply_to_message.from_user.mention
  70. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  71. if current_karma:
  72. current_karma = current_karma["karma"]
  73. karma = current_karma + 1
  74. new_karma = {"karma": karma}
  75. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  76. else:
  77. karma = 1
  78. new_karma = {"karma": karma}
  79. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  80. await message.reply_text(
  81. f"Incremented Karma of {user_mention} By 1 \nTotal Points: {karma}"
  82. )
  83. @app.on_message(
  84. filters.text
  85. & filters.group
  86. & filters.incoming
  87. & filters.reply
  88. & filters.regex(regex_downvote, re.IGNORECASE)
  89. & ~filters.via_bot
  90. & ~filters.bot
  91. & ~filters.edited,
  92. group=karma_negative_group,
  93. )
  94. @capture_err
  95. async def downvote(_, message):
  96. if not await is_karma_on(message.chat.id):
  97. return
  98. if not message.reply_to_message.from_user:
  99. return
  100. if not message.from_user:
  101. return
  102. if message.reply_to_message.from_user.id == message.from_user.id:
  103. return
  104. chat_id = message.chat.id
  105. user_id = message.reply_to_message.from_user.id
  106. user_mention = message.reply_to_message.from_user.mention
  107. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  108. if current_karma:
  109. current_karma = current_karma["karma"]
  110. karma = current_karma - 1
  111. new_karma = {"karma": karma}
  112. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  113. else:
  114. karma = 1
  115. new_karma = {"karma": karma}
  116. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  117. await message.reply_text(
  118. f"Decremented Karma Of {user_mention} By 1 \nTotal Points: {karma}"
  119. )
  120. @app.on_message(filters.command("karma") & filters.group & ~filters.edited)
  121. @capture_err
  122. async def command_karma(_, message):
  123. chat_id = message.chat.id
  124. if not message.reply_to_message:
  125. m = await message.reply_text("Analyzing Karma...")
  126. karma = await get_karmas(chat_id)
  127. if not karma:
  128. return await m.edit("No karma in DB for this chat.")
  129. msg = f"Karma list of {message.chat.title}"
  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(),
  139. key=lambda item: item[1],
  140. reverse=True,
  141. )
  142. )
  143. if not karma_dicc:
  144. return await m.edit("No karma in DB for this chat.")
  145. userdb = await get_user_id_and_usernames(app)
  146. karma = {}
  147. for user_idd, karma_count in karma_arranged.items():
  148. if limit > 15:
  149. break
  150. if int(user_idd) not in list(userdb.keys()):
  151. continue
  152. username = userdb[int(user_idd)]
  153. karma["@" + username] = ["**" + str(karma_count) + "**"]
  154. limit += 1
  155. await m.edit(section(msg, karma))
  156. else:
  157. if not message.reply_to_message.from_user:
  158. return await message.reply("Anon user has no karma.")
  159. user_id = message.reply_to_message.from_user.id
  160. karma = await get_karma(chat_id, await int_to_alpha(user_id))
  161. if karma:
  162. karma = karma["karma"]
  163. await message.reply_text(f"**Total Points**: __{karma}__")
  164. else:
  165. karma = 0
  166. await message.reply_text(f"**Total Points**: __{karma}__")
  167. @app.on_message(
  168. filters.command("karma_toggle") & ~filters.private & ~filters.edited)
  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)