Ошибка при выводе информации об админов беседы при помощи vk_api
newBOT.py:
import vk_api
import utils
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from models import User
from devolper import *
import time
class MyLongPoll(VkBotLongPoll):
def listen(self):
while True:
try:
for event in self.check():
yield event
except Exception as e:
print(e)
class VkBot:
def __init__(self):
self.vk = vk_api.VkApi(
token = 'token')
self.vk_session = vk_api.VkApi(
token='token')
self.vk = self.vk_session.get_api()
self.longpoll = MyLongPoll(self.vk_session, 214751545)
def run(self):
for event in self.longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
msg = event.object.message
user_id = msg['from_id']
user = utils.get_user_by_id(user_id)
user.role = 0
user.save()
text = msg['text']
fwd = self.vk_session.method('messages.getByConversationMessageId', {
'conversation_message_ids': msg['conversation_message_id'],
'peer_id': msg['peer_id']
})['items'][0]
if 'reply_message' in fwd:
fwd = fwd['reply_message']
else:
fwd = None
if user.vk_id == devolper:
if fwd:
...
elif text == 'админы':
user_id = msg['from_id']
user_name = self.vk_session.method('users.get', {'user_id': user_id})[0]['first_name']
admins = self.vk.messages.getConversationMembers(cmids=msg['conversation_message_id'], peer_id=msg['peer_id'])
self.vk_session.method('messages.send', {
'chat_id': msg['peer_id'] - 2000000000,
'message': f'Хорошо, {user_name}!'
f'\nВот список администраторов беседы:'
f'\n \n',
'random_id': 0
})
for admin in admins['items']:
admin_name = self.vk_session.method('users.get', {'user_id': admin['member_id']})[0]['first_name']
self.vk_session.method('messages.send', {
'chat_id': msg['peer_id'] - 2000000000,
'message': f'{admin_name} - @id{admin["member_id"]}',
'random_id': 0
})
...
if __name__ == '__main__':
VkBot().run()
developer.py:
developer = мой айди
utils.py
from models import User
def get_user_by_id(user_id):
try:
return User().get(vk_id=user_id)
except:
User(
vk_id=user_id,
warns=0,
role=0
).save()
return User().get(vk_id=user_id)
сама ошибка:
C:\Users\matyt\Downloads\VkBot\venv\Scripts\python.exe C:\Users\matyt\Downloads\VkBot\newBOT.py
Traceback (most recent call last):
File "C:\Users\matyt\Downloads\VkBot\newBOT.py", line 180, in <module>
VkBot().run()
File "C:\Users\matyt\Downloads\VkBot\newBOT.py", line 138, in run
admin_name = self.vk_session.method('users.get', {'user_id': admin['member_id']})[0]['first_name']
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
Process finished with exit code 1