This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
/
admin.py
237 lines (192 loc) · 8.59 KB
/
admin.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
import html
from typing import Optional, List
from telegram import Message, Chat, Update, Bot, User
from telegram import ParseMode
from telegram.error import BadRequest
from telegram.ext import CommandHandler, Filters
from telegram.ext.dispatcher import run_async
from telegram.utils.helpers import escape_markdown, mention_html
from tg_bot import dispatcher
from tg_bot.modules.disable import DisableAbleCommandHandler
from tg_bot.modules.helper_funcs.chat_status import bot_admin, can_promote, user_admin, can_pin
from tg_bot.modules.helper_funcs.extraction import extract_user
from tg_bot.modules.log_channel import loggable
@run_async
@bot_admin
@can_promote
@user_admin
@loggable
def promote(bot: Bot, update: Update, args: List[str]) -> str:
chat_id = update.effective_chat.id
message = update.effective_message # type: Optional[Message]
chat = update.effective_chat # type: Optional[Chat]
user = update.effective_user # type: Optional[User]
user_id = extract_user(message, args)
if not user_id:
message.reply_text("You don't seem to be referring to a user.")
return ""
user_member = chat.get_member(user_id)
if user_member.status == 'administrator' or user_member.status == 'creator':
message.reply_text("How am I meant to promote someone that's already an admin?")
return ""
if user_id == bot.id:
message.reply_text("I can't promote myself! Get an admin to do it for me.")
return ""
# set same perms as bot - bot can't assign higher perms than itself!
bot_member = chat.get_member(bot.id)
bot.promoteChatMember(chat_id, user_id,
can_change_info=bot_member.can_change_info,
can_post_messages=bot_member.can_post_messages,
can_edit_messages=bot_member.can_edit_messages,
can_delete_messages=bot_member.can_delete_messages,
# can_invite_users=bot_member.can_invite_users,
can_restrict_members=bot_member.can_restrict_members,
can_pin_messages=bot_member.can_pin_messages,
can_promote_members=bot_member.can_promote_members)
message.reply_text("Successfully promoted!")
return "<b>{}:</b>" \
"\n#PROMOTED" \
"\n<b>Admin:</b> {}" \
"\n<b>User:</b> {}".format(html.escape(chat.title),
mention_html(user.id, user.first_name),
mention_html(user_member.user.id, user_member.user.first_name))
@run_async
@bot_admin
@can_promote
@user_admin
@loggable
def demote(bot: Bot, update: Update, args: List[str]) -> str:
chat = update.effective_chat # type: Optional[Chat]
message = update.effective_message # type: Optional[Message]
user = update.effective_user # type: Optional[User]
user_id = extract_user(message, args)
if not user_id:
message.reply_text("You don't seem to be referring to a user.")
return ""
user_member = chat.get_member(user_id)
if user_member.status == 'creator':
message.reply_text("This person CREATED the chat, how would I demote them?")
return ""
if not user_member.status == 'administrator':
message.reply_text("Can't demote what wasn't promoted!")
return ""
if user_id == bot.id:
message.reply_text("I can't demote myself! Get an admin to do it for me.")
return ""
try:
bot.promoteChatMember(int(chat.id), int(user_id),
can_change_info=False,
can_post_messages=False,
can_edit_messages=False,
can_delete_messages=False,
can_invite_users=False,
can_restrict_members=False,
can_pin_messages=False,
can_promote_members=False)
message.reply_text("Successfully demoted!")
return "<b>{}:</b>" \
"\n#DEMOTED" \
"\n<b>Admin:</b> {}" \
"\n<b>User:</b> {}".format(html.escape(chat.title),
mention_html(user.id, user.first_name),
mention_html(user_member.user.id, user_member.user.first_name))
except BadRequest:
message.reply_text("Could not demote. I might not be admin, or the admin status was appointed by another "
"user, so I can't act upon them!")
return ""
@run_async
@bot_admin
@can_pin
@user_admin
@loggable
def pin(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]
chat = update.effective_chat # type: Optional[Chat]
is_group = chat.type != "private" and chat.type != "channel"
prev_message = update.effective_message.reply_to_message
is_silent = True
if len(args) >= 1:
is_silent = not (args[0].lower() == 'notify' or args[0].lower() == 'loud' or args[0].lower() == 'violent')
if prev_message and is_group:
try:
bot.pinChatMessage(chat.id, prev_message.message_id, disable_notification=is_silent)
except BadRequest as excp:
if excp.message == "Chat_not_modified":
pass
else:
raise
return "<b>{}:</b>" \
"\n#PINNED" \
"\n<b>Admin:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name))
return ""
@run_async
@bot_admin
@can_pin
@user_admin
@loggable
def unpin(bot: Bot, update: Update) -> str:
chat = update.effective_chat
user = update.effective_user # type: Optional[User]
try:
bot.unpinChatMessage(chat.id)
except BadRequest as excp:
if excp.message == "Chat_not_modified":
pass
else:
raise
return "<b>{}:</b>" \
"\n#UNPINNED" \
"\n<b>Admin:</b> {}".format(html.escape(chat.title),
mention_html(user.id, user.first_name))
@run_async
@bot_admin
@user_admin
def invite(bot: Bot, update: Update):
chat = update.effective_chat # type: Optional[Chat]
if chat.username:
update.effective_message.reply_text(chat.username)
elif chat.type == chat.SUPERGROUP or chat.type == chat.CHANNEL:
bot_member = chat.get_member(bot.id)
if bot_member.can_invite_users:
invitelink = bot.exportChatInviteLink(chat.id)
update.effective_message.reply_text(invitelink)
else:
update.effective_message.reply_text("I don't have access to the invite link, try changing my permissions!")
else:
update.effective_message.reply_text("I can only give you invite links for supergroups and channels, sorry!")
@run_async
def adminlist(bot: Bot, update: Update):
administrators = update.effective_chat.get_administrators()
text = "Admins in *{}*:".format(update.effective_chat.title or "this chat")
for admin in administrators:
user = admin.user
name = "[{}](tg://user?id={})".format(user.first_name + (user.last_name or ""), user.id)
if user.username:
name = escape_markdown("@" + user.username)
text += "\n - {}".format(name)
update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
def __chat_settings__(chat_id, user_id):
return "You are *admin*: `{}`".format(
dispatcher.bot.get_chat_member(chat_id, user_id).status in ("administrator", "creator"))
__help__ = """
- /adminlist: list of admins in the chat
*Admin only:*
- /pin: silently pins the message replied to - add 'loud' or 'notify' to give notifs to users.
- /unpin: unpins the currently pinned message
- /invitelink: gets invitelink
- /promote: promotes the user replied to
- /demote: demotes the user replied to
"""
__mod_name__ = "Admin"
PIN_HANDLER = CommandHandler("pin", pin, pass_args=True, filters=Filters.group)
UNPIN_HANDLER = CommandHandler("unpin", unpin, filters=Filters.group)
INVITE_HANDLER = CommandHandler("invitelink", invite, filters=Filters.group)
PROMOTE_HANDLER = CommandHandler("promote", promote, pass_args=True, filters=Filters.group)
DEMOTE_HANDLER = CommandHandler("demote", demote, pass_args=True, filters=Filters.group)
ADMINLIST_HANDLER = DisableAbleCommandHandler("adminlist", adminlist, filters=Filters.group)
dispatcher.add_handler(PIN_HANDLER)
dispatcher.add_handler(UNPIN_HANDLER)
dispatcher.add_handler(INVITE_HANDLER)
dispatcher.add_handler(PROMOTE_HANDLER)
dispatcher.add_handler(DEMOTE_HANDLER)
dispatcher.add_handler(ADMINLIST_HANDLER)