N
N
Nikita Mokhovikov2021-05-05 10:02:05
Python
Nikita Mokhovikov, 2021-05-05 10:02:05

Doesn't display role names in chat, error AttributeError: 'NoneType' object has no attribute 'mention', but id of all roles is displayed. Where is the mistake?

I am making my own economic bot. I created the !sc-shop command, it was to display all the roles from the database. But when it is executed, an error AttributeError: 'NoneType' object has no attribute 'mention' is thrown

@client.command(aliases=['shop'])
async def __shop(ctx):
    embed = discord.Embed(
        title='Магазин',
        description='Здесь вы можете приобрести роль на сервере за **SCoins**',
        color=0xCC33FF
    )

    for row in cursor.execute(f'SELECT role_id, cost FROM shop WHERE id = {ctx.guild.id} ORDER BY cost'):
        embed.add_field(
            name=f'Стоимость: **{row[1]} SCoins**',
            value=f'Роль - {ctx.guild.get_role(row[0]).mention}',
            inline=False
        )

    await ctx.send(embed = embed)

As I understand it, he did not find the role id, so he returns NoneType, and as a result he cannot find mention. But if we rewrite the command so that only the id of the roles would be displayed:
@client.command(aliases=['shop'])
async def __shop(ctx):
    embed = discord.Embed(
        title='Магазин',
        description='Здесь вы можете приобрести роль на сервере за **SCoins**',
        color=0xCC33FF
    )

    for row in cursor.execute(f'SELECT role_id, cost FROM shop WHERE id = {ctx.guild.id} ORDER BY cost'):
        embed.add_field(
            name=f'Стоимость: **{row[1]} SCoins**',
            value=f'Роль - {row[0]}',
            inline=False
        )

    await ctx.send(embed = embed)

Then it will display all the roles with id
Help solve the problem, so that the names of each of the roles would be displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2021-05-05
@mohovoy

Check type(row[0])most likely you store/db returns ID as a string, get_roleaccepts ID asint

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question