-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdonators.py
444 lines (414 loc) · 15.1 KB
/
donators.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
__version__ = (0, 0, 31)
# ▄▀█ █▄ █ █▀█ █▄ █ █▀█ ▀▀█ █▀█ █ █ █▀
# █▀█ █ ▀█ █▄█ █ ▀█ ▀▀█ █ ▀▀█ ▀▀█ ▄█
#
# © Copyright 2024
#
# developed by @anon97945
#
# https://t.me/apodiktum_modules
# https://github.com/anon97945
#
# 🔒 Licensed under the GNU GPLv3
# 🌐 https://www.gnu.org/licenses/gpl-3.0.html
# meta developer: @apodiktum_modules
# meta banner: https://t.me/apodiktum_dumpster/11
# meta pic: https://t.me/apodiktum_dumpster/13
# scope: hikka_only
# scope: hikka_min 1.3.3
import logging
from datetime import date, timedelta
from telethon.tl.types import Message
from .. import loader, utils
logger = logging.getLogger(__name__)
@loader.tds
class ApodiktumDonatorsMod(loader.Module):
"""
Handle donations in a given channel and kick them after the period of time.
"""
strings = {
"name": "Apo-Donators",
"developer": "@anon97945",
"_cfg_cst_auto_migrate": "Wheather to auto migrate defined changes on startup.",
"_cfg_cst_channel": "The Channel ID where the donations should be saved.",
"_cfg_cst_custom_message": (
"The message send to the user after the subscription is added. Use"
" <br> for new line."
),
"_cfg_cst_kickchannel": (
"The channel ids to kick the user from after the subscription."
),
"_cfg_cst_monthlyamount": "The monthly cost of the subscription.",
"_cfg_cst_subscription_gift": (
"The gift to send to the user after the subscription. Will be"
" attached to custom_message. Use <br> for new line."
),
"_cfg_doc_log_kick": "Logs successful kicks from the chats.",
"_log_doc_kicked": "Kicked {} from {}.",
"amount": "Amount",
"code": "Code",
"date": "Date",
"donation_saved": "🎉 Donation saved!",
"dtype": "DonationType",
"no_amount": "No amount found.",
"no_args": "No args.",
"no_channel": "No logchannel set.",
"no_reply": "You didn't reply to a message.",
"rank": "Rank",
"total_amount": "<b><u>Total amount of donations:</u></b>\n{}",
"uname": "Name",
"userid": "UserID",
"username": "Username",
}
strings_en = {}
strings_de = {
"_cfg_cst_channel": "Die Kanal-ID, wo die Spenden gespeichert werden sollen.",
"_cfg_cst_custom_message": (
"Die Nachricht, die an den Benutzer gesendet wird, nachdem das"
" Abonnement hinzugefügt wurde. Benutze <br> für einen"
" Zeilenumbruch."
),
"_cfg_cst_kickchannel": (
"Die Kanal-IDs, aus denen der Benutzer nach dem Abonnement gekickt"
" werden soll."
),
"_cfg_cst_monthlyamount": "Die monatlichen Kosten des Abonnements.",
"_cfg_cst_subscription_gift": (
"Das Geschenk, das an den Benutzer gesendet wird, nachdem das"
" Abonnement hinzugefügt wurde. Wird an custom_message angehängt."
" Benutze <br> für einen Zeilenumbruch."
),
"_log_doc_kicked": "{} von {} gekickt.",
"_log_doc_log_kicks": "Protokolliert die erfolgreichen Kicks aus den Chats.",
"amount": "Betrag",
"code": "Code",
"date": "Datum",
"donation_saved": "🎉 Spende gespeichert!",
"dtype": "Spendentyp",
"no_amount": "Kein Betrag gefunden.",
"no_args": "Keine Argumente angegeben",
"no_channel": "Kein Protokollkanal gesetzt.",
"no_reply": "Du hast nicht auf eine Nachricht geantwortet.",
"rank": "Rang",
"total_amount": "<b><u>Gesamtbetrag der Spenden:</u></b>\n{}",
"uname": "Name",
"userid": "BenutzerID",
"username": "Benutzername",
}
strings_ru = {}
all_strings = {
"strings": strings,
"strings_en": strings,
"strings_de": strings_de,
"strings_ru": strings_ru,
}
changes = {
"migration1": {
"name": {
"old": "Apo Donators",
"new": "Apo-Donators",
},
},
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"logchannel",
"None",
lambda: self.strings("_cfg_cst_logchannel"),
validator=loader.validators.Union(
loader.validators.TelegramID(),
loader.validators.NoneType(),
),
),
loader.ConfigValue(
"custom_message",
["Thank you very much for your donation! 🎉"],
doc=lambda: self.strings("_cfg_cst_custom_message"),
validator=loader.validators.Series(
validator=loader.validators.String()
),
),
loader.ConfigValue(
"kick_channel",
doc=lambda: self.strings("_cfg_cst_kickchannel"),
validator=loader.validators.Series(
validator=loader.validators.TelegramID()
),
),
loader.ConfigValue(
"log_kicks",
True,
doc=lambda: self.strings("_cfg_doc_log_kick"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"monthly_amount",
10,
doc=lambda: self.strings("_cfg_cst_monthlyamount"),
validator=loader.validators.Integer(minimum=1),
),
loader.ConfigValue(
"subscription_gift",
doc=lambda: self.strings("_cfg_cst_subscription_gift"),
validator=loader.validators.Hidden(
validator=loader.validators.Series(
validator=loader.validators.String()
),
),
),
loader.ConfigValue(
"auto_migrate",
True,
doc=lambda: self.strings("_cfg_cst_auto_migrate"),
validator=loader.validators.Boolean(),
), # for MigratorClass
)
async def client_ready(self):
self.apo_lib = await self.import_lib(
"https://raw.githubusercontent.com/anon97945/hikka-libs/master/apodiktum_library.py",
suspend_on_error=True,
)
await self.apo_lib.migrator.auto_migrate_handler(
self.__class__.__name__,
self.strings("name"),
self.changes,
self.config["auto_migrate"],
)
async def cdonatorscmd(self, message: Message):
"""
This will open the config for the module.
"""
name = self.strings("name")
await self.allmodules.commands["config"](
await utils.answer(message, f"{self.get_prefix()}config {name}")
)
async def donamountcmd(self, message: Message):
"""
Calculate the amount of donations.
"""
if not self.config["logchannel"]:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_channel", self.all_strings, message),
)
return
amounts = await self._get_amounts(message, self.config["logchannel"])
if amounts:
await utils.answer(
message,
self.apo_lib.utils.get_str(
"total_amount", self.all_strings, message
).format(amounts),
)
else:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_amount", self.all_strings, message),
)
async def donsavecmd(self, message: Message):
"""
Save donation. Reply to the user message!
Pattern:
.donsave <amount> <currency> <dtype> <rank> <code> | as reply!
Example:
.donsave 100 € amazon vip 123-123-123-123, 456-456-456-456 | as reply!
"""
reply = await message.get_reply_message()
if not self.config["logchannel"]:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_channel", self.all_strings, message),
)
return
if not reply:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_reply", self.all_strings, message),
)
return
user = await reply.get_sender()
if not user:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_user", self.all_strings, message),
)
return
args = utils.get_args_raw(message).lower()
args = str(args).split()
if not args:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_args", self.all_strings, message),
)
return
(
monthly_amount,
today,
uname,
username,
userid,
amount,
currency,
dtype,
rank,
code,
) = self._vars(user, args)
string_join, string_kick = self._jk_strings(
today, uname, username, userid, amount, currency, dtype, rank, code
)
msg = await message.client.send_message(
int(self.config["logchannel"]),
string_join,
)
await message.client.send_message(
int(self.config["logchannel"]),
string_kick,
schedule=(
date.today() + timedelta(days=int(amount) / monthly_amount * 30)
),
)
if self.config["custom_message"]:
custom_msg = " ".join(self.config["custom_message"])
if self.config["subscription_gift"]:
custom_msg += " ".join(self.config["subscription_gift"])
custom_msg = custom_msg.replace("<br>", "\n")
await utils.answer(message, custom_msg)
else:
await utils.answer(
message,
self.apo_lib.utils.get_str("donation_saved", self.all_strings, message),
)
await msg.react("👍")
async def _get_amounts(self, message: Message, logchannel: int):
amounts = ""
amounts_euro = []
amounts_usd = []
amounts_gbp = []
amounts_rub = []
itermsg = message.client.iter_messages(entity=logchannel, limit=None)
async for msg in itermsg:
if (
msg
and isinstance(msg, Message)
and "#join" in self.apo_lib.utils.raw_text(msg).lower()
):
msg_lines = self.apo_lib.utils.raw_text(msg).splitlines()
for lines in msg_lines:
if (
"€" in lines.lower()
or "$" in lines.lower()
or "£" in lines.lower()
or "₽" in lines.lower()
):
for z in lines.split():
if "€" in z:
z = z.replace("€", "")
if z.isdigit():
amounts_euro.append(int(z))
if "$" in z:
z = z.replace("$", "")
if z.isdigit():
amounts_usd.append(int(z))
if "£" in z:
z = z.replace("£", "")
if z.isdigit():
amounts_gbp.append(int(z))
if "₽" in z:
z = z.replace("₽", "")
if z.isdigit():
amounts_rub.append(int(z))
if amounts_euro:
amounts += f"<code>{sum(amounts_euro)}€</code>\n"
if amounts_usd:
amounts += f"<code>{sum(amounts_usd)}$</code>\n"
if amounts_gbp:
amounts += f"<code>{sum(amounts_gbp)}£</code>\n"
if amounts_rub:
amounts += f"<code>{sum(amounts_rub)}₽</code>\n"
return amounts
def _vars(self, user, args):
monthly_amount = self.config["monthly_amount"]
today = date.today()
uname = user.first_name
if user.last_name:
uname += f" {user.last_name}"
username = f"@{user.username}" if user.username else ""
userid = user.id
amount = args[0]
currency = args[1]
dtype = args[2].capitalize()
rank = args[3].upper()
code = str(args[4:]).upper()
return (
monthly_amount,
today,
uname,
username,
userid,
amount,
currency,
dtype,
rank,
code,
)
def _jk_strings(
self,
today,
uname,
username,
userid,
amount,
currency,
dtype,
rank,
code,
):
string_join = (
"#Join\n"
+ f"#{self.strings('date')} {today}\n"
+ f"#{self.strings('uname')} {uname}\n"
+ f"#{self.strings('username')} {username}\n"
+ f"#ID_{userid}\n"
+ f"#{self.strings('dtype')} {dtype}\n"
+ f"#{self.strings('amount')} {amount}{currency}\n"
+ f"#{self.strings('rank')} {rank}\n"
+ f"#{self.strings('code')} {code}\n"
)
string_kick = (
"#Kick\n"
+ f"#{self.strings('date')} {today}\n"
+ f"#{self.strings('uname')} {uname}\n"
+ f"#{self.strings('username')} {username}\n"
+ f"#ID_{userid}\n"
+ f"#{self.strings('dtype')} {dtype}\n"
+ f"#{self.strings('amount')} {amount}{currency}\n"
+ f"#{self.strings('rank')} {rank}\n"
+ f"#{self.strings('code')} {code}\n"
)
return string_join, string_kick
@loader.watcher("only_messages")
async def watcher(self, message: Message):
if (
not self.config["logchannel"]
or utils.get_chat_id(message) != self.config["logchannel"]
or "#kick" not in self.apo_lib.utils.raw_text(message).lower()
):
return
msg_lines = self.apo_lib.utils.raw_text(message).splitlines()
for text in msg_lines:
if "#ID_" in text:
userid = int(text.replace("#ID_", ""))
kchannels = self.config["kick_channel"]
for kchannel in kchannels:
if await self.apo_lib.utils.is_member(kchannel, userid):
await message.client.kick_participant(
kchannel,
userid,
)
self.apo_lib.utils.log(
logging.INFO,
__name__,
self.strings("_log_doc_kicked").format(userid, kchannel),
)
await message.react("👍")