0

How can I send messages to the users after they got kicked from servers? Whenever I try the following code, it doesn't work properly.

import discord
from discord.ext import commands  

intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True,presences=True,guild_messages=True)

client = commands.Bot(command_prefix="!dc ", intents=intents)

@client.event
async def on_ready():  
    print("I am ready!")

@client.command(aliases=["ban"])
@commands.has_role("admin")

async def ban_user(self,ctx, member: discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f"{member} has been kicked from server.")
    dm_channel = await create_dm(member)  #These two code lines are where I got this error
    await dm_channel.send("You've been banned from the server.You won't join the server until admin opens your ban.")
        
@commands.command(aliases=["kick"])
@commands.has_role("admin")

async def kick_user(self,ctx, member: discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"{member} has been kicked from the server.")

client.run(myToken)
Ruli
  • 2,592
  • 12
  • 30
  • 40
hARASİVa
  • 15
  • 5
  • where is your create_dm function? – Ahmet Burak Sep 08 '21 at 19:56
  • @AhmetBurak It is in the ban_user method.You can see,next to it,there is a comment which is '#These two code lines are where I got this error' – hARASİVa Sep 08 '21 at 19:58
  • 1
    I saw that, I mean where you defined that function (probably not defined anywhere). Do you have a reference code? – Ahmet Burak Sep 08 '21 at 20:00
  • @AhmetBurak ohh,okey I got your point.I use discord.py which is a module that help us to create discord bots.So,that create_dm funciton is defined in that module.But I couldn't figure out how to solve this problem – hARASİVa Sep 08 '21 at 20:09
  • Just use `await member.send("Your ban message")` **BEFORE** you ban the member instead of `await dm_channel.send()`. You also use `self` just in cogs. If you do not have this in a `cog` get rid of `self` and just say `(ctx, ..)` – Dominik Sep 08 '21 at 20:12
  • OK I didn't use discordpy, check this https://stackoverflow.com/a/57340966/12306993 – Ahmet Burak Sep 08 '21 at 20:14
  • @Dominik Thanks dude it worked.By the way,I was using cogs,so there is a **self**.If you don't mind I want to know,why it worked because When I use **await member.send("Your ban message")** after I ban the member,it didn't work but your solution worked.Can you explain it? – hARASİVa Sep 08 '21 at 20:20
  • @AhmetBurak Thank you,adamsın :) – hARASİVa Sep 08 '21 at 20:21
  • @Dominik thank you so much :)I got it – hARASİVa Sep 08 '21 at 20:25
  • @hARASİVa Once you ban the member, the bot will most likely no longer share a server with them so you have to send the message before the ban so that the bot can still either create a private message or send a message via `discord.member`. (*Corrected version of the comment before!*) - Happy coding! :) – Dominik Sep 08 '21 at 20:26
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough . When something isn't working, "it doesn't work properly" is *not enough information*, and neither is a rough quote from an error message (which also makes a poor title for the question). – Karl Knechtel Sep 09 '21 at 03:15

2 Answers2

1

According to your question, I am assuming that you want to dm the user who was banned. To do this you can,

async def ban(self, ctx, member: discord.Member, *, reason=None):
    await ctx.send(f'{member} has banned from the server.') # sends the message in the server
    await member.send(f'You have been banned from {member.guild.name}.') #dms the member that he has been banned.
    await member.ban(reason = reason) #bans the user from the server.

Note: If you first ban the member, and then try to send the message in the server and dm, you will get an error as the member would not be found.

Nishil Sheth
  • 155
  • 1
  • 10
-1

You can use the DMChannel function, the code would look like this:

# put this on top:
from discord import DMChannel

# some stuff

async def ban_user(self, ctx, member: discord.Member=None, *, reason=None):
    if member is None: 

        # if the user don't inform a name to ban, returns this message:
        return await ctx.send(f'You need to inform which member you want to ban')
    
    # bans the user from the server:
    await member.ban(reason = reason)

    # sends the message in the server:
    await ctx.send(f'{member} has banned from the server.') 

    # dm the member 
    await DMChannel.send(member, f'You have been banned from {member.guild.name}.')