Disnake – 尝试获取消息作者名称时出现错误

huangapple go评论63阅读模式
英文:

Disnake - Error when try get message author name

问题

I have problem with Disnake.

When I try to get the message author's name, discriminator, and other similar information, 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 I'm a beginner in Disnake. I expect that I just missed something.

英文:

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.

答案1

得分: 0

我使用类而不是实例。所以我需要将 ctx 放在 disnake.Message 之前 - 这段代码必须能够运行。

英文:

I used class instead of instance. So I need put ctx instead of disnake.Message - and this code must work.

答案2

得分: 0

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}")
英文:

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


</details>



huangapple
  • 本文由 发表于 2023年5月17日 20:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定