'asyncio.run() cannot be called from an event loop

I'm trying to call a function in async function and the problem is asyncio.run() don't work in a loop event

Here is the RuntimeError:

asyncio.run() cannot be called from a running event loop.

I also tried to use nest_asyncio.apply() but it's show me the same problem

Here is my code:

@client.event
async def on_message(message):
    
    if message.content.startswith("!shodan organization"):
        ip = (message.content.split()[2])
        queryexec =  asyncio.run(query(hostname,apikey,ip))
      
        message.channel.send(queryexec)

The function that I'm trying to call:

async def query(hostname,apikey,ip):
        url = "http://%s" % (hostname) + "/ip/%s" % ip + "?key=%s" % apikey
        r = requests.get(url)
        if r.status_code == requests.codes.NOT_FOUND:
            return 'test 1'
        if r.status_code == requests.codes.OK:
            return r.json()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source