An Interest In:
Web News this Week
- April 14, 2024
- April 13, 2024
- April 12, 2024
- April 11, 2024
- April 10, 2024
- April 9, 2024
- April 8, 2024
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 discordclient = discord.Client()
We next need to add our on_member_join
event, and tell the bot to send an embed in the channel:
import discordclient = discord.Client()@client.eventasync 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 discordintents = discord.Intents.default()intents.members=Trueclient = discord.Client(intents=intents)@client.eventasync 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!
Original Link: https://dev.to/mikeywastaken/discord-py-project-2-welcomer-46p7
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To