Client, you have access to a wide range of Discord APIs. In this lesson, you’ll start to use the API to send information to the console, including the name of your bot, the name of your server, and the server’s identification number.
This is part six, and here you’ll build on the
Client you created last time. By using a
Client, you have access to a very wide range of Discord APIs. For example, let’s say that you wanted to write the name and identifier of your guild that you’ve registered your bot user with to the console. Firstly, you will need to add a new environment variable to the
00:24 Keep in mind that any placeholders that you may have, or that may be in the code that you download as part of the course material, will need to be replaced with actual tokens or your actual server name.
Remember that Discord calls the
on_ready() method, which you used previously, once the
Client has made the connection and prepared the data, so you can rely on the guild data being available inside
So, what have we done here? After looping through the guild data that
on_ready() has stored for you, specifically
client.guilds, you find the guild with the matching name from your
.env file and print a
formatted string to
stdout (standard out). Please note at this point that while you can be reasonably confident that your bot is only connected to the one guild because you just created it, so,
client.guilds would be simpler than looping through and looking for a particular guild name, it is important to remember that a bot can be connected to multiple guilds.
I should point out at this time that whenever you need to rerun the code, you will need to manually stop the
bot.py script. In something like Thonny, this is achieved by going Run and Stop/Restart backend.
Just keep in mind that you will need to do that every time you want to rerun your
bot.py script. Now, if you run your program at this point, you can see what the results are. It’s running, it’s running, it’s running, it’s connected! So, as you can see the name of your bot, just here, the name of your server, just here, and your server
id number, just here.
Another interesting bit of information that you can extract from a guild is a list of users who are members of the guild. By looping through
guild.members, you can pull the names of all the members of the guild and print them using a formatted string. Let’s do that now.
03:19 Here is the implementation of what I just mentioned. If you run the program now, you should see at least the name of the account you created the guild with and the name of the bot user itself. There you go!
03:43 Now these examples are barely scratching the surface of the APIs available on Discord. So be sure to check out the documentation to see all that they have to offer. A link to this documentation will be below the video.
Become a Member to join the conversation.