创建组时discord.py给出AttributeError

时间:2018-08-23 14:55:21

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

我试图制造一个可以实现reddit功能的不和谐机器人。我决定将其分为具有帮助功能的组和具有共同功能的组。

# reddit group commands
@function_debug
@bot.group(pass_context = True)
async def reddit(context):
    message = context.message
    logger.debug(message.content)
    try:
        if message.server.name == 'PeaceCrafters' and message.channel.name == 'utilbots':
            cont = True
        elif message.server.name != 'PeaceCrafters':
            cont = True
        else:
            cont = False
    except AttributeError:
        cont = True
    finally:
        if cont:
            if context.invoked_subcommand is None:
                await reddit_help()


@function_debug
@reddit.command(name = 'help', description = 'Provides help with reddit', pass_context = True)
async def reddit_help(context):
    message = context.message
    logger.debug(message.content)
    try:
        if message.server.name == 'PeaceCrafters' and message.channel.name == 'utilbots':
            cont = True
        elif message.server.name != 'PeaceCrafters':
            cont = True
        else:
            cont = False
    except AttributeError:
        cont = True
    finally:
        if cont:
            message = reddit_message
            await bot.say(message)


@function_debug
@reddit.group(pass_context = True)
async def subreddit(context):
    message = context.message
    logger.debug(message.content)
    try:
        if message.server.name == 'PeaceCrafters' and message.channel.name == 'utilbots':
            cont = True
        elif message.server.name != 'PeaceCrafters':
            cont = True
        else:
            cont = False
    except AttributeError:
        cont = True
    finally:
        if cont:
            info = message.content.split(' ')[1:]
            subreddit = info[0]

@function_debug
@subreddit.command(name = 'hot', description = 'Lists the hot commands in the subreddit', pass_context = True)
async def subreddit_hot(context):
    message = context.message
    logger.debug(message.content)
    try:
        if message.server.name == 'PeaceCrafters' and message.channel.name == 'utilbots':
            cont = True
        elif message.server.name != 'PeaceCrafters':
            cont = True
        else:
            cont = False
    except AttributeError:
        cont = True
    finally:
        if cont:
            pass #i will add more once i figure out how message.content is handled

但是,每当我运行代码时,都会出现此错误:

Traceback (most recent call last):
  File "*/main.py", line 447, in <module>
    @reddit.group(name = 'reddit', pass_context=True)
AttributeError: 'function' object has no attribute 'group'
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x00000223C92FFEF0>

我在网上查看了其他人如何编码他们的小组,并据此证明我的代码应该有效。为什么会出现该错误?

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您的@function_debug装饰器返回的是function,而不是group。重写装饰器以返回协程并切换装饰器的顺序。

@bot.group(pass_context = True)
@function_debug