Home > Mobile >  How do you make a bot that sends a welcome embed and deletes the embed after a few seconds in discor
How do you make a bot that sends a welcome embed and deletes the embed after a few seconds in discor

Time:07-06

Here's my code but it seems like it doesn't work. I'm so sorry but, I'm still a newbie but, I would very much appreciate your help and critics.

import discord
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
                      intents=discord.Intents.all())

@client.event
async def on_message_join(member):
    channel = client.get_channel(channelid)
    count = member.guild.member_count
    embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count) 
    embed.set_thumbnail(url=member.avatar_url) 

    await channel.send(embed=embed)
    time.sleep(5)
    message.delete(embed)

CodePudding user response:

The correct discord event to catch that a person joins your discord is:

async def on_member_join(member: discord.Member):

rather than on_message_join

To easily delete the message you can first make it a string:

msg = await channel.send(embed=embed)

then get it's id by:

msg_id = msg.id

then fetch it:

msg_todel = await channel.fetch_message(int(msg_id))

then delete it:

await msg_todel.delete()

CodePudding user response:

Just use delete_after=seconds, this exactly what's your wanted

import discord
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
                      intents=discord.Intents.all())

@client.event
async def on_message_join(member):
    channel = client.get_channel(channelid)
    count = member.guild.member_count
    embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count) 
    embed.set_thumbnail(url=member.avatar_url) 

    await channel.send(embed=embed, delete_after=5)

CodePudding user response:

According to the discord.py docs, you could edit the message object after 5 seconds and then just set the new embed parameter to None, this seems to be what you're after here.

Below is a way you can alter your code to do this.

import discord
import asyncio # You will need to import this as well
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
                      intents=discord.Intents.all())

@client.event
async def on_message_join(member):
    channel = client.get_channel(channelid)
    count = member.guild.member_count
    embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count) 
    embed.set_thumbnail(url=member.avatar_url) 

    message_object = await channel.send(embed=embed) # You can save the message sent by attaching it to a variable. You don't need to send more calls to refetch it.
    
    '''
    time.sleep(5) <--- You should not use time.sleep() as it will stop your entire bot from doing anything else during these 5 seconds.

    Use asyncio.sleep() as a better alternative. Does the same thing as time.sleep() but other functions of your bot will still function, this is called a  blocking function.
    '''

    asyncio.sleep(5)
    
    await message_object.edit(embed = None)

Unless you want the entire message deleted, then you can just use delete_after in order to obtain this.

import discord
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
                      intents=discord.Intents.all())

@client.event
async def on_message_join(member):
    channel = client.get_channel(channelid)
    count = member.guild.member_count
    embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count) 
    embed.set_thumbnail(url=member.avatar_url) 

    await channel.send(embed=embed, delete_after(5)) # Just add this parameter at the end.
  • Related