NuiDev
NuiDev

Reputation: 121

Bot crashes if user has no role

I'm coding a user info bot, and I'm almost done. My main problem is that my bot crashes if the user has no other role than @everyone and I don't know how to fix it

This is the error I'm getting:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In embeds.0.fields.5.value: This field is required

Here is my code:

from unicodedata import name
import discord
from datetime import datetime
from discord.ext import commands

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

@bot.event
async def on_ready():
    print("Bot calisiyor!")

@bot.command(name="user")
async def user(ctx,user:discord.Member=None):

    if user==None:
        user=ctx.author

    rlist = []
    for role in user.roles:
        if role.name != "@everyone":
            rlist.append(role.mention)
    b = ','.join(rlist)
    
    for role in user.roles:
        if role.name == "@everyone":
            rlist.append("rol yok!")

    embed = discord.Embed(colour=user.color,timestamp=ctx.message.created_at)

    embed.set_author(name=f"User Info - {user}"),
    embed.set_thumbnail(url=user.avatar.url),
    embed.set_footer(text=f'Requested by - {ctx.author}',
    icon_url=ctx.author.avatar.url)

    embed.add_field(name='ID:' ,value=user.id,inline=False)
    embed.add_field(name='Name:' ,value=user.display_name,inline=False)

    embed.add_field(name='Created at:' ,value=datetime.strftime(user.created_at, "%#d/%m/%Y"), inline=False)
    embed.add_field(name='Joined at:' ,value=datetime.strftime(user.joined_at, "%#d/%m/%Y"), inline=False)
    
    embed.add_field(name='Bot?' ,value=user.bot,inline=False)

    embed.add_field(name=f'Roles:({len(rlist)})',value=''.join([b]),inline=False)
    embed.add_field(name='Top Role:' ,value=user.top_role.mention,inline=False)

    await ctx.send(embed=embed)

Upvotes: 0

Views: 30

Answers (1)

moinierer3000
moinierer3000

Reputation: 1979

If the user doesnt have any roles, the embed field Roles is going to be empty since the variable b is going to be an empty string, and you cannot have empty embed fields. I would suggest checking if the list is empty and if it is, send something else.

Something like this:

if rlist:
    b = ','.join(rlist)
else:
    b = "No roles"

Upvotes: 1

Related Questions