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

Discord音乐机器人play命令触发AttributeError异常问题求助

Discord音乐机器人play命令异常修复方案

第一个AttributeError修复('NoneType' object has no attribute 'channel')

错误原因

触发该错误是因为调用!play命令的用户当前没有加入任何语音频道,author.voice返回的是空值None,尝试访问None的channel属性自然报错。

修复方法

在获取用户语音频道前先做非空判断,不满足条件直接返回提示:

@bot.command()
async def play(ctx, *, command = None):
    global server, server_id, name_channel
    author = ctx.author
    # 新增非空判断代码
    if not author.voice:
        await ctx.channel.send(f"{author.mention}, 请先加入任意语音频道后再使用播放命令!")
        return
    # 原有后续代码

第二个AttributeError修复('Guild' object has no attribute 'voice')

错误原因

discord.py框架中,服务器(Guild)对象的语音频道列表属性是voice_channels,你误用了不存在的voice属性,导致报错。同时你在voice变量还未定义的情况下就尝试赋值给voice.channel,变量名也写错了。

修复方法

把两处调用server.voice.channels的代码替换为正确的属性和变量名:

# 错误写法
# name_channel = author.voice.channel.name
# voice.channel = discord.utils.get(server.voice.channels, name = name_channel)
# 正确写法
name_channel = author.voice.channel.name
voice_channel = discord.utils.get(server.voice_channels, name = name_channel)

其他需要同步修复的代码问题

你的代码还存在多处拼写错误和逻辑问题,否则修复完上述两个错误后仍会报错:

  • 拼写错误:所有ctx.chanell改为ctx.channel,sourse改为source,startswithh改为startswith,postprovessors改为postprocessors,preferredguality改为preferredquality
  • check_domains函数逻辑错误:当前循环只判断第一个域名就返回结果,需要将return False移到for循环外部
  • 域名配置错误:domains列表里的YouTube域名写错,把https://www/youtube.com改为https://www.youtube.com
  • 不要使用全局变量存储server、name_channel这类单请求上下文参数,多用户同时调用会出现参数冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 08:45:04