I'm pretty new to discord bots so i wondered if someone here could help me, I wrote a code for kicking and i want to add something that DM's the person before he gets kicked but after the !kick command was given.
#bot.command()
async def kick(ctx, member: discord.Member=None):
if not member:
await ctx.send('Please mention a member')
return
await member.kick()
await ctx.send(f'{member.display_name}\'s was kicked from the server')
You just need to create a dm channel and send the message.
Try adding the two lines below:
#bot.command()
async def kick(ctx, member: discord.Member = None):
if member is None:
await ctx.send(f'{ctx.author.mention} Please mention a member')
return
channel = await member.create_dm() # line 1 create channel
await channel.send('You are getting kicked') # line 2 send the message
await member.kick()
await ctx.send(f'{member.display_name}\'s was kicked from the server')
Related
Soo i want my bot to send a message when someone join but it is not workin
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
and
i want my bot to take a massage from me and send it to the guy i say but this is not workin too
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
and
here is my WHOLE code:
import os, discord,asyncio
import keep_alive
from discord.ext import commands
client = commands.Bot(command_prefix="!")
token = os.environ.get('Token')
GUILD = os.environ.get('Guild')
#client.event
async def on_ready():
print(f'{client.user} is connected')
#client.command()
async def dm(ctx, member: discord.Member):
await ctx.send('what do u want to say bitch!')
def check(m):
return m.author.id == ctx.author.id
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
keep_alive.keep_alive()
client.run(token)
For making a welcome channel, it is safer to use get_channel instead of get. Because in your code, every time you rename your channel, you need to change your code too, but channel ids cannot be changed until if you delete and create another one with the same name.
Code:
#client.event
async def on_member_join(member):
channel = client.get_channel(YOUR_CHANNEL_ID_GOES_HERE)
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
As for the dm command, I recommend you to get your message as a function parameter. Also you can check when you're DM'ing your bot with the isinstance() function. There is a * before the message parameter though. Its purpose is collecting all of your messages with or without spacing.
Code:
#client.command()
async def dm(ctx, member:discord.Member,*, message):
if isinstance(ctx.channel,discord.DMChannel):
await member.send(f'{ctx.member.mention} has a message for you: \n {message}')
#commands.command()
#commands.guild_only()
#commands.has_guild_permissions(ban_members=True)
async def ban(self, ctx, member: discord.Member, *, reason=None):
if member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only ban members below your role.")
return
channel = self.bot.get_channel(864083000211406849)
embed = discord.Embed(title=f"{ctx.author.name} banned: {member.name}", description=reason)
await channel.send(embed=embed)
await ctx.send(embed=embed)
await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
await ctx.guild.ban(user=member, reason=reason)
#ban.error
async def ban_error(self,ctx, error):
if isinstance(error, commands.CheckFailure):
title=f"{ctx.author.mention} you do not have the required permission(s)/role(s) to run this command!"
await ctx.send(title)
So I want my code to basically kick a member that has left my server using their ID. Ive looked in to previous questions but it seems to be no help for me.
You are converting the member input to a discord.Member, which needs to be in the server. Instead convert it to a discord.User, who does not need to be in your server.
async def ban(self, ctx, member: discord.User, *, reason=None):
Please keep in mind that your top role lookup and comparison will not work when you use the discord.User, you will have to get the actual member beforehand, like this:
server_member = ctx.guild.get_member(member.id)
if server_member != None:
if server_member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only ban members below your role.")
return
Also you will get an error trying to message someone who does not share a server with your bot, or has blocked your bot etc., I would just use a try/except block for this:
try:
await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
except discord.errors.Forbidden:
pass
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)
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.
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}.')
When I run my kick/ban command, I want it to send an embed to the channel the command is executed in to announce who has been banned. I have it in the code, but it doesn't post when the user is kicked. How would I fix this?
#commands.has_permissions(kick_members=True)
async def kick(context, member : discord.Member, *, reason=None):
# Conditions
if reason == None:
await context.channel.send("**``Please provide a reason for why this member should be kicked!``**", delete_after=3)
else:
# Await Kick
await member.kick(reason=reason)
# Send Embed in Server
myEmbed = discord.Embed(title="CRYPTIC Moderation", color=0x000000)
myEmbed.add_field(description=f'{member.mention} has been successfully kicked for: **``{reason}``**!')
myEmbed.set_footer(icon_url=context.author.avatar_url, text=f'Invoked by {context.message.author}')
await context.message.channel.send(embed=myEmbed)
# DM Kicked User
if member.dm_channel == None:
await member.create_dm()
await member.dm_channel.send(
content=f"You have been kicked from **``{context.guild}``** by {context.message.author} for **``{reason}``**!"
) ```
The DM part works in both commands, but the embed doesn't work in either. Thank you.
The problem is, that instead of just adding a description to the embed, you add a field, but fields have name and value, and not description. So instead, set the description where you set the embed title:
##bot.command or something similar is missing here. Copy and paste error?
#commands.has_permissions(kick_members=True)
async def kick(context, member : discord.Member, *, reason=None):
# Conditions
if reason == None:
await context.channel.send("**``Please provide a reason for why this member should be kicked!``**", delete_after=3)
else:
# Await Kick
await member.kick(reason=reason)
# Send Embed in Server
myEmbed = discord.Embed(title="CRYPTIC Moderation", color=0x000000, description=f'{member.mention} has been successfully kicked for: **``{reason}``**!') #add the description where you add the title
myEmbed.set_footer(icon_url=context.author.avatar_url, text=f'Invoked by {context.message.author}')
await context.message.channel.send(embed=myEmbed)
# DM Kicked User
if member.dm_channel == None:
await member.create_dm()
await member.dm_channel.send(
content=f"You have been kicked from **``{context.guild}``** by {context.message.author} for **``{reason}``**!"
) ```
#client.event
async def on_member_leave(member: discord.Member):
for message in client.text_channels:
if message.author == member:
await message.delete()
Thats what I have so far, but it isn't really working.
I have no idea how to execute it.
The easiest way would be to ban them and immediately unban them, clearing all of their messages.
#client.event
async def on_member_remove(member: discord.Member):
await member.ban()
await member.unban()