如何将特定用户移至特定频道? [英] How can I move specific User to specific channels?

查看:39
本文介绍了如何将特定用户移至特定频道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个不和谐的bot,只是为了与python一起玩.我尝试通过命令将人员转移到特定渠道,但这是行不通的.有人知道这是如何工作的吗?这是我的代码:

I am making a discord bot just for fun with python. I try to move people via command to a specific channel, but it doesn't work. Do someone have a clue how this works? Here my code:

@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)

推荐答案

您的代码中有一些错误,您得到了 discord.VoiceChannel 对象,然后在 client中.get_channel 再次获取,目的是什么?与用户相同,参数 user 已经是 discord.Member 对象,无需再次获取.这是您的固定代码

There's a few things wrong in your code, you're getting a discord.VoiceChannel object, and then in client.get_channel getting it again, what's the purpose? Same with the user, the argument user is already discord.Member object, no need getting it again. Here's your fixed code

@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 和默认意图

Also make sure to enable intents.members as well as default intents

这篇关于如何将特定用户移至特定频道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆