discord 1.0.1 discord.py 1.5.1 在语音频道中获取成员的问题

时间:2021-01-18 13:45:20

标签: discord.py

目前我有这个,但它不起作用

@client.command(pass_context = True)
async def vcmembers(ctx, voice_channel_id):
    #First getting the voice channel object
    voice_channel = discord.utils.get(ctx.message.server.channels, id = voice_channel_id)
    if not voice_channel:
        return await client.say("That is not a valid voice channel.")

    members = voice_channel.voice_members
    member_names = '\n'.join([x.name for x in members])

    embed = discord.Embed(title = "{} member(s) in {}".format(len(members), voice_channel.name),
                          description = member_names,
                          color=discord.Color.blue())

    return await client.say(embed = embed)

它让我无法弄清楚:discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'Message'对象没有属性'server'

1 个答案:

答案 0 :(得分:0)

  1. server 属性已重命名为 guild,(v.1.0.0 中的 iirc)
  2. 没有像 client.say 这样的东西了,它是 Messageable.send
  3. VoiceChannel.voice_members 它也不再是一个东西,重命名为 VoiceChannel.members
  4. 您可以使用 VoiceChannelConverter 立即获取 VoiceChannel 实例,无需 utils.get

您的代码已修复:

@client.command(pass_context = True)
async def vcmembers(ctx, voice_channel: discord.VoiceChannel): 
    # The `voice_channel` var will be already a `discord.VoiceChannel` instance,
    # `commands.ChannelNotFound` will be raised if it's not found

    members = voice_channel.members
    member_names = '\n'.join([x.name for x in members])

    embed = discord.Embed(
        title=f"{len(members)} member(s) in {voice_channel.name}",
        description=member_names, colour=discord.Colour.blue()
    )

    await ctx.send(embed=embed)

注意:如果您不知道如何查看我的答案 here,您应该启用 intents.membersintents.voice_states。此外,我假设您使用的是 discord.py 版本 1.5+

参考: