【问题标题】:Discord.py - 'Member' object has no attribute 'channel'Discord.py - 'Member' 对象没有属性 'channel'
【发布时间】:2021-03-06 02:43:42
【问题描述】:

我刚刚开始使用 discord.py,我正在尝试创建一个非常简单的机器人,可以播放本地存储中的 mp3 音频。所以基本上机器人应该加入一个语音频道并播放 mp3。这是我的代码:

import discord
import ffmpeg
from discord.ext import commands
import datetime

from urllib import parse, request
import re

bot = commands.Bot(command_prefix='$', description="This is a Helper Bot")

@bot.command(name="test")
async def test(ctx):
    # Gets voice channel of message author
    voice_channel = ctx.author.channel
    channel = None
    if voice_channel != None:
        channel = voice_channel.name
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(executable="C:/ffmpeg/bin/ffmpeg.exe", source=r"PATH"))
        # Sleep while audio is playing.
        while vc.is_playing():
            sleep(.1)
        await vc.disconnect()
    else:
        await ctx.send(str(ctx.author.name) + "is not in a channel.")
    # Delete command after the audio is done playing.
    await ctx.message.delete()

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))



bot.run('MYKEY')

我在 SO 上找到了 test 函数,但我在使用它时遇到了问题。当我从不和谐中调用它时,我收到以下错误:

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

我在这里做错了什么?

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

discord.Member 实例没有名为 channel 的属性。但是它确实有一个名为voice 的属性,它是Optional[VoiceState] 的一个实例,请参阅https://discordpy.readthedocs.io/en/latest/api.html?highlight=voice_state#discord.Member.voice

voice 对象有一个名为channel 的属性,它是Optional[VoiceChannel] 的一个实例,请参阅https://discordpy.readthedocs.io/en/latest/api.html?highlight=voice_state#discord.VoiceState.channel

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 2020-12-17
    • 2021-07-27
    • 2021-01-05
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多