pythondiscorddisnake

Disnake - Error when try get message author name


I have problem with Disnake.

When I try get message author like name, discriminator and other like this - I get this error:

Ignoring exception in command user:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\core.py", line 173, in wrapped
    ret = await coro(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Other\discord-bot\test\test-discord.py", line 13, in _user
    await ctx.send(disnake.Message.author.name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'member_descriptor' object has no attribute 'name'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\bot_base.py", line 589, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\core.py", line 914, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\core.py", line 182, in wrapped
    raise CommandInvokeError(exc) from exc
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'member_descriptor' object has no attribute 'name'

My code:

import disnake
from disnake.ext import commands

intents = disnake.Intents.all()
bot = commands.Bot(command_prefix="t!", intents=intents, activity=disnake.Game(name="testing, testing..."))

@bot.event
async def on_ready():
    print("bot started")

@bot.command(name="user")
async def _user(ctx):
  await ctx.send(disnake.Message.author.name)

bot.run("my token lol")

I suspect this is my fault, because im a noob in Disnake. I expect that I just missed something.


Solution

  • To get the author's name: ctx.author.name. And for the Discriminator: ctx.author.discriminator See this Link. So your code would look like this:

    @bot.command(name="user")
    async def _user(ctx):
      await ctx.send(f"{ctx.author.name}")