-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfirstcomment.py
140 lines (114 loc) · 6.28 KB
/
firstcomment.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
# Sh1t-UB (telegram userbot by sh1tn3t)
# Copyright (C) 2021-2022 Sh1tN3t
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
from pyrogram import Client, types, filters, errors
from .. import loader, utils
@loader.module("FirstComment", "sh1tn3t")
class FirstCommentMod(loader.Module):
"""Первый комментарий на канале"""
async def fcsw_cmd(self, app: Client, message: types.Message):
"""Включить/выключить режим. Использование: fcsw"""
status = not self.db.get("FirstComment", "status")
self.db.set("FirstComment", "status", status)
return await utils.answer(
message, "<b>[FirstComment]</b> Теперь статус: " + (
"Включен" if status
else"Выключен"
)
)
async def fctext_cmd(self, app: Client, message: types.Message, args: str):
"""Изменить текст комментария. Использование: fctext <текст>"""
if not args:
return await utils.answer(
message, "<b>[FirstComment]</b> Текст комментария: " + self.db.get("FirstComment", "text"))
self.db.set("FirstComment", "text", args)
return await utils.answer(
message, "<b>[FirstComment]</b> Текст был изменён успешно")
async def fcadd_cmd(self, app: Client, message: types.Message, args: str):
"""Добавить канал(-ы) в базу. Использование: fcadd <@ или ID> (через пробел)"""
if not (args := args.split()):
return await utils.answer(
message, "<b>[FirstComment - Error]</b> Нет аргументов")
channels = self.db.get("FirstComment", "channels", [])
for arg in args:
try:
chat = await app.get_chat(
int(arg) if arg.isdigit() or arg.startswith("-")
else arg
)
if chat.type != "channel":
raise errors.RPCError("Это не канал")
except errors.RPCError as error:
logging.exception(error)
await message.reply(
f"<b>[FirstComment - Error]</b> \"{arg}\" не был добавлен в базу. Подробности в логах")
else:
if chat.id not in channels:
channels.append(chat.id)
self.db.set("FirstComment", "channels", channels)
return await utils.answer(
message, "<b>[FirstComment]</b> Канал(-ы) были добавлены в базу")
async def fcdel_cmd(self, app: Client, message: types.Message, args: str):
"""Удалить канал(-ы) из базы. Использование: fcdel <@ или ID> (через пробел)"""
channels = self.db.get("FirstComment", "channels", [])
if not channels:
return await utils.answer(
message, "<b>[FirstComment - Error]</b> База и так пуста")
if not (args := args.split()):
return await utils.answer(
message, "<b>[FirstComment - Error]</b> Нет аргументов")
if len(args) == 1 and args[0] == "all":
self.db.set("FirstComment", "channels", [])
return await utils.answer(
message, "<b>[FirstComment]</b> Теперь база пуста")
for arg in args:
arg = int(arg) if arg.isdigit() or arg.startswith("-") else arg
if arg not in channels:
await message.reply(
f"<b>[FirstComment - Error]</b> \"{arg}\" нет в базе")
continue
channels.remove(arg)
self.db.set("FirstComment", "channels", channels)
return await utils.answer(
message, "<b>[FirstComment]</b> Канал(-ы) был(-и) удален(-ы) из базы")
async def fcs_cmd(self, app: Client, message: types.Message):
"""Вывести список каналов в базе. Использование: fcs"""
if not (channels := self.db.get("FirstComment", "channels", [])):
return await utils.answer(
message, "<b>[FirstComment - Error]</b> База пуста")
text = "<b>[FirstComment]</b> Каналы в базе:\n"
for channel in channels.copy():
try:
chat = await app.get_chat(channel)
except errors.RPCError as error:
channels.remove(channel)
self.db.set("FirstComment", "channels", channels)
logging.exception(error)
await message.reply(
f"<b>[FirstComment - Error]</b> \"{channel}\" не найден. Подробности в логах")
else:
text += f"\n• {chat.title} | <code>{chat.id}</code>"
return await utils.answer(
message, text)
@loader.on(filters.channel)
async def watcher(self, app: Client, message: types.Message):
if (
not self.db.get("FirstComment", "status")
or (chat := message.chat).id not in self.db.get("FirstComment", "channels", [])
):
return
try:
msg = await app.get_discussion_message(chat.id, message.message_id)
await msg.reply(self.db.get("FirstComment", "text", "Я первый!"))
except errors.RPCError as error:
logging.exception(error)