File size: 6,686 Bytes
c7dfe8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# https://github.com/Team-ProjectCodeX
# UPDATED BY https://t.me/O_okarma
# https://t.me/ProjectCodeX

# <============================================== IMPORTS =========================================================>
import asyncio

from pyrogram import filters

from Database.mongodb.karma_mongo import *
from Mikobot import OWNER_ID, app
from Mikobot.utils.can_restrict import can_restrict
from Mikobot.utils.errors import capture_err

# <=======================================================================================================>

karma_positive_group = 3
karma_negative_group = 4


# <================================================ FUNCTION =======================================================>
@app.on_message(
    filters.text
    & filters.group
    & filters.incoming
    & filters.reply
    & filters.regex(
        r"^(\+|\+\+|\+1|thx|tnx|ty|tq|thank you|thanx|thanks|pro|cool|good|agree|makasih|๐Ÿ‘|\+\+ .+)$"
    )
    & ~filters.via_bot
    & ~filters.bot,
    group=karma_positive_group,
)
@capture_err
async def upvote(_, message):
    if not await is_karma_on(message.chat.id):
        return
    reply_user = message.reply_to_message.from_user
    current_user = message.from_user
    if not (reply_user and current_user):
        return
    if reply_user.id == OWNER_ID:
        await message.reply_text("How so pro?")
        return
    if reply_user.id == current_user.id:
        return

    chat_id = message.chat.id
    user_id = reply_user.id
    user_mention = reply_user.mention
    current_karma = await get_karma(chat_id, await int_to_alpha(user_id))
    karma = current_karma["karma"] + 1 if current_karma else 1
    new_karma = {"karma": karma}
    await update_karma(chat_id, await int_to_alpha(user_id), new_karma)
    await message.reply_text(
        f"๐—œ๐—ป๐—ฐ๐—ฟ๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฒ๐—ฑ ๐—ธ๐—ฎ๐—ฟ๐—บ๐—ฎ ๐—ผ๐—ณ {user_mention} ๐—ฏ๐˜† 1.\n**โญ๏ธ ๐—ง๐—ข๐—ง๐—”๐—Ÿ ๐—ฃ๐—ข๐—œ๐—ก๐—ง๐—ฆ:** {karma}"
    )


@app.on_message(
    filters.text
    & filters.group
    & filters.incoming
    & filters.reply
    & filters.regex(r"^(-|--|-1|not cool|disagree|worst|bad|๐Ÿ‘Ž|-- .+)$")
    & ~filters.via_bot
    & ~filters.bot,
    group=karma_negative_group,
)
@capture_err
async def downvote(_, message):
    if not await is_karma_on(message.chat.id):
        return
    reply_user = message.reply_to_message.from_user
    current_user = message.from_user
    if not (reply_user and current_user):
        return
    if reply_user.id == OWNER_ID:
        await message.reply_text("I know him, so I'm not gonna do that, baby.")
        return
    if reply_user.id == current_user.id:
        return

    user_id = reply_user.id
    user_mention = reply_user.mention
    current_karma = await get_karma(message.chat.id, await int_to_alpha(user_id))
    karma = current_karma["karma"] - 1 if current_karma else 0
    new_karma = {"karma": karma}
    await update_karma(message.chat.id, await int_to_alpha(user_id), new_karma)
    await message.reply_text(
        f"๐——๐—ฒ๐—ฐ๐—ฟ๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฒ๐—ฑ ๐—ธ๐—ฎ๐—ฟ๐—บ๐—ฎ ๐—ผ๐—ณ {user_mention} ๐—ฏ๐˜† 1.\n**โญ๏ธ ๐—ง๐—ข๐—ง๐—”๐—Ÿ ๐—ฃ๐—ข๐—œ๐—ก๐—ง๐—ฆ:** {karma}"
    )


@app.on_message(filters.command("karmastat") & filters.group)
@capture_err
async def karma(_, message):
    if not message.reply_to_message:
        m = await message.reply_text("Analyzing karma... This may take a while.")
        karma = await get_karmas(message.chat.id)
        if not karma:
            await m.edit_text("No karma in the database for this chat.")
            return
        msg = f"**๐ŸŽ– ๐—ž๐—”๐—ฅ๐— ๐—” ๐—Ÿ๐—œ๐—ฆ๐—ง ๐—ข๐—™ {message.chat.title} :**\n"
        limit = 0
        karma_dicc = {}
        for i in karma:
            user_id = await alpha_to_int(i)
            user_karma = karma[i]["karma"]
            karma_dicc[str(user_id)] = user_karma
            karma_arranged = dict(
                sorted(karma_dicc.items(), key=lambda item: item[1], reverse=True)
            )
        if not karma_dicc:
            await m.edit_text("No karma in the database for this chat.")
            return
        for user_idd, karma_count in karma_arranged.items():
            if limit > 9:
                break
            try:
                user = await _.get_users(int(user_idd))
                await asyncio.sleep(0.8)
            except Exception:
                continue
            first_name = user.first_name
            if not first_name:
                continue
            msg += f"`{karma_count}`  {(first_name[0:12] + '...') if len(first_name) > 12 else first_name}\n"
            limit += 1
        await m.edit_text(msg)
    else:
        user_id = message.reply_to_message.from_user.id
        karma = await get_karma(message.chat.id, await int_to_alpha(user_id))
        karma = karma["karma"] if karma else 0
        await message.reply_text(f"**โญ๏ธ ๐—ง๐—ข๐—ง๐—”๐—Ÿ ๐—ฃ๐—ข๐—œ๐—ก๐—ง๐—ฆ:** {karma}")


@app.on_message(filters.command("karma"))
@can_restrict
async def karma_toggle_xd(_, message):
    usage = "**Usage:**\n/karma [ON|OFF]"
    if len(message.command) != 2:
        return await message.reply_text(usage)
    chat_id = message.chat.id
    state = message.text.split(None, 1)[1].strip().lower()

    if state == "on":
        disabled = karmadb.find_one({"chat_id_toggle": chat_id})
        if disabled:
            karmadb.delete_one({"chat_id_toggle": chat_id})
            await message.reply_text("Enabled the karma system.")
        else:
            await message.reply_text("Karma system is already enabled.")
    elif state == "off":
        disabled = karmadb.find_one({"chat_id_toggle": chat_id})
        if disabled:
            await message.reply_text("Karma system is already disabled.")
        else:
            karmadb.insert_one({"chat_id_toggle": chat_id})
            await message.reply_text("Disabled the karma system.")
    else:
        await message.reply_text(usage)


# <=================================================== HELP ====================================================>


__mod_name__ = "KARMA"
__help__ = """

โž  *UPVOTE* - Use upvote keywords like "+", "+1", "thanks", etc. to upvote a message.
โž  *DOWNVOTE* - Use downvote keywords like "-", "-1", etc. to downvote a message.

โž  *Commands*

ยป /karmastat:- `Reply to a user to check that user's karma points`

ยป /karmastat:- `Send without replying to any message to check karma point list of top 10`

ยป /karma [OFF|ON] - `Enable or disable karma system in your chat.`
"""
# <================================================ END =======================================================>