How to Make A Discord Bot - Part 4
Hey, everyone we are back with another Discord Bot Tutorial and judging from the number of upvotes I got for Part 3 Compared to Part 1, I decided to show you all today some really cool things.
What is an embed?
It is a special and more cool way to display text, watch this.
The cooler one is called an embed.
Using the help function example we did last tutorial, let's turn that boring text into some cool box!!!
Lets look back at what we did last tutorial:
@client.command() async def help(ctx): ##Define Embed Here await ctx.send("..........")
Now instead of using ctx.send, we will have to send an embed instead, so do this
So instead of passing in some text, we are passing in an embed with the name embed.
It's a good practice to give your embed a good name, but if you see later on, it gets kinda annoying.
So now we need to define our embed, and here's where it gets abit CSSsy if you get it.
@client.command() async def help(ctx): embed = discord.Embed( title = 'Help', description = "Try out these commmands below", colour = discord.Colour.orange() ) #Embed Elements await ctx.send("..........")
We will have to initialize it as a embed object, by using discord.Embed (Capital 'E').
Next, we will have to give it 3 Main Things: Title, Description and Colour
The colour is the line on the left side of the embed.
Do note that the colour of the embed can only be assigned by a discord.Colour Class (Capital 'C'). Most of the basic colours can be used by doing:
Remember to add your brackets at the start and end together with your commas after every line.
Author and Footer
embed.set_footer(text = "Made By @Fishball_Noodles. Bot Realeased Since June 01,2020") embed.set_author(name = "Moderation#2520")
As shown in the picture above, a field consist of 2 part the name and value.
The name is just the subtitle of the of that section, while the value is just the main text. You can add up to as many fields you want, by doing this
embed.add_field(name = "Date & Time", value = datetime.datetime.now())
As you can see, I used embed.add_field or embed.set_footer for adding elements to the embed. If your variable name was to be something like, help_embed, you would have to type help_embed.add_field and trust me on this when you want to send bigger embeds you wouldn't want to do this.
An example of how big my embed is:
Sneak Peek on Moderation 2.0
Ok, so you don't want random people who join your server to just clear the chat using the |clear function we talked about in Part 2. Maybe only those with the Moderator Role can do it, sure. So how we are gonna do it is making use of a role check built in function that will check the user's role before the code is being run.
@client.command() @commands.has_role('Moderators') async def clear(ctx, amount = 10): await ctx.channel.purge(limit=amount+1)
This is very self-explanatory, @commands.has_role(#role_name)
where role_name is the name of the role required in stringed form.
Lastly Don't Forget to at the run command to get your bot online and working
Well done guys, if you stuck with me throughout this whole tutorial do let me know in the comments section below. Also if you encountered any problems during the entire process, feel free to ask it in the comment section. I will try to answer all your questions. Hope you found the embed portion especially useful, stay tuned for Part 5 tomorrow, where we will be covering on how to accept keyword arguments. Till next time:)