I am coding a discord bot with python, I am running into difficulties with the next feature. When the message: ('user' joined) is read the bot should start looping the function every x seconds until the message: ('user' left) is read.
I have been testing different code in scratch files. It succesfully starts looping at 'user' joined. But when the ('user' left) message should come through it won't take the new argument and continues the loop forever.
These two versions look most promising:
import re
import time
def message(servermessage):
a = re.sub("[^\S]", " ", servermessage).split()
if a[1] == 'joined':
online = False
elif a[1] == 'left':
online = True
while True:
mcusername = a[0]
print(mcusername, 0)
if online:
break
time.sleep(2)
message('user joined')
time.sleep(10)
message('user left')
and
import re
import sched, time
s = sched.scheduler(time.time, time.sleep)
def message(servermessage):
a = re.sub("[^\S]", " ", servermessage).split()
def rewardplayer():
s.enter(1, 1, rewardplayer)
mcusername = a[0]
print(mcusername, 0)
if a[1] == 'joined':
s.enter(1, 1, rewardplayer)
s.run()
elif a[1] == 'left':
s.cancel()
message('user joined')
time.sleep(10)
print('done')
message('user left')
Another requirement that I have is that it should be able to run the same loop for a different user when a ('newuser' joined) message is given before the ('previoususer' left) message from a previous user.
I apologise for the vague explanation. But I hope you can help. Thanks in advance!