I'm currently creating a bot for twitch in python.
If a user types !start
I want the output Tracking time
, and if the SAME USER again types start
I want the output Already tracking time
.
I have tried this:
people = []
if "!start" in message:
sendMessage(s, "Now recording your time in chat.")
print(user.title() + " is now tracking time.")
people.append(user)
print(", ".join(people))
if user in people and "start" in message:
sendMessage(s, "Worked")
The current output I am getting when I type "!start" in chat is: Tracking time.
~new line~ Already tracking time.
Your issue is that you don't check if user
is already being tracked until the end of your case, after already having sent "Now recording your time in chat". You need to perform that check earlier. Something along these lines might work for you:
people = []
if "!start" in message:
if user in people:
sendMessage(s, "Already tracking time")
else:
sendMessage(s, "Now recording your time in chat.")
print(user.title() + " is now tracking time.")
people.append(user)
print(", ".join(people))
Having developed bots in Python a while back (with poor coding practices), I'm guessing that this if
block is one of many in a large handle_message
function. If that's the case, you most likely want to move people = []
out of that function so it doesn't get reinitialised on each received message.
To demonstrate this solution using a mock implementation of sendMessage
:
def sendMessage(message):
print('Bot responds: {}'.format(message))
people = []
def handle_message(user, message):
print('{} says: {}'.format(user, message))
if "!start" in message:
if user in people:
sendMessage("Already tracking time")
else:
sendMessage("Now recording your time in chat.")
print(user.title() + " is now tracking time.")
people.append(user)
print(", ".join(people))
if __name__ == '__main__':
for _ in range(2):
handle_message("John", "!start")
Output
John says: !start
Bot responds: Now recording your time in chat.
John is now tracking time.
John
John says: !start
Bot responds: Already tracking time