Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord Bot can only see itself and no other users in guild

I have recently been following this tutorial to get myself started with Discord's API. Unfortunately, when I got the part about printing all the users in the guild I hit a wall.

When I try to print all users' names it only prints the name of the bot and nothing else. For reference, there are six total users in the guild. The bot has Administrator privileges.

import os
import discord

TOKEN = os.environ.get('TOKEN')
client = discord.Client()

@client.event
async def on_ready():
    for guild in client.guilds:
        print(guild, [member.name for member in guild.members])

client.run(TOKEN)
like image 689
meowkovich Avatar asked Oct 01 '20 02:10

meowkovich


People also ask

What is Guild in Discord bot?

Guilds in Discord represent an isolated collection of users and channels, and are often referred to as "servers" in the UI.

Why wont the bots join my Discord server?

Checking Permissions Not everyone can add a bot to a Discord server! Only people who have Administrative or “Manage Server” permissions on the server can invite a bot. If you don't have either of these roles, you won't be able to add bots. If you created the server, you should be the administrator by default.


Video Answer


2 Answers

  1. Enable the server members intent near the bottom of the Bot tab of your Discord Developer Portal:

    enter image description here

  2. Change the line client = discord.Client() to this:

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

    This makes your bot request the "members" gateway intent.


What are gateway intents? Intents allow you to subscribe to certain events. For example, if you set intents.typing = False your bot won't be sent typing events which can save resources.

What are privileged intents? Privileged intents (like members and presences) are considered sensitive and require verification by Discord for bots in over 100 servers. For bots that are in less than 100 servers you just need to opt-in at the page shown above.

A Primer to Gateway Intents

Intents API Reference

like image 100
xjcl Avatar answered Oct 18 '22 20:10

xjcl


As of discord.py v1.5.0, you are required to use Intents for your bot, you can read more about them by clicking here In other words, you need to do the following changes in your code -

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents.all()
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild: \n' 
        f'{guild.name} (id: {guild.id})'
    )

    # just trying to debug here
    for guild in client.guilds:
        for member in guild.members:
            print(member.name, ' ')

    members = '\n - '.join([member.name for member in guild.members])
    print(f'Guild Members:\n - {members}')
    
client.run(TOKEN)
like image 45
Videro Avatar answered Oct 18 '22 21:10

Videro