Answer the question
In order to leave comments, you need to log in
VK python bot not responding. how to solve?
Wrote vk query bot. the bot itself started, but does not respond to commands. no errors appear in the console. how to solve?
API 5.80
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
import yaml
import requests
import json
token = "мой токен"
group_id = айди группы
vk_session = vk_api.VkApi(token=token)
longpoll = VkBotLongPoll(vk_session, group_id)
vk = vk_session.get_api()
for event in longpoll.listen():
try:
if event.type == VkBotEventType.MESSAGE_NEW:
message = event.obj.message
peer_id = message["peer_id"]
text = message["text"].lower()
args = text.split()
cmd = args.pop(0)
if cmd in ["/mcpe", "/query", "/сервер"]:
try:
args[0]
except:
vk.messages.send(peer_id = peer_id, random_id = 0, message = f"Введите {cmd} <айпи> <порт>")
continue
try:
port = args[1]
except:
port = 19132
request = json.loads(requests.get(f"https://api.mcsrvstat.us/2/{args[0]}:{port}").text)
if not request['online']:
vk.messages.send(peer_id = peer_id, random_id = 0, message = "Сервер выключен либо отключён показ данных сервера")
else:
message = f" IP сервера: {request['ip']}:{request['port']}\n"
try: message += f" MOTD: {request['motd']['clean'][0]}\n"
except: message += " MOTD: скрыто\n"
try: message += f" Версия: {request['version']}\n"
except: message += " Версия: скрыто"
try: message += f"⚄1¤7 Ядро: {request['sowtware']}\n"
except: message += "⚄1¤7 Ядро: скрыто\n"
try: message += f" Карта: {request['map']}\n"
except: message += " Карта: скрыто\n"
try: message += f" Онлайн: {request['players']['online']}/{request['players']['max']}\n"
except: message += " Онлайн: скрыто\n"
try: message += f" Игроки: {', '.join(request['players']['list'])}\n\n\n"
except: message += " Игроки: скрыто\n\n\n"
try: message += f" Плагины: {', '.join(request['plugins']['names'])}\n"
except: message += " Плагины: отключено"
vk.messages.send(peer_id = peer_id, random_id = 0, message = message)
if cmd == "/help":
vk.messages.send(peer_id = peer_id, random_id = 0, message = f"Помощь по Query\n/query, /сервер, /mcpe")
except: pass
Answer the question
In order to leave comments, you need to log in
As far as I know, API version 5.80 does not yet exist
event.obj.message["text"]
, the object message
seems to have appeared in later versions. Print it event.object
and see what it returns.
You have too much logic inside the loop. Try to debug your program. Earlier they wrote about the message object, it appeared starting from version 5.80. Also, if you are writing a community bot, I recommend taking a look at the vk_maria library ( https://github.com/lxstvayne/vk_maria ) written by me. With it, you can make your code much cleaner and more understandable.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question