[英]"local variable referenced before assignment" error when trying to get emoji ID and compare it
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.我想从ctx.guild
中的emoji
中获取相应的 ID,因为这是不可更改的,因此如果名称突然更改,更容易查询并且不会妨碍。 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:到目前为止,我获取 ID 的代码如下所示:
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.我在这里查看了很多帖子,甚至在全球范围内定义了emoji
,但这并没有帮助。 I also tried to return
the emoji but none of that works.我也尝试return
表情符号,但这些都不起作用。 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.当我将emoji: str
设置为完全有效的command
中的必需参数时,但如果名称发生更改或其他情况,我将无法再使用它。 Is there a solution how to get the ID in an easier way/fix the problem?是否有解决方案如何以更简单的方式获取 ID/解决问题?
And yes, I looked at the following posts:是的,我查看了以下帖子:
The error message is quite clear: emoji2
isn't defined by the time you get to the roles
definition.错误消息很清楚:当您到达roles
定义时,尚未定义emoji2
。 This would mean that str(em.id) == emoji
never is true.这意味着str(em.id) == emoji
永远不会是真的。
As Ulrich Eckhardt in his comment:正如Ulrich Eckhardt在他的评论中所说:
It makes sense to use an else
statement here.在这里使用else
语句是有意义的。 I assume you are looking for a specific emoji
while going through the loop?我假设您在循环时正在寻找特定的emoji
?
Since you can have custom emojis and some kind of Unicode emoji, you have to check both ways.由于您可以拥有自定义表情符号和某种 Unicode 表情符号,因此您必须同时检查两种方式。
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)
str(em.id)
and replacing it with str(em)
and giving an else
-statement should do the trick here.删除str(em.id)
并将其替换为str(em)
并给出else
语句应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.