M
M
mishkafrede20042022-01-20 11:42:52
Python
mishkafrede2004, 2022-01-20 11:42:52

When creating the unban command, discord.py throws this error. Command raised an exception: AttributeError: 'str' object has no attribute 'name' what to do?

I am creating a discord bot and when displaying information about unbanned in embed, it gives an error Command raised an exception: AttributeError: 'str' object has no attribute 'name'

@bot.command(pass_context = True)
@commands.has_permissions(view_audit_log=True)
async def unban(ctx,*,member):
    await ctx.channel.purge(limit = 1)
    emb = discord.Embed (title = 'Unban :unlock:', colour = 15105570)
    banned_users = await ctx.guild.bans()
    for ban_entry in banned_users:
        user = ban_entry.user
        await ctx.guild.unban (user)
    emb = discord.Embed (title = 'Unban :lock:', colour = 15105570)
    emb.set_author (name = member.name, icon_url = member.avatar_url)
  emb.add_field (name = 'Ban user', value = 'Baned user : {}'.format(member.mention))
  emb.set_footer (text = 'Был заблокирован администратором {}'.format (ctx.author.name), icon_url = ctx.author.avatar_url)
  await ctx.send( embed = emb)


In the console gives this
Ignoring exception in command unban:
Traceback (most recent call last):
  File "C:\Users\alopa\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "E:\Новая папка (2)\main.py", line 74, in unban
    emb.set_author (name = member.name, icon_url = member.avatar_url)
AttributeError: 'str' object has no attribute 'name'

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

Traceback (most recent call last):
  File "C:\Users\alopa\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\alopa\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\alopa\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'name'


Please help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2022-01-20
@mishkafrede2004

In this case, member is just a string. If you call a function that mentions /unban @user1222, then you can use the built-in converter. Similar to regular type annotations, described here: https://discordpy.readthedocs.io/en/stable/ext/com...
async def unban(ctx,*, member: discord.Member):

V
Vindicar, 2022-01-20
@Vindicar

1. Turn on the head.
2. Read the documentation about parameter conversion in bot command handlers.
3. Realize that in the description of the async handler def unban(ctx,*,member): member has no type indication, and therefore discord.py leaves it as a string .
4. Contrast this fact with the fact that calling member.name generates the error " string object has no name attribute"
5. Slap yourself on the forehead.
6. Insert a type indication for member: member: discord.Member into the handler declaration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question