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

Discord音乐机器人gg!join命令报错:VoiceState无voice_channel属性

Discord音乐机器人报错修复方案

一、gg!join命令的AttributeError错误修复

错误根源

  1. 版本更新导致属性名变更:discord.py v2.x及以上版本中,VoiceState对象的语音频道属性已从voice_channel改为channel,原代码使用旧属性名触发报错。
  2. 命令参数错误:commands.Bot的命令函数第一个参数应为Context对象(通常命名为ctx),原代码误用message导致message.message的错误写法。
  3. 旧API方法弃用:bot.join_voice_channel()方法已被移除,需改用语音频道对象的connect()方法。

修正后的join命令代码

@bot.command(name="join")
async def join(ctx):
    # 检查用户是否处于语音频道
    if not ctx.author.voice:
        await ctx.send("请先加入一个语音频道!")
        return
    # 获取用户所在语音频道
    channel = ctx.author.voice.channel
    # 连接到目标频道
    await channel.connect()

二、Application command 'say' not found错误说明

该错误是因为有人尝试调用机器人未注册的斜杠命令/say,你的代码中未定义此应用命令。若不需要该命令,此报错可直接忽略;若需添加,可补充以下代码:

@bot.tree.command(name="say", description="让机器人重复你的消息")
async def say(interaction: discord.Interaction, content: str):
    await interaction.response.send_message(content)

注意:添加斜杠命令后需重启机器人,且Discord服务器同步命令可能需要几分钟时间。

完整修正后的机器人代码

import discord
from discord.ext import commands
import decouple as dec

intents = discord.Intents.default()
intents.message_content = True
# 语音操作需开启voice_states意图
intents.voice_states = True

bot = commands.Bot(command_prefix='gg!', intents=intents)

@bot.event
async def on_ready():
    print('ONLINE!')
    # 同步斜杠命令(首次添加/修改时执行,建议仅运行一次)
    await bot.tree.sync()
    print("斜杠命令已同步")

@bot.command(name="join")
async def join(ctx):
    if not ctx.author.voice:
        await ctx.send("请先加入一个语音频道!")
        return
    channel = ctx.author.voice.channel
    await channel.connect()

# 可选:添加say斜杠命令
@bot.tree.command(name="say", description="让机器人重复你的消息")
async def say(interaction: discord.Interaction, content: str):
    await interaction.response.send_message(content)

# 安全读取token
TOKEN = dec.config("key")
bot.run(TOKEN)

额外提示:确保discord.py为最新版本,可执行pip install --upgrade discord.py更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 19:27:37