Discord.py: Issue with running in the new thread

Created on 7 Mar 2019  路  2Comments  路  Source: Rapptz/discord.py

Hello,
Could you help me with this situation (don't judge me, I am not very familiar with python).
I want to launch discord in the new thread via client.run()
This is a working example:

TOKEN = '123'
client = discord.Client()

def run_discord():
    client.run(TOKEN)

def init():
    t = threading.Thread(target=run_discord)
    t.start()
    count()

def count():
    while True:
        time.sleep(1)
        print("while-true")

init()

Created a thread -> client connected -> it reacts on @client.event -> main thread is not blocked (print("while-true")) keeps working. Seems fine if I am not wrong (used python3.6 from Ubuntu).

Now I am doing the same via c code + python3.7 (I've posted my question on the SO but nobody answered to me so far. Sorry for the duplication.)
So, I am calling the same code from the Main thread but I am getting an error:

Traceback (most recent call last):
  File "./build/Lib/asyncio/unix_events.py", line 92, in add_signal_handler
ValueError: set_wakeup_fd only works in main thread

Am I doing something wrong? Could there be an issue inside the python library? (I've compiled it by myself). Ah and discord will work fine if I remove the threads (but all the execution of the other components of the game will be blocked as expected).
Any ideas? Thank you in advance.

Added: I've managed to do this:

TOKEN = '123'

client = discord.Client()
asyncio.get_child_watcher()
loop = asyncio.get_event_loop()

def init():
    loop.create_task(client.start(TOKEN))
    thread = threading.Thread(target=loop.run_forever())
    thread.start()

It connects, but events tell me that
AttributeError: 'Client' object has no attribute 'send_message'
What's this?

Most helpful comment

You are free to close this issue.

As are you.

All 2 comments

Oh gawd I'm an idiot.

This is the final working version

TOKEN = '123'

client = discord.Client()

async def start():
    await client.start(TOKEN)

def run_it_forever(loop):
    loop.run_forever()

def init():
    asyncio.get_child_watcher()
    loop = asyncio.get_event_loop()
    loop.create_task(start())

    thread = threading.Thread(target=run_it_forever, args=(loop,))
    thread.start()

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    await message.channel.send('Hello!')

It works.
Inside that game I compiled and was using rewrite version while inside the system over the pip I got 0.16.12 and read the same documentation.
You are free to close this issue.

You are free to close this issue.

As are you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VoxelPrismatic picture VoxelPrismatic  路  16Comments

AceKiron picture AceKiron  路  23Comments

haaddaa1 picture haaddaa1  路  14Comments

Yuvira picture Yuvira  路  18Comments

ThePiGuy24 picture ThePiGuy24  路  17Comments