Discord Bot只能看到自己,而公会中则看不到其他用户 [英] Discord Bot can only see itself and no other users in guild

查看:32
本文介绍了Discord Bot只能看到自己,而公会中则看不到其他用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在关注本教程让我开始使用Discord的API.不幸的是,当我得到有关打印行会中所有用户的内容时,我碰壁了.

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)

推荐答案

从discord.py v1.5.0开始,您需要对机器人使用 Intents ,您可以通过以下方式阅读有关它们的更多信息:单击此处换句话说,您需要在代码中进行以下更改-

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)

这篇关于Discord Bot只能看到自己,而公会中则看不到其他用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆