DEV Community

terabyte.
terabyte.

Posted on

discord.py Project 2: Welcomer πŸ‘‹πŸ½

In this article, you're going to learn how to make a welcoming bot!

It will send a welcome message to a specified channel with the user's avatar and name!


To start, we need to initialize our bot. For this walkthrough, we can use discord.Client instead of commands.Bot, because we aren't going to have any commands.

import discord

client = discord.Client()
Enter fullscreen mode Exit fullscreen mode

We next need to add our on_member_join event, and tell the bot to send an embed in the channel:

import discord

client = discord.Client()

@client.event
async def on_message_join(member):
    channel = client.get_channel(channel_id)
    embed=discord.Embed(title=f"Welcome {member.name}", description=f"Thanks for joining {member.guild.name}!") # F-Strings!
    embed.set_thumbnail(url=member.avatar_url) # Set the embed's thumbnail to the member's avatar image!

    await channel.send(embed=embed)
Enter fullscreen mode Exit fullscreen mode

Now run the code, grab an alt account or ask a friend to join the server, and boom!

But wait... It doesn't work!
That's because we need the members intent enabled!

After you've enabled the Privileged Intents in the dashboard, we need to enable them in the code!

import discord

intents = discord.Intents.default()
intents.members=True
client = discord.Client(intents=intents)

@client.event
async def on_message_join(member):
    channel = client.get_channel(channel_id)
    embed=discord.Embed(title=f"Welcome {member.name}", description=f"Thanks for joining {member.guild.name}!") # F-Strings!
    embed.set_thumbnail(url=member.avatar_url) # Set the embed's thumbnail to the member's avatar image!
    await channel.send(embed=embed)
Enter fullscreen mode Exit fullscreen mode

What you've just done is told the discord.Client to ask for the Members intent when it starts up. Now it will be able to see member events.

Now run the code to see the bot welcome the user!


Have Questions? Have a suggestion about what to do for the next post?

Tell me in the comments and I'll reply as soon as possible!

If you found this useful, make sure to like the post and share it with a friend! You can also follow me to get my content in your feed as soon as it's released!

Top comments (0)