【问题标题】:Im using Replit to create a simple music bot on discord.py yet it keeps giving me an error我使用 Replit 在 discord.py 上创建了一个简单的音乐机器人,但它一直给我一个错误
【发布时间】:2021-04-07 08:44:46
【问题描述】:

代码

voiceChannel = discord.utils.get(ctx.guild.voice_channels,name='CBT')
voice = discord.utils.get(client.voice_clients,  guild=ctx.guild)
await voiceChannel.connect()
      
ydl_opts = {    
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key':'FFmpegExtractAudio',
        'preferredcodec':'mp3',
        'preferredquality':'192',
    }],
}
    
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
        os.rename(file, "song.mp3")

我认为这给了我一个问题

voice.play(discord.FFmpegPCMAudio('song.mp3'))

错误信息:

AttributeError: 'NoneType' object has no attribute 'play'

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    错误只是意味着discord.utils.get() 调用正在返回NoneClient.voice_clients 属性是VoiceProtocol 对象的列表。 (不过,实际的语音连接是VoiceClient 对象。)ctx.guild 属性(Guild 对象)是与Client.voice_clients 的项目不同的数据类型,因此discord.utils.get() 将始终返回None

    我认为您必须使用for 循环来遍历每个语音连接并检查公会是否匹配:

    connection = None
    for vconn in client.voice_clients:
        if vconn.guild == ctx.guild:
            connection = vconn
            break
    
    assert connection is not None
    

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2021-11-26
      • 1970-01-01
      • 2021-08-05
      • 2021-09-30
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多