当我踢他们时,如何让 Discord bot DM 成为某人

How can I make a Discord bot DM someone when I kick them

我正在尝试让我的 discord 机器人 DM 在我踢他们时成为一个人。我尝试了一些东西,但最终没有任何效果。这是当前的踢码:

@bot.command(name="kick", aliases=["k"], help= "Kicks specified user from the server.")
async def kick(ctx, member: discord.Member, *, reason=None):
  await member.kick(reason=reason)
  await ctx.send(f'User {member} has been kicked.')
  embed=discord.Embed(
    title= f"{bot.user} kicked {member}",
    color=bot.embed_color,
    timestamp = datetime.datetime.now(datetime.timezone.utc)
  )
  embed.set_author(
    name = ctx.author.name,
    icon_url = ctx.author.avatar_url
  )
  embed.set_footer(
  text = bot.footer,
  icon_url = bot.footerimg
  )
  await bot.debug_channel.send(embed = embed)
  print("Sent and embed")
  await ctx.message.add_reaction('✅')
  print("Added a reaction to a message")

有谁知道如何让机器人发送 DM 吗?

您必须向被踢用户发送消息。

既然你已经有了成员对象,你就可以await member.send("What you want to send")

此外,消息应该在 await member.kick() 之前,因为机器人将无法向人们发送消息说他们没有共同的服务器。

您应该做的另一件事是在向成员发送消息之前检查用户是否可踢。

不能踢用户的一些情况是:

  1. 他们比不和谐的机器人有更高的作用。 (可以通过比较member.top_role和bots top角色来解决)
  2. 他们没有踢球的权限。 (可以通过检查机器人公会权限来解决)

另一件事是,您还应该创建一个 try except,以捕获在尝试向用户发送消息时可能发生的任何错误。一些用户关闭了 DM,因此在尝试向该成员发送消息时会出现错误。