有几种方法可以做到这一点
来自用户 ID
@client.command()
async def DM(ctx, id: int, *, content):
user = client.get_user(id)
await user.send(content)
# Invoking
# $DM 123123123123123 something here
来自用户名#discriminator
@client.command()
async def DM(ctx, username, *, content):
name, discriminator = username.split("#")
user = discord.utils.get(ctx.guild.members, name=name, discriminator=discriminator)
await user.send(content)
# Invoking
# $DM example#1111 something here
带有用户提及、ID、姓名、昵称(最好的选择 imo)
@client.command()
async def DM(ctx, member: discord.Member, *, content):
await member.send(content)
# Invoking
# $DM @example#1111 something here
# $DM 1231231231231 something here
# $DM example#1111 something here
还要确保您启用了intents.members,有关更多信息,请查看我的previous answers之一
PS:您需要使用 commands.Bot 才能使所有这些工作,而不是 discord.Client