Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using APIs

Using a 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.

00:00 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 .env file.

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.

00:37 I have removed my bot token here, but I will replace it before we run any more code.

00:43 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 on_ready().

00:57 Now, what you want to do here is loop through the guild data that Discord sends client, specifically, client.guilds. Ah, I have forgotten something. My apologies.

01:12 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

01:28 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[0] 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.

01:54 Therefore, looping through the names is a more robust solution and a better practice in order to find the guild you’re looking for. Just be aware that, technically, you could have this instead.

02:06 Don’t run it from here.

02:08 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.

02:23 Interrupt execution should also work, but occasionally that’s been buggy for me. If you’re using something that’s not Thonny, then Control + C or an equivalent should be fine to get you going.

02:36 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.

03:03 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:33 guild.members! Ah, it’s meant to be guild.members, but you get the general idea. There’s my Discord account name and the bot name.

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.

03:57 Next up, you will begin to explore utility functions and how to use them.

Become a Member to join the conversation.