Wafelack
Wafelack

Reputation: 13

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

Upvotes: 0

Views: 959

Answers (3)

Jawad
Jawad

Reputation: 2041

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)

Upvotes: 1

okay
okay

Reputation: 169

Try sending a message right before banning the user.

Upvotes: 1

stijndcl
stijndcl

Reputation: 5647

You can't. Discord bots are only able to send DM's to users they share at least one server with, so if someone was kicked/banned from the only mutual server, your bot will not be able to send them any messages.

Upvotes: 0

Related Questions