0

I'm new to python and coding in general here the code:

def job():
try:
    users = sub_client.get_all_users(size=1)
except json.decoder.JSONDecodeError:
    pass
for uid in users.profile.id:
    with open("uid.txt", "r") as file:
        uids = file.read().split("\n")[:-1]
        if uid in uids:
            print("already done")
        else:
            print("done")
            sub_client.comment(userId=uid, message="welcome")
            print(uid)
            with open("uid.txt", "a") as f:
                f.write(f"{uid}\n")
                f.close()

while True:
    readChat = ['4044f0f7-7963-46ad-b453-bb7d417be19a']
    for chat in readChat:
        messageList = sub_client.get_chat_messages(size=1, chatId=chat)
        chatInfo = sub_client.get_chat_thread(chatId=chat)
        for nickname, content, id in zip(messageList.author.nickname, messageList.content, messageList.messageId):
            print(nickname, content)
            content = str(content).split(" ")
            if content[0][0] == "!":
                if content[0][1:].lower() == "start":
                    sub_client.send_message(message=" | ok! bot activé",chatId=chatInfo.chatId)
                    asyncio.create_task(job())
                if content[0][1:].lower() == "stop":
                    sub_client.send_message(message=" | ok! bot désactivé", chatId=chatInfo.chatId)
                    break

Would like that the first "job" run all the time when someone write "!start" into a chat, and that the task stop when someone write "!stop" (but not completly stopped, for example if someone write "!start" again into the chat, it restart the task, etc...)

Is this even possible? The script will run 24/24, 7/7

Mehdi Zare
  • 1,221
  • 1
  • 16
  • 32

0 Answers0