Discord & multiprocessing deadman switch python

0

i was trying to make this logic so if i got a message on discord that contains "911" it destroys the script or calls some functions and stop the timer im facing a lot of errors and i can't think ofa proper way to do it ...

any help ?

from time import sleep
import discord
import multiprocessing

client = discord.Client()
manager = multiprocessing.Manager()
sign = manager.boolean(False)

def Timer(sign):
    for second in range(15):
        if sign == True:
            print("bloooock")
            break
        if sign == False:
            print("passsssss")
            sleep(1)
def discord_message(client, sign):
    @client.event
    async def on_ready():
        print('Logged on as', client)
    async def on_message(message):
        messageContent = message.content
        if len(messageContent) > 0:
            if messageContent == str("911"):
                sign = manager.bool(True)
    client.run('###')
if __name__ == '__main__':
    freeze_support()
    p1 = multiprocessing.Process(target=Timer, args=(sign,))
    p2 = multiprocessing.Process(target=discord_message, args=(client, sign,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
discord discord.py python python-3.x
2021-11-23 22:45:54
1

1

When you set a variable within a function, it will default to the local scope. Look at this example:

a = 1
def f(): a = 2
f()
print(a)

Try it online!

This will output 1.

a = 1
def f(): print(a); a = 2
f()
print(a)

Try it online!

This will actually error, because since you set a = 2 within the function f, a becomes a local variable so print(a) errors because it isn't defined at that point.

To get around this, put global a at the top of your function. In your case, do global sign inside def on_message like so:

def discord_message(client, sign):
    ...
    async def on_message(message):
        global sign
        ...

Right now, when sign = manager.bool(True) is being set, it isn't affecting the global variable sign which the Timer is reading.

Also, I think you need to add @client.event above async def on_message as well.

2021-11-23 23:20:31

thanks it works so well :)
Joseph Yosoevsky

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................