首页 > 解决方案 > 尝试 dm 禁止用户时引发禁止异常 (Discord.py)

问题描述

当我运行此代码时,它会引发 403 Forbidden 异常:无法向该用户发送消息。我怎样才能绕过那个等 dm 禁止和踢用户?

if user.dm_channel == None:
    await user.create_dm()
await user.dm_channel.send(
    content=f"You have been kicked from {server} by <@{message.author.id}> (reason : {splited[2]})")

标签: pythondiscord.pydm

解决方案


在踢他们之前将消息发送到用户的 DM 频道。

@client.command()
async def kick(ctx, user: discord.Member, *, reason=None):
  if user.dm_channel == None:
      await user.create_dm()
  await user.dm_channel.send(
      content=f"You have been kicked from {ctx.guild} by {ctx.message.author}\nReason: {reason} ")
  await user.kick(reason=reason)

推荐阅读