forked from TeamYukki/YukkiChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
314 lines (292 loc) · 11.3 KB
/
main.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#
# Copyright (C) 2021-2022 by TeamYukki@Github, < https://github.com/YukkiChatBot >.
#
# This file is part of < https://github.com/TeamYukki/YukkiChatBot > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/TeamYukki/YukkiChatBot/blob/master/LICENSE >
#
# All rights reserved.
#
import asyncio
from sys import version as pyver
import pyrogram
from pyrogram import __version__ as pyrover
from pyrogram import filters, idle
from pyrogram.errors import FloodWait
from pyrogram.types import Message
import config
import mongo
from mongo import db
loop = asyncio.get_event_loop()
SUDO_USERS = config.SUDO_USER
app = pyrogram.Client(
":YukkiBot:",
config.API_ID,
config.API_HASH,
bot_token=config.BOT_TOKEN,
)
save = {}
grouplist = 1
async def init():
await app.start()
@app.on_message(filters.command("help"))
async def start_command(_, message: Message):
if await mongo.is_banned_user(message.from_user.id):
return
await mongo.add_served_user(message.from_user.id)
await message.reply_text(config.PRIVATE_START_MESSAGE)
@app.on_message(
filters.command("mode") & filters.user(SUDO_USERS)
)
async def mode_func(_, message: Message):
if db is None:
return await message.reply_text(
"MONGO_DB_URI var not defined. Please define it first"
)
usage = "**Usage:**\n\n/mode [group | private]\n\n**Group**: All the incoming messages will be forwarded to Log group.\n\n**Private**: All the incoming messages will be forwarded to the Private Messages of SUDO_USERS"
if len(message.command) != 2:
return await message.reply_text(usage)
state = message.text.split(None, 1)[1].strip()
state = state.lower()
if state == "group":
await mongo.group_on()
await message.reply_text(
"Group Mode Enabled. All the incoming messages will be forwarded to LOG Group"
)
elif state == "private":
await mongo.group_off()
await message.reply_text(
"Private Mode Enabled. All the incoming messages will be forwarded to Private Message of all SUDO_USERs"
)
else:
await message.reply_text(usage)
@app.on_message(
filters.command("block") & filters.user(SUDO_USERS)
)
async def block_func(_, message: Message):
if db is None:
return await message.reply_text(
"MONGO_DB_URI var not defined. Please define it first"
)
if message.reply_to_message:
if not message.reply_to_message.forward_sender_name:
return await message.reply_text(
"Please reply to forwarded messages only."
)
replied_id = message.reply_to_message_id
try:
replied_user_id = save[replied_id]
except Exception as e:
print(e)
return await message.reply_text(
"Failed to fetch user. You might've restarted bot or some error happened. Please check logs"
)
if await mongo.is_banned_user(replied_user_id):
return await message.reply_text("Already Blocked")
else:
await mongo.add_banned_user(replied_user_id)
await message.reply_text("Banned User from The Bot")
try:
await app.send_message(
replied_user_id,
"You're now banned from using the Bot by admins.",
)
except:
pass
else:
return await message.reply_text(
"Reply to a user's forwarded message to block him from using the bot"
)
@app.on_message(
filters.command("unblock") & filters.user(SUDO_USERS)
)
async def unblock_func(_, message: Message):
if db is None:
return await message.reply_text(
"MONGO_DB_URI var not defined. Please define it first"
)
if message.reply_to_message:
if not message.reply_to_message.forward_sender_name:
return await message.reply_text(
"Please reply to forwarded messages only."
)
replied_id = message.reply_to_message_id
try:
replied_user_id = save[replied_id]
except Exception as e:
print(e)
return await message.reply_text(
"Failed to fetch user. You might've restarted bot or some error happened. Please check logs"
)
if not await mongo.is_banned_user(replied_user_id):
return await message.reply_text("Already UnBlocked")
else:
await mongo.remove_banned_user(replied_user_id)
await message.reply_text(
"Unblocked User from The Bot"
)
try:
await app.send_message(
replied_user_id,
"You're now unbanned from the Bot by admins.",
)
except:
pass
else:
return await message.reply_text(
"Reply to a user's forwarded message to unblock him from the bot"
)
@app.on_message(
filters.command("stats") & filters.user(SUDO_USERS)
)
async def stats_func(_, message: Message):
if db is None:
return await message.reply_text(
"MONGO_DB_URI var not defined. Please define it first"
)
served_users = len(await mongo.get_served_users())
blocked = await mongo.get_banned_count()
text = f""" **ChatBot Stats:**
**Python Version :** {pyver.split()[0]}
**Pyrogram Version :** {pyrover}
**Served Users:** {served_users}
**Blocked Users:** {blocked}"""
await message.reply_text(text)
@app.on_message(
filters.command("broadcast") & filters.user(SUDO_USERS)
)
async def broadcast_func(_, message: Message):
if db is None:
return await message.reply_text(
"MONGO_DB_URI var not defined. Please define it first"
)
if message.reply_to_message:
x = message.reply_to_message.message_id
y = message.chat.id
else:
if len(message.command) < 2:
return await message.reply_text(
"**Usage**:\n/broadcast [MESSAGE] or [Reply to a Message]"
)
query = message.text.split(None, 1)[1]
susr = 0
served_users = []
susers = await mongo.get_served_users()
for user in susers:
served_users.append(int(user["user_id"]))
for i in served_users:
try:
await app.forward_messages(
i, y, x
) if message.reply_to_message else await app.send_message(
i, text=query
)
susr += 1
except FloodWait as e:
flood_time = int(e.x)
if flood_time > 200:
continue
await asyncio.sleep(flood_time)
except Exception:
pass
try:
await message.reply_text(
f"**Broadcasted Message to {susr} Users.**"
)
except:
pass
@app.on_message(filters.private & ~filters.edited)
async def incoming_private(_, message):
user_id = message.from_user.id
if await mongo.is_banned_user(user_id):
return
if user_id in SUDO_USERS:
if message.reply_to_message:
if (
message.text == "/unblock"
or message.text == "/block"
or message.text == "/broadcast"
):
return
if not message.reply_to_message.forward_sender_name:
return await message.reply_text(
"Please reply to forwarded messages only."
)
replied_id = message.reply_to_message_id
try:
replied_user_id = save[replied_id]
except Exception as e:
print(e)
return await message.reply_text(
"Failed to fetch user. You might've restarted bot or some error happened. Please check logs"
)
try:
return await app.copy_message(
replied_user_id,
message.chat.id,
message.message_id,
)
except Exception as e:
print(e)
return await message.reply_text(
"Failed to send the message, User might have blocked the bot or something wrong happened. Please check logs"
)
else:
if await mongo.is_group():
try:
forwarded = await app.forward_messages(
config.LOG_GROUP_ID,
message.chat.id,
message.message_id,
)
save[forwarded.message_id] = user_id
except:
pass
else:
for user in SUDO_USERS:
try:
forwarded = await app.forward_messages(
user, message.chat.id, message.message_id
)
save[forwarded.message_id] = user_id
except:
pass
@app.on_message(
filters.group & ~filters.edited & filters.user(SUDO_USERS),
group=grouplist,
)
async def incoming_groups(_, message):
if message.reply_to_message:
if (
message.text == "/unblock"
or message.text == "/block"
or message.text == "/broadcast"
):
return
replied_id = message.reply_to_message_id
if not message.reply_to_message.forward_sender_name:
return await message.reply_text(
"Please reply to forwarded messages only."
)
try:
replied_user_id = save[replied_id]
except Exception as e:
print(e)
return await message.reply_text(
"Failed to fetch user. You might've restarted bot or some error happened. Please check logs"
)
try:
return await app.copy_message(
replied_user_id,
message.chat.id,
message.message_id,
)
except Exception as e:
print(e)
return await message.reply_text(
"Failed to send the message, User might have blocked the bot or something wrong happened. Please check logs"
)
print("[LOG] - Yukki Chat Bot Started")
await idle()
if __name__ == "__main__":
loop.run_until_complete(init())