2

I'm trying to use the library discord.py to create a "faction bot". The create command of my bot raises the exception AttributeError: 'str' object has no attribute 'value'. Here is my code:

@commands.command()
async def c(self, ctx, color: str, *, name: str):
    name_lower = name.lower()

    if name_lower.endswith("owner"):
        embed = discord.Embed(
            title=":x: Error Creating Faction",
            colour=discord.Colour.purple(),
            description="Make sure your faction name doesn't end with the word **owner**."
        )
        await ctx.send(embed=embed)
    else:
        if name_lower.endswith("faction"):
            role_name = name_lower.capitalize()
        else:
            role_name = f"{name_lower.capitalize()} Faction"

        await ctx.guild.create_role(name=role_name, color=color)
        await ctx.guild.create_role(name=f"{role_name} Owner", color=color)

        await ctx.message.add_reaction(":white_check_mark:")

The command takes a color string and a name string as an input. On line 5 (if name_lower.endswith("owner"):) it detects whether a lowercase version of name ends with the word "owner" or not. The problem is, this line raises the exception AttributeError: 'str' object has no attribute 'value'.

What is causing it and how can I fix it?

IAmAHuman
  • 199
  • 6
  • 16
  • 1
    Are you sure it is that line? Can you show the full exception stacktrace? – tobias_k Nov 04 '20 at 08:28
  • @tobias_k I would but discord.py catches command exceptions and outputs them without showing the full stacktrace. I'm sure it's that line because I ran the bot in debug mode (I use PyCharm) with a bunch of breakpoints and the exception was raised right after that line. – IAmAHuman Nov 04 '20 at 09:03
  • You should have `foo.value` or `foo.value()` somewhere in your code where `foo` is a string. Current code snippet can't produce that error message. – Asocia Nov 04 '20 at 09:19
  • 1
    There's (almost) no way this line could raise the error, since the method takes only one parameter and that is clearly a string (and should be). The only way it could be that line is if `name` is _not_ a string but some object that happens to have a method `lower` returning some other object that happens to have a method `endswith` that expects something different than a string. I'd rather think it's in `discord.Embed`. – tobias_k Nov 04 '20 at 09:42
  • 1
    Try replacing that line with just `if True` and see if the error still occurs. – tobias_k Nov 04 '20 at 09:43
  • @tobias_k Even if that is the case, it wouldn't raise `AttributeError` saying string object has no attribute `value`. Maybe it would be a `TypeError` if it expects a different parameter type. – Asocia Nov 04 '20 at 09:50
  • @Asocia I guess that would depend on how that mysterious object is implemented, whether it checks the type or just tries to access the attribute. – tobias_k Nov 04 '20 at 09:55
  • 2
    @tobias_k Alright you were right, that line didn't raise that error. The error was raised by `await ctx.guild.create_role(name=role_name, color=color)`. Apparently the color parameter requires the `discord.Colour` class, not a string. I just replaced `color` with `discord.Colour(int(f"0x{color}", 0))` and it worked like a charm. Thanks for the help! – IAmAHuman Nov 04 '20 at 10:54

2 Answers2

0

The most common source of that error is when you redefine a variable and shadow the actual variable. I cant see your whole program, but I would recommend verifying if you have redefined any variables in your program (mostly due to typo).

A good example is:

for items in items:
  // loop
Serial Lazer
  • 1,667
  • 1
  • 7
  • 15
0

Solution taken from this comment by IAmAHuman

The error was raised by await ctx.guild.create_role(name=role_name, color=color). Apparently the color parameter requires the discord.Colour class, not a string. I just replaced color with discord.Colour(int(f"0x{color}", 0)) and it worked

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153