You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人报错:'Client'对象无'author'属性求助

亚马逊爬取Discord机器人报错:AttributeError: 'Client' object has no attribute 'author'

问题描述

开发亚马逊网页爬取Discord机器人时出现错误:AttributeError: 'Client' object has no attribute 'author',报错提示位于文件/Users/kailash/Documents/devkai/Amazon Scrapper/bot.py的on_message函数第13行,但该行实际为空。尝试更换discord.py版本为1.0.1,问题仍未解决。

代码示例

import discord
from amazon import search

TOKEN = 'hidden'
CHANNEL_ID = 'hidden'

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:   #line 11
        return 
#line 13
    if message.channel.id != CHANNEL_ID:
        return

    if message.content.split(' ')[0] == '!amazon':
        try:
            query = message.content.replace('!amazon ', '')

            item = search(query)

            embed = discord.Embed(
                title=item['title'],
                url='https://www.amazon.co.uk/' + item['url']
            )
            embed.set_thumbnail(
                url=item['img']
            )
            embed.add_field(
                name='Price',
                value=item['price']
            )
            embed.add_field(
                name='Rating',
                value=item['rating']
            )
            embed.add_field(
                name='Number of Ratings',
                value=item['number_of_ratings']
            )
            await message.channel.send(embed=embed)
        except:
            response = 'An error occurred with your request.'
            await message.channel.send(response)

client.run(TOKEN)

终端报错信息

/usr/local/bin/python "/Users/kailash/Documents/devkai/Amazon Scrapper/bot.py"
2022-11-16 21:42:22 ERROR    discord.client Ignoring exception in on_message
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 409, in _run_event
is not resumed until the WebSocket connection is terminated.
File "/Users/kailash/Documents/devkai/Amazon Scrapper/bot.py", line 13, in on_message
AttributeError: 'Client' object has no attribute 'author'

解决方案

  1. 核对代码行号与实际内容:报错提示的第13行可能因空行、注释的行号统计偏差,实际错误大概率是误将message写成client(比如client.author),需检查on_message函数内所有涉及author的代码,确保调用的是message.author而非client.author。
  2. 修正频道ID类型:message.channel.id是整数类型,当前CHANNEL_ID定义为字符串,需将其转换为整数,避免类型不匹配引发的逻辑异常:
    CHANNEL_ID = int('你的频道ID')
    
  3. 调整discord.py版本:1.0.1版本较老旧,可尝试升级到v1.7.3(该版本与你的代码结构兼容,且是v1.x分支的稳定版),执行命令:
    pip install discord.py==1.7.3
    
  4. 排查外部函数问题:若上述操作无效,需检查amazon.py中的search函数,确认是否在内部错误调用了client.author导致报错。

内容的提问来源于stack exchange,提问作者Kailash Muthu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 20:25:23