BlazingLeaf12
BlazingLeaf12

Reputation: 332

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.

Upvotes: 4

Views: 23203

Answers (3)

Mark Booth
Mark Booth

Reputation: 7934

PyCord 2 beta 1 has just been released, so you can now install it with

pip install py-cord==2.0.0b1

rather than installing the version from source.

In order to get the example to work though, you need to add the applications.commands scope to your OAuth2 URL and re-register you bot with your test server.

Also, the quickstart guide now suggests adding a list of guild (server) id's when creating a slash_command :

The guild_ids attribute contains a list of guilds where this command will be active. If you omit it, the command will be globally available, and may take up to an hour to register.

Upvotes: 5

Hridesh MG
Hridesh MG

Reputation: 21

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

Upvotes: 2

Kwsswart
Kwsswart

Reputation: 541

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")

Upvotes: 3

Related Questions