Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return
input_str = event.pattern_match.group(1)
if input_str:
logger.info("TODO: Not yet Implemented")
else:
if event.is_private:
return False
await event.edit("Searching Participant Lists.")
p = 0
async for i in borg.iter_participants(event.chat_id, filter=ChannelParticipantsKicked, aggressive=True):
rights = ChatBannedRights(
until_date=0,
view_messages=False
)
try:
await borg(functions.channels.EditBannedRequest(event.chat_id, i, rights))
except FloodWaitError as ex:
logger.warn("sleeping for {} seconds".format(ex.seconds))
sleep(ex.seconds)
except Exception as ex:
await event.edit(str(ex))
else:
p += 1
await event.edit("{}: {} unbanned".format(event.chat_id, p))
async def is_admin(chat_id, sender_id) -> bool:
"""Check if the sender is an admin or bot"""
try:
result = await client(functions.channels.GetParticipantRequest(
channel=chat_id,
user_id=sender_id
))
if isinstance(
result.participant,
(types.ChannelParticipantAdmin, types.ChannelParticipantCreator)
):
return True
except Exception:
return False
async def get_pinned_message(self):
"""
If ``new_pin`` is `True`, this returns the `Message
` object that was pinned.
"""
if self._pinned_message == 0:
return None
if isinstance(self._pinned_message, int) \
and await self.get_input_chat():
r = await self._client(functions.channels.GetMessagesRequest(
self._input_chat, [self._pinned_message]
))
try:
self._pinned_message = next(
x for x in r.messages
if isinstance(x, types.Message)
and x.id == self._pinned_message
)
except StopIteration:
pass
if isinstance(self._pinned_message, types.Message):
return self._pinned_message
creator = await get_chat_link(creator)
except (TypeError, ValueError):
creator = f"`{creatorid}`"
text = f"**Link:** {link}"
text += f"\n**Link creator:** {creator}\n**ID:** `{cid}`"
try:
chat = await client.get_entity(cid)
except (TypeError, ValueError):
break
except Exception as e:
text += f"\n```{await client.get_traceback(e)}```"
break
if isinstance(chat, types.Channel):
result = await client(
functions.channels.GetFullChannelRequest(
channel=chat
)
)
text += await misc.resolve_channel(event.client, result)
elif isinstance(chat, types.Chat):
result = await client(
functions.messages.GetFullChatRequest(
chat_id=chat
)
)
text += await misc.resolve_chat(event.client, result)
break
else:
try:
chat = await client.get_entity(valid)
except (TypeError, ValueError):
users = await self(functions.users.GetUsersRequest([
types.InputUser(peer.user_id, access_hash=0)]))
if users and not isinstance(users[0], types.UserEmpty):
# If the user passed a valid ID they expect to work for
# channels but would be valid for users, we get UserEmpty.
# Avoid returning the invalid empty input peer for that.
#
# We *could* try to guess if it's a channel first, and if
# it's not, work as a chat and try to validate it through
# another request, but that becomes too much work.
return utils.get_input_peer(users[0])
elif isinstance(peer, types.PeerChat):
return types.InputPeerChat(peer.chat_id)
elif isinstance(peer, types.PeerChannel):
try:
channels = await self(functions.channels.GetChannelsRequest([
types.InputChannel(peer.channel_id, access_hash=0)]))
return utils.get_input_peer(channels.chats[0])
except errors.ChannelInvalidError:
pass
raise ValueError(
'Could not find the input entity for {!r}. Please read https://'
'docs.telethon.dev/en/latest/concepts/entities.html to'
' find out more details.'
.format(peer)
)
def channel_get_participants(self, channel):
tg_users = []
offset = 0
while True:
participants = self.proc(tl.functions.channels.GetParticipantsRequest(
channel.tg_room,
tl.types.ChannelParticipantsSearch(''),
offset,
100,
0, # hash
))
if not participants.users: break
tg_users.extend(participants.users)
offset += len(participants.users)
channel.update_members(tg_users)
await client.kick_participant(chat, user)
# Leaving chat
await client.kick_participant(chat, 'me')
"""
entity = await self.get_input_entity(entity)
user = await self.get_input_entity(user)
if helpers._entity_type(user) != helpers._EntityType.USER:
raise ValueError('You must pass a user entity')
ty = helpers._entity_type(entity)
if ty == helpers._EntityType.CHAT:
await self(functions.messages.DeleteChatUserRequest(entity.chat_id, user))
elif ty == helpers._EntityType.CHANNEL:
if isinstance(user, types.InputPeerSelf):
await self(functions.channels.LeaveChannelRequest(entity))
else:
await self(functions.channels.EditBannedRequest(
channel=entity,
user_id=user,
banned_rights=types.ChatBannedRights(until_date=None, view_messages=True)
))
await asyncio.sleep(0.5)
await self(functions.channels.EditBannedRequest(
channel=entity,
user_id=user,
banned_rights=types.ChatBannedRights(until_date=None)
))
else:
raise ValueError('You must pass either a channel or a chat')
async def promote_bot(self, chat: tg.types.InputPeerChannel, username: str) -> None:
rights = tg.tl.types.ChatAdminRights(
delete_messages=True, ban_users=True, invite_users=True, pin_messages=True
)
request = tg.tl.functions.channels.EditAdminRequest(
chat, username, rights, "bot"
)
await self.bot.client(request)
users = ""
chats = ""
channels = ""
failed = []
for user in entities:
try:
input_entity = await client.get_input_entity(user)
if isinstance(input_entity, types.InputPeerChat):
full_chat = await client(
functions.messages.GetFullChatRequest(input_entity)
)
string = await Parser.parse_full_chat(full_chat, event)
chats += f"\n{chats}\n"
elif isinstance(input_entity, types.InputPeerChannel):
full_channel = await client(
functions.channels.GetFullChannelRequest(input_entity)
)
string = await Parser.parse_full_chat(full_channel, event)
channels += f"\n{string}\n"
else:
full_user = await client(
functions.users.GetFullUserRequest(input_entity)
)
string = await Parser.parse_full_user(full_user, event)
users += f"\n{string}\n"
except Exception as e:
LOGGER.debug(e)
failed.append(user)
if users:
await event.answer("**USERS**" + users, reply=True)
if chats:
# Private channels should just contain the bot and the corresponding user.
if await channel.is_private():
if channel.source == str(self._bot_user["id"]):
return [TelegramUser.from_bot_user(self, self._bot_user)]
elif int(channel.source) > 0:
entity = self._client.session.get_entity(channel.source)
if entity:
return [TelegramUser.from_bot_user(self, self._bot_user),
await self.user_from_id(channel.source)]
# Channel and supergroup chat IDs have a bot-API-only prefix to distinguish them.
if channel.source.startswith("-100"):
chat = int(channel.source[4:])
users = []
try:
while True:
data = await self._client(tl.functions.channels.GetParticipantsRequest(
chat, tl.types.ChannelParticipantsRecent(), len(users), 1000, 0))
if data.users:
users += [TelegramUser.from_proto_user(self, user) for user in data.users]
else:
break
except BadRequestError:
return None
else:
return users
else:
chat = abs(int(channel.source))
try:
data = await self._client(tl.functions.messages.GetFullChatRequest(chat))
except BadRequestError:
return None
else: