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

Unlock This Lesson

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

Unlock This Lesson

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

Jeff on Dec. 21, 2020

Thanks for the great tutorial. It’s unclear what is supposed to be in our .env file using the variable DISCORD_GUILD. Is this another token that we need to generate?

MajesticBeast on Feb. 8, 2021

Hopefully you figured it out by now. But if not, or for others who come across this post, it’s the name of your Discord server you are using the bot in. This can be found in the upper left of the Discord window.

xardas071996 on April 15, 2021

Somehow I can only see the name of the bot when returning the members list of my server. The code I ran is the exact same as shown in the video and everything works perfectly fine, except that the only name shown is that of the bot, no matter how many users the server actually has.

Thanks a lot in advance.

prashantsekuri on June 19, 2021

@xardas071996, I see the same output as you. Were you able to figure out what might have gone wrong?

ndsizemore on Jan. 31, 2022

@xardas071996 and @prashantsekuri, I was seeing the same issue, but I’ve found the solution – Discord now requires “intents” to access certain data, including members. If you modify your script as shown below, you should see every member of your server as expected.

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

You’ll also need to make sure the “Server Members Intent” option is turned on in your Discord bot settings (

For more details, see

Eric on May 14, 2022

It looks like with the changes to the discord API, the members function no longer works in this lesson. I’ve receiving the below error.

Traceback (most recent call last):
 File "", line 22, in <module>
   members = '\n - '.join([ for member in guild.member])
NameError: name 'guild' is not defined

Become a Member to join the conversation.