首页 > 解决方案 > Move all members to channel command using a foor loop discord.py

问题描述

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

I want the command to be used where the executor can go into a channel and use '.move (name of channel) and then it will move all the members in that channel to the (name of channel). One of the errors I'm getting is that it's ignoring spaces, so if there's a space in the name of the voice channel, it will only include the word before the space. And also I get this: Command raised an exception: AttributeError: 'Member' object has no attribute 'voice_channel'. Can somebody help me?

标签: pythonpython-3.xdiscord.py

解决方案


You can use keyword only arguments to process the rest of the message as a single argument. You also need to access the callers voice channel through 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)

推荐阅读