【问题标题】:How can I move specific User to specific channels?如何将特定用户移动到特定频道?
【发布时间】:2020-12-11 04:42:34
【问题描述】:

我正在制作一个不和谐的机器人,只是为了玩 python。我尝试通过命令将人们转移到特定频道,但它不起作用。有人知道这是如何工作的吗?这是我的代码:

@bot.command()
async def move(ctx, user : discord.Member, channelname):
    channel = discord.utils.get(ctx.guild.channels, name=channelname)
    channel_id = channel.id
    user_id = user.id
    channel = client.get_channel(channel_id)
    member = client.get_member(user_id)
    await member.move_to(channel)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你的代码有一些问题,你得到一个discord.VoiceChannel对象,然后在client.get_channel又得到它,目的是什么?与用户相同,参数user 已经是discord.Member 对象,无需再次获取。这是你的固定代码

    @bot.command()
    async def move(ctx, member: discord.Member, *, channel_name):
        if member.voice is None:
            # Exiting if the user isn't connected to any voice channel
            return await ctx.send('User must be in a voice channel')
    
        # Getting the channel
        channel = discord.utils.get(ctx.guild.voice_channels, name=channel_name)
        if channel is None:
            return await ctx.send('Invalid channel name')
        
        # Moving the user
        await member.move_to(channel)
    

    还要确保启用intents.members 以及默认意图

    【讨论】:

    • 肯定包括检查。你永远不能假设用户正在做你期望他们做的事情。
    猜你喜欢
    • 2021-07-28
    • 2018-07-30
    • 1970-01-01
    • 2021-02-06
    • 2012-12-07
    • 2017-03-18
    • 1970-01-01
    • 2019-12-12
    • 2015-01-10
    相关资源
    最近更新 更多