Invite command in discord.py (Invite user to server by ID)

Invite command in discord.py (Invite user to server by ID)

如标​​题所示,我想向我的机器人添加一个命令,该命令将向某人发送 DM 邀请到服务器。使用永久性的静态 link 不是一种选择,因为我希望该命令在不同的服务器上可用。

这是我目前拥有的:

@BSL.command(pass_context = True)
async def invite(ctx, userToInvite):
        inviteLinq = await BSL.create_invite(destination = ctx.message.channel, xkcd = True, max_uses = 1)
        await BSL.send_message(userToInvite, inviteLinq)

但是,我得到一个错误 InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received str。我了解到这是因为discord中的消息被保存为字符串。

有没有办法让您可以指定用户的 ID,并且他们会在 DM 中收到邀请link?

你的userToInvite是一个字符串,但它应该是一个User对象。您应该使用 Server.get_member_named 从服务器获取 Member 对象。像

@BSL.command(pass_context = True)
async def invite(ctx, userToInvite):
        inviteLinq = await BSL.create_invite(destination = ctx.message.channel, xkcd = True, max_uses = 1)
        target_member = await ctx.message.server.get_member_named(userToInvite)
        await BSL.send_message(target_member, inviteLinq)

编辑:

如果您使用用户 ID 邀请用户访问此服务器,您应该改为尝试

@BSL.command(pass_context = True)
async def invite(ctx, userToInvite):
        inviteLinq = await BSL.create_invite(destination = ctx.message.server, xkcd = True, max_uses = 1)
        target_user = await BSL.get_user_info(userToInvite)
        await BSL.send_message(target_member, inviteLinq)