0

I have been trying to build a discord bot using Python. Unfortunately, I have came across this error in the code which always seem to come up when I run the code.

Here is my error message:

runfile('D:/Isaac Khong/Personal and School/Personal/programming/python/Discord Bots/iK10 Official/ik10-discordbot.py', wdir='D:/Isaac Khong/Personal and School/Personal/programming/python/Discord Bots/iK10 Official')
Traceback (most recent call last):

  File "D:\Isaac Khong\Personal and School\Personal\programming\python\Discord Bots\iK10 Official\ik10-discordbot.py", line 56, in <module>
    client.run('4e17e1549ce6dc2eff051db897ffb86d90514ac6dbbb68e20d9074af4aea9559')

  File "C:\Users\isaac\Anacoonda_New\lib\site-packages\discord\client.py", line 860, in run
    asyncio.run(runner())

  File "C:\Users\isaac\Anacoonda_New\lib\asyncio\runners.py", line 33, in run
    raise RuntimeError(

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

This is the code, using the example at https://discordpy.readthedocs.io/en/stable/quickstart.html:

import discord

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run('My Client ID Here (in a string)')

I have also tried my own code here, which comes up with the same error pointing to the line "client.run("Client ID Here")":

import discord



TOKEN = '1081845655909187594'
GUILD = "iK10 Community"
intents = discord.Intents.default()
client = discord.Client(intents = intents)

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

client.run(TOKEN)

I am using Spyder Anaconda. This is my version of discord.py:

(base) C:\Users\isaac>pip show discord.py
Name: discord.py
Version: 2.2.2
Summary: A Python wrapper for the Discord API
Home-page: https://github.com/Rapptz/discord.py
Author: Rapptz
Author-email:
License: MIT
Location: c:\users\isaac\anacoonda_new\lib\site-packages
Requires: aiohttp
Required-by:

Any help will be appreciated. Thanks!

Isaac Khong
  • 13
  • 1
  • 3
  • Hey there, you seem to be mistaking your bot token with client id. To run your bot, you should use your bot token. As for the error, I don't see `asyncio.run(runner())` in the code you provided which is referenced in the error. – Raymus Mar 05 '23 at 13:28
  • @Raymus That is because `asyncio.run(runner())` is from line 860 of the [client.py](https://github.com/Rapptz/discord.py/blob/master/discord/client.py#L860) file in `discord.py`'s source folder. Which is shown in the traceback. – Catgal Mar 05 '23 at 14:02
  • And for the question, the traceback is saying that the `client.run` method that caused the error is being called from line 56 of the file `ik10-discordbot.py`, and the code that you provided doesn't seem to have that many lines, is it? Can you provide the full code? – Catgal Mar 05 '23 at 14:13

1 Answers1

0

Asyncio is having some trouble running many loops at once, like in this post

It appears to be some problem with Spyder. Luckily, this other post fixes that problem by using nest_asyncio

Clement Genninasca
  • 732
  • 1
  • 4
  • 14