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 ins…


This content originally appeared on DEV Community and was authored by Mikey

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()

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)

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)

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!


This content originally appeared on DEV Community and was authored by Mikey


Print Share Comment Cite Upload Translate Updates
APA

Mikey | Sciencx (2021-03-17T14:12:37+00:00) discord.py Project 2: Welcomer ??. Retrieved from https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/

MLA
" » discord.py Project 2: Welcomer ??." Mikey | Sciencx - Wednesday March 17, 2021, https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/
HARVARD
Mikey | Sciencx Wednesday March 17, 2021 » discord.py Project 2: Welcomer ??., viewed ,<https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/>
VANCOUVER
Mikey | Sciencx - » discord.py Project 2: Welcomer ??. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/
CHICAGO
" » discord.py Project 2: Welcomer ??." Mikey | Sciencx - Accessed . https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/
IEEE
" » discord.py Project 2: Welcomer ??." Mikey | Sciencx [Online]. Available: https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/. [Accessed: ]
rf:citation
» discord.py Project 2: Welcomer ?? | Mikey | Sciencx | https://www.scien.cx/2021/03/17/discord-py-project-2-welcomer-%f0%9f%91%8b%f0%9f%8f%bd/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.