【问题标题】:Why does editing an embed in one part of the code throw an error when it doesn't in another?为什么在代码的一部分中编辑嵌入时会引发错误,而在另一部分中却没有?
【发布时间】:2020-09-30 09:00:48
【问题描述】:

目前正在为我们之中的一个不和谐机器人工作,遇到一个我似乎无法修复的奇怪错误。

当我尝试编辑嵌入时,我在代码的一部分中没有遇到问题,但是当我复制并粘贴代码以在另一区域执行完全相同的操作时,它给了我这个错误: discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body

这是有问题的代码:

if emoji == '✅':
            #Add User to lobby members list
            if user in LOBBY_MEMBERS:
                consoleMessage = str(user) + ' is already in the lobby.'
            else:
                LOBBY_MEMBERS.append(user)
                consoleMessage = str(user) + ' has joined the lobby.'
                
                updated_embed=discord.Embed(title="Among Us Lobby", description="Welcome to the among us bot!", color=0x00ff1e)
################Error does not show up here:##########################
                await reaction.message.edit(embed=updated_embed)
if emoji == '❌':
            if user in LOBBY_MEMBERS:
                LOBBY_MEMBERS.remove(user)
                consoleMessage = str(user) + ' has left the lobby.'
                
                updated_embed=discord.Embed(title="Among Us Lobby", description="Welcome to the among us bot!", color=0x00ff1e)
################Error shows up here:##########################
                await reaction.message.edit(embed=updated_embed)
            else:
                consoleMessage = str(user) + ' is not in the lobby. Cannot remove someone who has not joined.'

我真的是 python 新手,我不知道为什么它在代码的一部分中起作用,而在另一部分中不起作用。

【问题讨论】:

  • 欢迎来到 Stack Overflow!寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码在问题本身。没有明确的问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Reproducible example

标签: python discord discord.py


【解决方案1】:

所以,如果 if/else 逻辑将它放在相反的块中,它永远不会到达上半部分。

如果 LOBBY_MEMBERS 中的用户返回 TRUE,则您跳过调用 discord.Embed 完全在上块中,但是当您尝试在下块中调用它时出错。

【讨论】:

  • 代码的其他部分虽然在块内工作,但其中的代码按预期工作,所以我认为它正在按照它应该的逻辑进行......就像代码的其他部分一样正在那些 if/else 语句中工作。
  • 你永远不会到达第一个 else 中,因为相应的 if 为真。第二个 if/else 对也返回 if 为 true,因为它让您进入 discord.Embed 调用。基本上,您已经知道 discord.Embed 调用存在问题,但是如果您进入不应该进入的 if/else 块的一部分,显然也存在逻辑流程问题。跨度>
  • 我不认为问题出在逻辑上,因为代码按预期工作,否则...实际上只是那一行给我带来了问题,我不知道为什么。
  • 所以,如果你在任何逻辑循环之外调用 updated_embed=discord.Embed(title="Among Us Lobby", description="Welcome to the around us bot!", color=0x00ff1e)不给你问题? ------ 另外,你用的是什么包?
  • 我呃...想通了? 我编辑了一大段代码,因为我认为这对问题并不重要,结果证明是导致错误的原因。试图发送一个没有实际值的字符串,这导致了错误。感谢您的帮助!
【解决方案2】:

我已经想通了,我只是个白痴,请忽略我。

我从问题中删去了一大段代码,因为我认为这并不重要,结果发现问题实际上出在哪里 ?

基本上,我正在创建一个加入大厅的人的列表,并且没有设置当大厅中有 0 人时的案例设置,因此为什么当我作为唯一的成员测试它时,然后我收到一个错误,因为那里没有价值。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 2012-08-18
    相关资源
    最近更新 更多