Discord Bot can only see itself and no other users in guild(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 只能看到自己,不能看到公会中的其他用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord Bot 只能看到自己,不能看到公会中的其他


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01