karma.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. MIT License
  3. Copyright (c) present 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. 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.reply_to_message.from_user.id
  104. user_mention = message.reply_to_message.from_user.mention
  105. current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
  106. if current_karma:
  107. current_karma = current_karma["karma"]
  108. karma = current_karma - 1
  109. new_karma = {"karma": karma}
  110. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  111. else:
  112. karma = 1
  113. new_karma = {"karma": karma}
  114. await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
  115. await message.reply_text(
  116. f"Decremented Karma Of {user_mention} By 1 \nTotal Points: {karma}"
  117. )
  118. @app.on_message(
  119. filters.command("karma")
  120. & filters.group
  121. )
  122. @capture_err
  123. async def command_karma(_, message):
  124. chat_id = message.chat.id
  125. if not message.reply_to_message:
  126. m = await message.reply_text("Analyzing Karma...")
  127. karma = await get_karmas(chat_id)
  128. if not karma:
  129. return await m.edit("No karma in DB for this chat.")
  130. msg = f"Karma list of {message.chat.title}"
  131. limit = 0
  132. karma_dicc = {}
  133. for i in karma:
  134. user_id = await alpha_to_int(i)
  135. user_karma = karma[i]["karma"]
  136. karma_dicc[str(user_id)] = user_karma
  137. karma_arranged = dict(
  138. sorted(
  139. karma_dicc.items(),
  140. key=lambda item: item[1],
  141. reverse=True,
  142. )
  143. )
  144. if not karma_dicc:
  145. return await m.edit("No karma in DB for this chat.")
  146. userdb = await get_user_id_and_usernames(app)
  147. karma = {}
  148. for user_idd, karma_count in karma_arranged.items():
  149. if limit > 15:
  150. break
  151. if int(user_idd) not in list(userdb.keys()):
  152. continue
  153. username = userdb[int(user_idd)]
  154. karma["@" + username] = ["**" + str(karma_count) + "**"]
  155. limit += 1
  156. await m.edit(section(msg, karma))
  157. else:
  158. if not message.reply_to_message.from_user:
  159. return await message.reply("Anon user hash no karma.")
  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(
  169. filters.command("karma_toggle")
  170. & ~filters.private
  171. )
  172. @adminsOnly("can_change_info")
  173. async def captcha_state(_, message):
  174. usage = "**Usage:**\n/karma_toggle [ENABLE|DISABLE]"
  175. if len(message.command) != 2:
  176. return await message.reply_text(usage)
  177. chat_id = message.chat.id
  178. state = message.text.split(None, 1)[1].strip()
  179. state = state.lower()
  180. if state == "enable":
  181. await karma_on(chat_id)
  182. await message.reply_text("Enabled karma system.")
  183. elif state == "disable":
  184. await karma_off(chat_id)
  185. await message.reply_text("Disabled karma system.")
  186. else:
  187. await message.reply_text(usage)