0

Hey so this is my code and I want that the bot dms the member with 'test'

@has_permissions(kick_members=True)
@client.command()
async def kick(ctx, member: discord.Member, *, reason=None):
    embed_kick = discord.Embed(title=f'Kick command usage')

    embed_kick.set_footer(text=f'VexArtz Community Discord')

    embed_kick.add_field(name=f'Kicked user', value=f'{member}', inline=True)
    embed_kick.add_field(name=f'Reason', value=f'-{reason}', inline=True)
    embed_kick.add_field(name=f'Author', value=f'{ctx.author.mention}', inline=True)
    await member.kick(reason=reason)
    await ctx.channel.purge(limit=1)
    log_channel = client.get_channel(858716439849861121)
    await log_channel.send(embed=embed_kick)
VexArtz
  • 1
  • 1
  • Does this answer your question? [Discord.py - How do I send private message to someone using the person's Id?](https://stackoverflow.com/questions/54418496/discord-py-how-do-i-send-private-message-to-someone-using-the-persons-id) – futur Jun 28 '21 at 15:12
  • What is wrong with the code that you have already? Is it producing the wrong result? – Tom Johnson Jun 29 '21 at 01:05

2 Answers2

1
  • You can use fetch_user method to get a member.
  • create_dm Creates a DMChannel with user. This should be rarely called, as this is done transparently for most people.

Well, now that we have taken the member and created a DM Channel object, we can now send a message to the member or the same user with this object.

@has_permissions(kick_members=True)
@client.command()
async def kick(ctx, member: discord.Member, *, reason=None):
    embed_kick = discord.Embed(title=f'Kick command usage')

    embed_kick.set_footer(text=f'VexArtz Community Discord')

    embed_kick.add_field(name=f'Kicked user', value=f'{member}', inline=True)
    embed_kick.add_field(name=f'Reason', value=f'-{reason}', inline=True)
    embed_kick.add_field(name=f'Author', value=f'{ctx.author.mention}', inline=True)
    
    user = await client.fetch_user(member.id) #Fetch the user with its id
    dm = await user.create_dm() #If dm is already made, it does not matter :)
    await dm.send("test")
    
    await member.kick(reason=reason)

    await ctx.channel.purge(limit=1)
    log_channel = client.get_channel(858716439849861121)
    await log_channel.send(embed=embed_kick)

We could use the same member.send(), but errors may occur.

Such as:

HTTPException – Sending the message failed.

Forbidden – You do not have the proper permissions to send the message.

ShoK
  • 43
  • 4
0

You can send a message to a user via member.send(). I guess you want to send a message to the user being kicked? In that case you code could look something like this


@has_permissions(kick_members=True)
@client.command()
async def kick(ctx, member: discord.Member, *, reason=None):
    embed_kick = discord.Embed(title=f'Kick command usage')

    embed_kick.set_footer(text=f'VexArtz Community Discord')

    embed_kick.add_field(name=f'Kicked user', value=f'{member}', inline=True)
    embed_kick.add_field(name=f'Reason', value=f'-{reason}', inline=True)
    embed_kick.add_field(name=f'Author', value=f'{ctx.author.mention}', inline=True)
   
    await member.send(content = "test") # send message to member being kicked

    await member.kick(reason=reason)

    await ctx.channel.purge(limit=1)
    log_channel = client.get_channel(858716439849861121)
    await log_channel.send(embed=embed_kick)
itzFlubby
  • 2,269
  • 1
  • 9
  • 31