0

I would like to get from an emoji in the ctx.guild the corresponding ID, because this is not changeable and so it is easier to query it and not get in the way if suddenly the name is changed. But now the following problem:

I always get the following error when I try to make an entry in the database:

    roles = rxner.find_one({"role1": role1.id}, {"emoji": emoji2}) # Search for existing entry about role + emoji
UnboundLocalError: local variable 'emoji2' referenced before assignment

My code to get the ID looks like this so far:

for em in ctx.guild.emojis: # loop over emojis
    if str(em.id) == emoji:
        emoji2 = em.id # get the ID
        break # break

roles = rxner.find_one({"role1": role1.id}, {"emoji": emoji2})  # Search for existing entry about role + emoji
emojicheck = rxner.find_one({"emoji": emoji})  # Check which role + emoji belong together
roletofind = rxner.find_one({"role1": role1.id})

This must be where the error is somewhere, but I don't know exactly where.

I've looked at many posts here, even defined emoji globally, but that didn't help. I also tried to return the emoji but none of that works. As I set emoji: str as a required argument in the command that totally works but if the name is changed or something, I can't work with that anymore. Is there a solution how to get the ID in an easier way/fix the problem?

And yes, I looked at the following posts:

R6Winner
  • 29
  • 7
  • `emoji2` is only defined if `str(em.id) == emoji`. You should define `emoji2` first, or check it exists before using it. – match Dec 04 '21 at 13:07
  • 1
    Just a suggestion: Python allows an `else` after a `for` loop. You can use that to add error handling to your code. – Ulrich Eckhardt Dec 04 '21 at 13:13
  • @match thanks for the hint. However, if I directly try to define `emoji2` and try to get the `ID` from the corresponding emoji it "does not work" e.g. I do not really know if I do it the correct way... – R6Winner Dec 04 '21 at 13:13
  • @UlrichEckhardt That is a good suggestion. "After a for-loop" means I use the known `if/else` statement or put `else` statement outside the `for`-loop? – R6Winner Dec 04 '21 at 13:14
  • https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops – Ulrich Eckhardt Dec 04 '21 at 13:16

2 Answers2

0

The error message is quite clear: emoji2 isn't defined by the time you get to the roles definition. This would mean that str(em.id) == emoji never is true.

ades
  • 207
  • 2
  • 13
  • I quite get the point that it should be referenced before but I am stuck at this point, just trying out what I can still do. – R6Winner Dec 04 '21 at 13:20
  • Look at the values in `ctx.guild.emojis` and compare with your `emoji` - maybe it's as simple as that you need to do `if em`id` == emoji.id`? – ades Dec 04 '21 at 13:23
  • Unluckily not that simple as mentioned. I printed out `em` to see what I can make out of it but `emoji` does not have `id` in my case, only if I use `emoji: discord.Emoji` or something, I use it as a `str` – R6Winner Dec 04 '21 at 13:36
  • Then it sounds like you need to look into how you can find the id of the emoji you're looking for. – ades Dec 04 '21 at 14:29
0

As Ulrich Eckhardt stated in his comment:

It makes sense to use an else statement here. I assume you are looking for a specific emoji while going through the loop?

Since you can have custom emojis and some kind of Unicode emoji, you have to check both ways.

Have a look at the following code:

        for em in ctx.guild.emojis:
            print(em) # We print all the emojis
            if str(em) == emoji: # State what has to match
                emoji2 = em.id # em.id is our emoji2
                print(emoji2) # Prints out the ID
                break # If something was found

        else: # If there is no match and the for-loop is over
            emoji2 = emoji # We just take the str or whatever (unicode emojis)
  • Removing str(em.id) and replacing it with str(em) and giving an else-statement should do the trick here.
Dominik
  • 3,551
  • 2
  • 6
  • 32