Home > Mobile >  Trying to learn how to make a discord bot with python. Keep getting same error everytime I try to st
Trying to learn how to make a discord bot with python. Keep getting same error everytime I try to st

Time:12-12

Ok so bear with me. Im still learning and may ask you some questions. As stated in title I recently read a few books on how to code python. Made a small stupid app, small pygame and text game. So I wanted to do something new and harder for myself and to try and grow.

I followed a tutorial on youtube and the person didnt have this issue come up when they were coding. So heres the code that I have for my discord bot:

import discord
import os


client = discord.Client()

TOKEN = '##############################'

@client.event
async def on_message(message):
    if message.auther == client.user:
        return
    
    if message.content.startswith('$hello'):
        await message.channel.send('Hello World')


@client.event
async def on_connect():
    print("Bot connected to the sever!")

client.run(TOKEN)

Now when the guy in the tutorial runs his code there is no errors. But when I run my code on my pc I get back the following:

    Traceback (most recent call last):

  File "C:\Users\ccarr\anaconda3\lib\site-packages\discord\client.py", line 713, in run
    loop.run_forever()

  File "C:\Users\ccarr\anaconda3\lib\asyncio\base_events.py", line 586, in run_forever
    self._check_running()

  File "C:\Users\ccarr\anaconda3\lib\asyncio\base_events.py", line 578, in _check_running
    raise RuntimeError('This event loop is already running')

RuntimeError: This event loop is already running


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\ccarr\anaconda3\lib\site-packages\discord\client.py", line 90, in _cleanup_loop
    _cancel_tasks(loop)

  File "C:\Users\ccarr\anaconda3\lib\site-packages\discord\client.py", line 75, in _cancel_tasks
    loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))

  File "C:\Users\ccarr\anaconda3\lib\asyncio\base_events.py", line 618, in run_until_complete
    self._check_running()

  File "C:\Users\ccarr\anaconda3\lib\asyncio\base_events.py", line 578, in _check_running
    raise RuntimeError('This event loop is already running')

RuntimeError: This event loop is already running


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\ccarr\OneDrive\Desktop\tenta bot\main.py", line 29, in <module>
    client.run(TOKEN)

  File "C:\Users\ccarr\anaconda3\lib\site-packages\discord\client.py", line 719, in run
    _cleanup_loop(loop)

  File "C:\Users\ccarr\anaconda3\lib\site-packages\discord\client.py", line 95, in _cleanup_loop
    loop.close()

  File "C:\Users\ccarr\anaconda3\lib\asyncio\selector_events.py", line 89, in close
    raise RuntimeError("Cannot close a running event loop")

RuntimeError: Cannot close a running event loop

I have no idea what is really going on error wise. I do understand that its a simple code that will say hello in ta message when typeing the proper command in a chat.

CodePudding user response:

Your problem has to do with asyncio. see a similar question Here and especially the second answer

  • Related