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

开发Discord YouTube热门视频Bot遇报错:discord无StoreData属性

问题:Discord机器人运行时报错discord has no attribute 'StoreData'

我正在开发一个Discord机器人,功能是根据用户输入的主题返回韩国地区的YouTube热门视频,但运行代码后出现错误:discord has no attribute 'StoreData'。

编写的代码:

import googleapiclient.discovery
from discord.ext import commands
import typing
from discord_ui import UI
cmd = commands.Bot(command_prefix='.')
client = discord.Client()

token = 


api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = 'googleapikeyhere'
youtube = googleapiclient.discovery.build(
    api_service_name, api_version, developerKey=DEVELOPER_KEY)

@cmd.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandOnCooldown):
        await ctx.reply(f"You are on cooldown for {round(error.retry_after, 2)} seconds!")


@ cmd.command()
@ commands.cooldown(1, 4, commands.BucketType.user)
async def info(ctx, *args):
    output2 = ''
    for word in args:
        output2 += str(word)
        output2 += ' '
        print(output2)
    request3 = youtube.videos().list(
        part="snippet, {}, statistics".format(output2),
        chart="mostPopular",
        regionCode="KR"
    )
    response3 = request3.execute()
    print(response3)
    channame = response3['items'][0]['snippet']['title']
    chanid = response3['etag']
    numberViews = response3['Items'][0]['statistics']['viewCount']
    numberLikes = response3['Items'][0]['statistics']['likeCount']
    embed2 = discord.Embed(title=f"{channame}", url=f"https://www.youtube.com/watch?v={chanid}",
                         numberviews=f"{numberViews}", numberlikes= f"{numberLikes}", color=0xFF5733)
    response3 = request3.execute()
    await ctx.send(embed=embed2)

cmd.run('findvideos')

错误原因分析:

你代码里没有直接调用discord.StoreData,这个报错的核心原因是第三方库discord_ui与当前使用的discord.py版本不兼容:

  • discord_ui是非官方的Discord UI组件库,它内部可能尝试调用discord.StoreData,但这个属性在你安装的discord.py版本中不存在(比如discord.py v2.x重构了内部结构,移除或重命名了该属性)。

另外,代码还存在其他几个明显问题(虽不直接导致当前报错,但会引发后续异常):

  • 同时初始化了commands.Bot和discord.Client,二者选其一即可,建议保留commands.Bot(因为使用了命令装饰器)。
  • token变量未赋值,且cmd.run('findvideos')传入的是错误值,应该传入你的Discord机器人token。
  • YouTube API调用逻辑错误:videos().list的part参数是指定要获取的资源字段(如snippet、statistics),不是用户输入的主题;如果要根据主题搜索视频,应该使用search().list接口,并通过q参数传入用户输入的关键词。
  • 响应解析时的大小写错误:response3['Items']应改为response3['items'](YouTube API返回的键是小写)。
  • Embed构建错误:discord.Embed没有numberviews、numberlikes这类参数,需要用add_field()方法添加这些信息。
  • 重复调用request3.execute(),第一次执行后已获取响应,无需再次调用。

修复建议:

  1. 移除discord_ui依赖,改用discord.py官方的discord.ui模块(官方维护,兼容性更好),或者确认discord_ui与你的discord.py版本匹配(比如安装适配v2.x的discord_ui分支)。
  2. 删除冗余的client = discord.Client()代码。
  3. 正确赋值token变量,并在cmd.run()中传入该token。
  4. 调整YouTube API调用逻辑:如果要搜索主题相关的视频,使用youtube.search().list(),同时指定q=output2、regionCode="KR"、type="video"等参数;如果要获取全品类热门视频,直接调用videos().list()即可,无需传入主题。
  5. 修正Embed的构建方式,使用add_field添加浏览量、点赞数等信息。
  6. 修正响应解析时的键名大小写错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 04:10:29