首页 > 解决方案 > Discord.py - 'VoiceState' 对象没有属性 'voice_channel'

问题描述

每个人。我正在编写一个 Discord 机器人,用于播放声音。但我面临一个问题。

    channel = ctx.message.author.voice.voice_channel
AttributeError: 'VoiceState' object has no attribute 'voice_channel'

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'voice_channel

有人可以帮我吗?感谢您的任何评论。

功能:

import asyncio
import discord, time
from discord.ext import commands
from discord.voice_client import VoiceClient

@bot.command(pass_context=True)
async def bb(ctx):
    user = ctx.message.author
    channel = ctx.message.author.voice.voice_channel
    await bot.join_voice_channel(channel)
    player = voice.create_ffmpeg_player('1.m4a')
    player.start()

标签: pythondiscord.py

解决方案


在重写版本中,版本 1.0VoiceState.voice_channel更改为VoiceState.channel.

如果您使用的是重写版本,则以下内容应该足以播放文件:

from discord import FFmpegPCMAudio
from discord.utils import get

@bot.command()
async def bb(ctx):
    channel = ctx.message.author.voice.channel
    if not channel:
        await ctx.send("You are not connected to a voice channel")
        return
    voice = get(bot.voice_clients, guild=ctx.guild)
    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
    source = FFmpegPCMAudio('1.m4a')
    player = voice.play(source)

推荐阅读