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 只能看到自己,不能看到公会中的其他
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
