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

查看:29
本文介绍了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: 
' 
        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 = '
 - '.join([member.name for member in guild.members])
    print(f'Guild Members:
 - {members}')
    
client.run(TOKEN)

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

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