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!