beginner: Discord bot problems - AttributeError: 'NoneType' object has no attribute 'display_name'

251 Views Asked by At
import discord
from discord.ext.commands import Bot

bot = Bot(command_prefix='$')
intents = discord.Intents().all()
TOKEN = 'changeditforexample'

@bot.event
async def on_ready():
    print(f'Bot connected as {bot.user}')


@bot.command(name='server')
async def fetchserverInfo(ctx):
    guild = ctx.guild

    await ctx.send(f'Server Name: {guild.name}')
    await ctx.send(f'Server Size: {len(guild.members)}')
    await ctx.send(f'Server owner: {guild.owner.display_name}')


bot.run(TOKEN)

The actual error.

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'display_name'

So my question is how do i make it work? it only shows the server has 1 member which is wrong i have 3 accounts in there and the {guild.owner.display_name} doesn't seem to work.

i'm pretty new to python and coding in general so it might just be really simple i just can't figure it out. i did see an example which showed this should work but it doesn't

I'm using

  • discord.py 1.7.2
  • python 3.9
  • newest version of pycharm

Error + Bot results

1

There are 1 best solutions below

1
On

Currently, your intents aren't being passed to the Bot constructor, therefore they aren't taking effect. The proper way to enable intents is:

bot = Bot(command_prefix='$', intents=discord.Intents.all())

Intents.all() is also a class method which is why you don't need the parentheses after Intents