/
__main__.py
300 lines (253 loc) · 11.6 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
""" auto welcome and left messages """
# Copyright (C) 2020-2022 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/UsergeTeam/Userge/blob/master/LICENSE >
#
# All rights reserved.
from userge import userge, filters, Message, get_collection
SAVED_SETTINGS = get_collection("CONFIGS")
WELCOME_COLLECTION = get_collection("welcome")
LEFT_COLLECTION = get_collection("left")
WELCOME_DELETE_TIMEOUT: int = 120
WELCOME_CHATS = filters.chat([])
LEFT_CHATS = filters.chat([])
CHANNEL = userge.getCLogger(__name__)
@userge.on_start
async def _init() -> None:
global WELCOME_DELETE_TIMEOUT # pylint: disable=global-statement
async for i in WELCOME_COLLECTION.find({'on': True}):
if 'mid' not in i:
continue
WELCOME_CHATS.add(i.get('_id'))
async for i in LEFT_COLLECTION.find({'on': True}):
if 'mid' not in i:
continue
LEFT_CHATS.add(i.get('_id'))
wel_t = await SAVED_SETTINGS.find_one({'_id': 'WELCOME_DELETE_TIMEOUT'})
if wel_t:
WELCOME_DELETE_TIMEOUT = wel_t['data']
@userge.on_cmd("setwelcome", about={
'header': "Creates a welcome message in current chat",
'options': {
'{fname}': "add first name",
'{lname}': "add last name",
'{flname}': "add full name",
'{uname}': "username",
'{chat}': "chat name",
'{count}': "chat members count",
'{mention}': "mention user"},
'types': [
'audio', 'animation', 'document', 'photo',
'sticker', 'voice', 'video_note', 'video'],
'examples': [
"{tr}setwelcome Hi {mention}, <b>Welcome</b> to {chat} chat\n"
"or reply to supported media",
"reply {tr}setwelcome to text message or supported media with text"],
'buttons': "<code>[name][buttonurl:link]</code> - <b>add a url button</b>\n"
"<code>[name][buttonurl:link:same]</code> - <b>add a url button to same row</b>"},
allow_channels=False, allow_bots=False, allow_private=False)
async def setwel(msg: Message):
""" set welcome message """
await raw_set(msg, 'Welcome', WELCOME_COLLECTION, WELCOME_CHATS)
@userge.on_cmd("setleft", about={
'header': "Creates a left message in current chat",
'options': {
'{fname}': "add first name",
'{lname}': "add last name",
'{flname}': "add full name",
'{uname}': "username",
'{chat}': "chat name",
'{count}': "chat members count",
'{mention}': "mention user"},
'types': [
'audio', 'animation', 'document', 'photo',
'sticker', 'voice', 'video_note', 'video'],
'examples': [
"{tr}setleft {flname}, Why you left :(\n"
"or reply to supported media",
"reply {tr}setleft to text message or supported media with text"],
'buttons': "<code>[name][buttonurl:link]</code> - <b>add a url button</b>\n"
"<code>[name][buttonurl:link:same]</code> - <b>add a url button to same row</b>"},
allow_channels=False, allow_bots=False, allow_private=False)
async def setleft(msg: Message):
""" set left message """
await raw_set(msg, 'Left', LEFT_COLLECTION, LEFT_CHATS)
@userge.on_cmd("nowelcome", about={
'header': "Disables welcome message in the current chat",
'flags': {'-all': "disable all welcome messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def nowel(msg: Message):
""" disable welcome message """
await raw_no(msg, 'Welcome', WELCOME_COLLECTION, WELCOME_CHATS)
@userge.on_cmd("noleft", about={
'header': "Disables left message in the current chat",
'flags': {'-all': "disable all left messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def noleft(msg: Message):
""" disable left message """
await raw_no(msg, 'Left', LEFT_COLLECTION, LEFT_CHATS)
@userge.on_cmd("dowelcome", about={
'header': "Turns on welcome message in the current chat",
'flags': {'-all': "enable all welcome messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def dowel(msg: Message):
""" enable welcome message """
await raw_do(msg, 'Welcome', WELCOME_COLLECTION, WELCOME_CHATS)
@userge.on_cmd("doleft", about={
'header': "Turns on left message in the current chat :)",
'flags': {'-all': "enable all left messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def doleft(msg: Message):
""" enable left message """
await raw_do(msg, 'Left', LEFT_COLLECTION, LEFT_CHATS)
@userge.on_cmd("delwelcome", about={
'header': "Delete welcome message in the current chat :)",
'flags': {'-all': "delete all welcome messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def delwel(msg: Message):
""" delete welcome message """
await raw_del(msg, 'Welcome', WELCOME_COLLECTION, WELCOME_CHATS)
@userge.on_cmd("delleft", about={
'header': "Delete left message in the current chat :)",
'flags': {'-all': "delete all left messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def delleft(msg: Message):
""" delete left messaage """
await raw_del(msg, 'Left', LEFT_COLLECTION, LEFT_CHATS)
@userge.on_cmd("vwelcome", about={
'header': "Shows welcome message in current chat",
'flags': {'-all': "view all welcome messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def viewwel(msg: Message):
""" view welcome message """
await raw_view(msg, 'Welcome', WELCOME_COLLECTION)
@userge.on_cmd("vleft", about={
'header': "Shows left message in current chat",
'flags': {'-all': "view all left messages"}},
allow_channels=False, allow_bots=False, allow_private=False)
async def viewleft(msg: Message):
""" view left message """
await raw_view(msg, 'Left', LEFT_COLLECTION)
@userge.on_cmd("swelto (\\d+)", about={
'header': "Set auto welcome/left message delete timeout",
'usage': "{tr}swelto [timeout in seconds]",
'examples': "{tr}swelto 15\n{tr}swelto 0 : for disable deletion"})
async def set_welcome_timeout(message: Message):
""" set welcome/left timeout """
global WELCOME_DELETE_TIMEOUT # pylint: disable=global-statement
await message.edit("`Setting auto welcome/left message delete timeout...`")
t_o = int(message.matches[0].group(1))
WELCOME_DELETE_TIMEOUT = t_o
await SAVED_SETTINGS.update_one(
{'_id': 'WELCOME_DELETE_TIMEOUT'}, {"$set": {'data': t_o}}, upsert=True)
if t_o:
await message.edit(
f"`Set auto welcome/left message delete timeout as {t_o} seconds!`", del_in=3)
else:
await message.edit("`Auto welcome/left message deletion disabled!`", del_in=3)
@userge.on_cmd("vwelto", about={'header': "View auto welcome/left message delete timeout"})
async def view_welcome_timeout(message: Message):
""" view welcome/left timeout """
if WELCOME_DELETE_TIMEOUT:
await message.edit(
"`Welcome/Left messages will be deleted after "
f"{WELCOME_DELETE_TIMEOUT} seconds!`",
del_in=5)
else:
await message.edit("`Auto welcome/left message deletion disabled!`", del_in=3)
@userge.on_new_member(WELCOME_CHATS)
async def saywel(msg: Message):
""" welcome message handler """
await raw_say(msg, 'Welcome', WELCOME_COLLECTION)
@userge.on_left_member(LEFT_CHATS)
async def sayleft(msg: Message):
""" left message handler """
await raw_say(msg, 'Left', LEFT_COLLECTION)
async def raw_set(message: Message, name, collection, chats):
replied = message.reply_to_message
string = message.input_or_reply_raw
if string or replied and replied.media:
message_id = await CHANNEL.store(replied, string)
await collection.update_one({'_id': message.chat.id},
{"$set": {'mid': message_id, 'on': True}},
upsert=True)
chats.add(message.chat.id)
out = f"{name} __message has been set for the__\n`{message.chat.title}`"
else:
out = f"**Wrong Syntax**\ncheck `.help .set{name.lower()}`"
await message.edit(text=out, del_in=3)
async def raw_no(message: Message, name, collection, chats):
if '-all' in message.flags:
async for c_l in collection.find({'on': True}):
if 'mid' not in c_l:
continue
chats.remove(c_l['_id'])
await collection.update_many({}, {"$set": {'on': False}})
out = f"`All {name} Messages Disabled Successfully!`"
else:
out = f"`First Set {name} Message!`"
if await collection.find_one_and_update(
{'_id': message.chat.id}, {"$set": {'on': False}}):
if message.chat.id in chats:
chats.remove(message.chat.id)
out = f"`{name} Disabled Successfully!`"
await message.edit(text=out, del_in=3)
async def raw_do(message: Message, name, collection, chats):
if '-all' in message.flags:
async for c_l in collection.find({'on': False}):
if 'mid' not in c_l:
continue
chats.add(c_l['_id'])
await collection.update_many({}, {"$set": {'on': True}})
out = f"`All {name} Messages Enabled Successfully!`"
else:
out = f'Please set the {name} message with `.set{name.lower()}`'
if await collection.find_one_and_update(
{'_id': message.chat.id}, {"$set": {'on': True}}):
chats.add(message.chat.id)
out = f'`I will {name} new members XD`'
await message.edit(text=out, del_in=3)
async def raw_del(message: Message, name, collection, chats):
out = f"`First Set {name} Message!`"
if '-all' in message.flags:
if chats:
chats.clear()
await collection.drop()
out = f"`All {name} Messages Removed Successfully!`"
elif await collection.find_one_and_delete({'_id': message.chat.id}):
if message.chat.id in chats:
chats.remove(message.chat.id)
out = f"`{name} Removed Successfully!`"
await message.edit(text=out, del_in=3)
async def raw_view(message: Message, name, collection):
liststr = ""
if '-all' in message.flags:
await message.edit(f"`getting {name.lower()}s ...`")
async for c_l in collection.find():
if 'mid' not in c_l:
continue
liststr += f"**{(await message.client.get_chat(c_l['_id'])).title}**\n"
liststr += f"**Active:** `{c_l['on']}` , {CHANNEL.get_link(c_l['mid'])}\n\n"
else:
found = await collection.find_one({'_id': message.chat.id})
if found:
if 'mid' not in found:
return
liststr += f"**{(await message.client.get_chat(message.chat.id)).title}**\n"
liststr += f"**Active:** `{found['on']}` , {CHANNEL.get_link(found['mid'])}"
await message.edit(
text=liststr or f'`NO {name.upper()} STARTED`', del_in=0)
async def raw_say(message: Message, name, collection):
users = message.new_chat_members if name == "Welcome" else [message.left_chat_member]
for user in users:
found = await collection.find_one({'_id': message.chat.id})
if 'mid' not in found:
return
await CHANNEL.forward_stored(client=message.client,
message_id=found['mid'],
chat_id=message.chat.id,
user_id=user.id,
reply_to_message_id=message.id,
del_in=WELCOME_DELETE_TIMEOUT)