python - Discord.py - 'VoiceState' 对象没有属性 'voice_channel'

标签 python discord.py

大家。我正在写一个 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()

最佳答案

在重写版本 1.0 中,VoiceState.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)

关于python - Discord.py - 'VoiceState' 对象没有属性 'voice_channel',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55321681/

相关文章:

python - python ;异步处理错误我需要一个单独的线程吗?

python - PyInstaller 无法通过 pip 安装

python - 使用 itertools.tee 检查下一个元素时如何最小化空间成本?

python - 如何在on_message中检查用户是否具有特定角色?

python - 使用 MS C 与 MinGW 编译 Python 的速度差异

python - 检测两张图片之间的变化

python - 如何在 replit 上运行音乐机器人 (discord.py)

python - 如何更改discord.py中的 channel 权限?

linux - 如何在 linux 的 discord.py 中创建重启命令?即使它需要其他文件,如 shell 之类的

python - 是否可以使用前缀和 ping 来执行命令?