'How to fix error: module 'discord' has no attribute 'Bot'

The Situation:

I'm trying to make a simple discord bot with pycord, but every time I run the code it gives this error:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    bot = discord.Bot()
AttributeError: module 'discord' has no attribute 'Bot'

The code:

import discord

bot = discord.Bot()

@bot.slash_command()
async def test(ctx):
  await ctx.send('Success!')

bot.run('token')

What I have Done:

I've already checked if I have pycord installed and if my token is correct.



Solution 1:[1]

When looking at your error message:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    bot = discord.Bot()
AttributeError: module 'discord' has no attribute 'Bot'

The key is to notice AttributeError is telling you that the module you have imported does not have the attribute Bot()

This indicates that you are using it wrong.

Take a look at the documentation for the correct usage and also at this tutorial

You will see that you need to use.


# bot.py
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

Edit after the comment by @Taku

After the comment below, I believe it could be required upgrading the library as done within the example

As well as requiring the command prefix as done within the example in the url

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix=">")

@bot.command()
async def ping(ctx):
    await ctx.send("pong")

bot.run("token")

Solution 2:[2]

The reason for this error is that your pycord version is v1.7.3 which doesnt support the syntax you used. You need to upgrade to v2.0.0 using these commands(Windows):

git clone https://github.com/Pycord-Development/pycord
cd pycord
pip install -U . 

or pip install -U .[voice] if you require voice support

Sources

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

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Hridesh MG