使用 foor 循环 discord.py 将所有成员移动到通道命令


@bot.command()
async def move(ctx, channel : discord.VoiceChannel):
for members in ctx.author.voice_channel:
await members.move_to(channel)

我希望在执行器可以进入频道并使用'.move (name of channel)的地方使用该命令,然后将该频道中的所有成员移动到(name of channel)。我得到的错误之一是它忽略了空格,所以如果语音通道的名称中有空格,它只会在空格之前包含单词。我也得到这个:Command raised an exception: AttributeError: 'Member' object has no attribute 'voice_channel'.有人可以帮助我吗?

您需要引用带有空格的参数作为命令的位置参数。
或者,您可以使用命令的仅关键字参数来使用其余输入。

正如异常消息告诉您的那样,Member对象没有voice_channel属性。
您需要改用Member.voice属性来获取该用户/成员的VoiceState对象。然后,您可以将VoiceState.channel用于用户/成员连接到的VoiceChannel

另请注意,您无法为连接到该语音通道的成员迭代VoiceChannel自身。您需要改用VoiceChannel.members属性。

您可以使用仅关键字参数将消息的其余部分作为单个参数进行处理。 您还需要通过ctx.author.voice.channel访问呼叫者语音通道

from discord.ext.commands import check
def in_voice_channel():  # check to make sure ctx.author.voice.channel exists
def predicate(ctx):
return ctx.author.voice and ctx.author.voice.channel
return check(predicate)
@in_voice_channel()
@bot.command()
async def move(ctx, *, channel : discord.VoiceChannel):
for members in ctx.author.voice.channel.members:
await members.move_to(channel)

最新更新