TheHamkerCat 5 лет назад
Родитель
Сommit
7e77152d58
2 измененных файлов с 144 добавлено и 2 удалено
  1. 100 0
      wbb/modules/karma.py
  2. 44 2
      wbb/utils/dbfunctions.py

+ 100 - 0
wbb/modules/karma.py

@@ -0,0 +1,100 @@
+from wbb import app
+from wbb.utils.errors import capture_err
+from wbb.utils.json_prettify import json_prettify
+from wbb.utils.dbfunctions import (update_karma, get_karma, get_karmas,
+    int_to_alpha, alpha_to_int)
+from pyrogram import filters
+
+
+regex_upvote = r"^((?i)\+|\+\+|\+1|thx|tnx|ty|thank you|thanx|thanks|pro|cool|good|👍)$"
+regex_downvote = r"^(\-|\-\-|\-1|👎)$"
+
+
+@app.on_message(filters.text
+                & filters.group
+                & filters.incoming
+                & filters.reply
+                & filters.regex(regex_upvote)
+                & ~filters.via_bot
+                & ~filters.bot
+                & ~filters.edited, group=3)
+async def upvote(_, message):
+    if message.reply_to_message.from_user.id == message.from_user.id:
+        return
+    chat_id = message.chat.id
+    user_id = message.reply_to_message.from_user.id
+    user_mention = message.reply_to_message.from_user.mention
+    current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
+    if current_karma:
+        current_karma = current_karma['karma']
+        karma = current_karma + 1
+        new_karma = {"karma": karma}
+        await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
+    else:
+        karma = 1
+        new_karma = {"karma": karma}
+        await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
+    await message.reply_text(
+        f'Incremented Karma of {user_mention} By 1 \nTotal Points: {karma}'
+    )
+
+
+@app.on_message(filters.text
+                & filters.group
+                & filters.incoming
+                & filters.reply
+                & filters.regex(regex_downvote)
+                & ~filters.via_bot
+                & ~filters.bot
+                & ~filters.edited, group=4)
+async def downvote(_, message):
+    if message.reply_to_message.from_user.id == message.from_user.id:
+        return
+    chat_id = message.chat.id
+    user_id = message.reply_to_message.from_user.id
+    user_mention = message.reply_to_message.from_user.mention
+    current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
+    if current_karma:
+        current_karma = current_karma['karma']
+        karma = current_karma - 1
+        new_karma = {"karma": karma}
+        await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
+    else:
+        karma = 1
+        new_karma = {"karma": karma}
+        await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
+    await message.reply_text(
+    f'Decremented Karma Of {user_mention} By 1 \nTotal Points: {karma}'
+)
+
+
+@app.on_message(filters.command("karma") & filters.group)
+async def karma(_, message):
+    chat_id = message.chat.id
+
+    if not message.reply_to_message:
+        karma = await get_karmas(chat_id)
+        msg = f"**Karma list of {message.chat.title}:- **\n"
+#        output = await json_prettify(karma)
+        limit = 0
+        for i in karma:
+            if limit > 9:
+                break
+            ii = ""
+            user_id = await alpha_to_int(i)
+            user_karma = karma[i]['karma']
+            user_name = (await app.get_users(user_id)).username
+            ii += f"{user_name}  {user_karma}"
+            msg += f"`{ii}`\n"
+            limit += 1
+        await message.reply_text(msg)
+    else:
+        user_id = message.reply_to_message.from_user.id
+        karma = await get_karma(chat_id, await int_to_alpha(user_id))
+        if karma:
+            karma = karma['karma']
+            await message.reply_text(f'**Total Points**: __{karma}__')
+        else:
+            karma = 0
+            await message.reply_text(f'**Total Points**: __{karma}__')
+

+ 44 - 2
wbb/utils/dbfunctions.py

@@ -5,7 +5,7 @@ from typing import Dict, List, Union
 notesdb = db.notes
 filtersdb = db.filters
 warnsdb = db.warns
-
+karmadb = db.karma
 
 """ Notes functions """
 
@@ -132,7 +132,7 @@ async def delete_filter(chat_id: int, name: str) -> bool:
 
 
 """ Warn function """
-async def int_to_alpha(user_id):
+async def int_to_alpha(user_id: int) -> str:
     alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
     text = ""
     user_id = str(user_id)
@@ -141,6 +141,16 @@ async def int_to_alpha(user_id):
     return text
 
 
+async def alpha_to_int(user_id_alphabet: str) -> int:
+    alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
+    user_id = ""
+    for i in user_id_alphabet:
+        index = alphabet.index(i)
+        user_id += str(index)
+    user_id = int(user_id)
+    return user_id
+
+
 async def get_warns(chat_id: int) -> Dict[str, int]:
     warns = await warnsdb.find_one({"chat_id": chat_id})
     if warns:
@@ -191,3 +201,35 @@ async def remove_warns(chat_id: int, name: str) -> bool:
     return False
 
 
+""" Karma functions """
+
+
+async def get_karmas(chat_id: int) -> Dict[str, int]:
+    karma = await karmadb.find_one({"chat_id": chat_id})
+    if karma:
+        karma = karma['karma']
+    else:
+        karma = {}
+    return karma
+
+
+async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
+    name = name.lower().strip()
+    karmas = await get_karmas(chat_id)
+    if name in karmas:
+        return karmas[name]
+
+
+async def update_karma(chat_id: int, name: str, karma: dict):
+    name = name.lower().strip()
+    karmas = await get_karmas(chat_id)
+    karmas[name] = karma
+    await karmadb.update_one(
+        {"chat_id": chat_id},
+        {
+            "$set": {
+                "karma": karmas
+            }
+        },
+        upsert=True
+    )