Answer the question
In order to leave comments, you need to log in
How not to execute a command if a person's ID is not found in the user's text?
Hello, there was a problem when creating a bot for a VK conversation.
When the conversation admin enters the "kick" command without mentioning the person, the script will crash. There is also a problem when the same kick command is entered, but with the mention of an ID that is not in VK at all, or which is in VK, but this user is not in the conversation.
Here is the code itself (it already has a distribution of a person into groups according to the type of admin, etc.):
if msg[0:3] == "кик":
get_user = re.search("\d{1,}", msg)
user_id = event.obj["from_id"]
if user_id in admin:
vk.method("messages.send", {"peer_id": event.object.peer_id, "message": " | @id" + str(get_user[0]) + "(Пользователь) был исключен из беседы.\nДля возврата напишите @id" + str(user_id) + "(администратору).", "random_id": 0})
vk.method("messages.removeChatUser", {"chat_id": event.chat_id, "user_id": int(get_user[0])})
Answer the question
In order to leave comments, you need to log in
To begin with, it would be nice to add a check if there is an ID in the message
get_user = re.search("\d{1,}", msg)
# если re.search ничего не найдет, она вернет None
if not get_user:
return
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question