如果猜测错误,则断开用户与频道的连接 DiscordPy

问题描述

我正在编写一个 discord 机器人。我做了这个命令,它用一个从 1 到 10 的随机整数一个小猜谜游戏。如果用户的猜测是正确的,只会弹出一条消息。如果不是,它会断开用户与语音通道的连接(如果在一个通道中)。

这是我正在处理的代码

@bot.command()
async def adivinar(ctx: commands.Context):
    user = discord.Member
    aleatorio = random.randint(1,10)
    await ctx.send(f"Guess a number from 1 to 10")

    msg = await bot.wait_for("message")

    if int(msg.content) == aleatorio:
        await ctx.send(f"Congrats! My number was {aleatorio}")
    else:
        await ctx.send(f"nope. My number was {aleatorio}")
        await user.move_to(None)

代码不起作用。它在终端中显示错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: move_to() missing 1 required positional argument: 'channel'

为了制作“adivinar”命令,我查看了这段代码作为参考,效果很好:

@bot.command()
async def kick1(ctx: commands.Context,user: discord.Member):
    await ctx.send(f'{user.mention} has been kicked from {user.voice.channel.mention}')
    await user.move_to(None)

我很确定这很容易解决。但目前我很难弄清楚。谢谢!

解决方法

您将用户分配给了 discord.Member 类,而不是真正的成员

user = discord.Member

你需要做一些像⬇这样的事情才能移动它

user = ctx.guild.get_member(1234)  # user id

在您的情况下,您也可以使用 user = msg.author