【问题标题】:AttributeError: module 'embed storage' has no attribute 'help_command'AttributeError:模块“嵌入存储”没有属性“help_command”
【发布时间】:2021-06-14 16:37:50
【问题描述】:

我有我为 discord.py 机器人制作的这个模块(虽然不是 COG)。它显然定义了函数“help_command”,但每当我尝试运行它时,都会出现上述错误。我无法理解它,所以我认为向社区询问可能是值得的。

discord_main.py

import discord
from discord.ext import commands
import gsheet
import os
import embed_storage

async def help(ctx, argument='Helping'):
    await ctx.send(embed=embed_storage.help_command('stargazer', argument))

embed_storage.py

import discord
from cogs.points import points_help_dict
from discord_main import help_dictionary
import gsheet

def help_command(cog_name: str, command: str):
    if cog_name == 'stargazer':
        use_dict = help_dictionary
        title = 'Help'
        description = 'Learn how the Stargazer Bot works'
        footer_message = 'Prefix for this bot is \'a\''
        else:
            raise UnboundLocalError(
                "Yeh this part of the code should have never run.")
    command = command.lower()
    if command in use_dict:
        title = command.title()
        description = use_dict[command]
        field = False

    help_command_embed = discord.Embed(
        title=title,
        description=description,
        colour=discord.Colour.from_rgb(234, 255, 208)
    )
    return help_command_embed

任何帮助都会令人惊叹。也很抱歉这个愚蠢的问题。只是无法解决这个愚蠢的错误。

编辑:尝试回滚到我最新的工作版本的代码以及我运行它的时间。它仍然给了我同样的错误。所以错误不在新添加的代码中

【问题讨论】:

  • 看起来你们已经开始相互导入了。您能否更新您的问题以显示您的所有导入?
  • 是否会两次导入相同的模块(一次在 discord_main.py 中,另一次在 embed_storage.py 中)会导致此错误?
  • Python 版本 3.8.1
  • 我已经更新了答案。
  • 这不起作用,Discord 自动关闭了会话。但我意识到我真的不需要discord_main.py 中的帮助字典。字典的一个简单的 Ctrl + C、Ctrl + v 解决了这个问题。非常感谢

标签: python python-3.x discord.py attributeerror


【解决方案1】:

我认为问题在于您从discord_main 导入embed_storage,反之亦然。

您需要以某种方式解决此问题,或者,如果没有其他方法,您可以将导入移动到函数定义中,例如在embed_storage.py:

def help_command(cog_name: str, command: str):
    from discord_main import help_dictionary
    if cog_name == 'stargazer':
        use_dict = help_dictionary
# ...

【讨论】:

  • 得到以下错误:ImportError: cannot import name 'help_command' from 'embed_storage' (C:\Users\Aryan Garg\PycharmProjects\StarGrazer\embed_storage.py)
  • 您是否在discord_main.py 中进行导入?
  • 是的。我正在将 embed_storage 导入 discord_main.py
猜你喜欢
  • 1970-01-01
  • 2017-11-12
  • 2016-07-30
  • 2016-02-23
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
  • 2019-02-18
相关资源
最近更新 更多