pythondiscord.pydm

Raise forbidden exception when trying to dm a banned user (Discord.py)


When i run this code, it raises a 403 Forbidden exception : Cannot send messages to this user. How can i bypass that and so dm banned & kicked users ?

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]})")

Solution

  • Send the message to the user's DM channel before you kick them.

    @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)