i was messing around with a Python library called 'ytchat'. It's subscribes to my Youtube livestream chat and takes commands from there. In my case it controls a PTZ networkcamera.
In general i got it all working, but now i need some sort of timer and 'active user' built in. Otherwise it would most likely escalate in a livechat mess. (chatbot reacting to all users instead of the active one)
So the relevant part is this:
def start():
print(timer_user)
if timer_user == 50:
chat_obj.start()
chat_obj.subscribe_chat_message(respond)
chat_obj.join()
else:
print('Timer still running')
# sleep(timer_interval)
start()
start()
and
def respond(msgs, chatid):
print(chatid)
global active_user_name
for msg in msgs:
print(msg)
print(msg.author.display_name)
active_user_name = msg.author.display_name
if msg.message_text.strip().startswith("!") and msg.message_text.strip() in valid_commands \
and msg.author.display_name == active_user_name:
#valid
chat_obj.send_message('Gebruiker ' + active_user_name + ' heeft controle over de camera.', chatid)
print('Countdown user start')
user_thread = threading.Thread(target=start_countdown_user)
user_thread.start()
if msg.message_text.strip() in ['!right', '!Right']:
camera.ptz_control_command(action="start", code="Right", arg1=0, arg2=4, arg3=0)
sleep(1)
camera.ptz_control_command(action="stop", code="Right", arg1=0, arg2=4, arg3=0)
print("Right")
elif msg.message_text.strip() in ['!left', '!Left']:
camera.ptz_control_command(action="start", code="Left", arg1=0, arg2=4, arg3=0)
sleep(1)
camera.ptz_control_command(action="stop", code="Left", arg1=0, arg2=4, arg3=0)
print("Left")
else:
print('Else')
True
else:
print('Invalid command, just a chat message or not active user')
The problem is that i need to check IF:
- a message starts with an exclamation mark
- the message is in the valid commands list
- and the active username is the current msg author
As far as i can tell the FOR loop inside the respond function takes the 'msgs' object and spits out each message as 'msg'. I need 'msg' to valide my IF statement.
So my main question is how do i validate these three criteria before entering a loop ? Or maybe break out/ continue (another) loop ?
Aucun commentaire:
Enregistrer un commentaire