简体   繁体   English

discord py bot 只看到一个成员

[英]discord py bot sees only one member

I'm trying to get the # of all users on server, the bot sees only itself although it responds to the messages of the others.我正在尝试获取服务器上所有用户的数量,尽管机器人会响应其他人的消息,但它只能看到自己。

client = discord.Client()

@client.event
async def on_message(message):
  msg=message.content
  if message.author == client.user:
    return
  if msg.startswith("count"):
    await message.channel.send(client.users)

The code outputs a list with one user (bot itself)该代码输出一个包含一个用户(机器人本身)的列表

You need to enable the Intents, they are missing.您需要启用 Intent,它们丢失了。

Make sure to turn them on in the Discord Developer Portal for your application ( Settings -> Bot ).确保在Discord 开发人员门户中为您的应用程序打开它们(设置 -> 机器人)。

To implement them to your code you can use the following:要将它们实现到您的代码中,您可以使用以下内容:

intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents)

Or in your case:或者在你的情况下:

intents = discord.Intents.all() # Imports all the Intents
client = discord.Client(intents=intents)

You can read more in the Docs here .您可以在此处的文档中阅读更多内容。

Well the issue you're having is because since version 1.5 (not 1.6 as i initially tought, thanks Łukasz Kwieciński) discord.py needs you to specify the intents your bot needs in order to receive such information from discord's API.好吧,您遇到的问题是因为从版本 1.5(不是 1.6 开始,感谢 Łukasz Kwieciński)discord.py 需要您指定您的机器人需要的意图,以便从不和谐的 API 接收此类信息。

Your code will work if you change this:如果您更改此代码,您的代码将起作用:

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

For it to work though you'll need to go to your bot's page and enable the members intent.尽管您需要 go 到您的机器人页面并启用成员意图,但它才能正常工作。

For what i can see in your code it appears you're trying to make a command, i strongly suggest you start using the commands extension.对于我在您的代码中看到的内容,您似乎正在尝试执行命令,我强烈建议您开始使用命令扩展名。 Your code would look like this:您的代码如下所示:

from discord.ext import commands

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

bot = commands.Bot(command_prefix='.', intents=intents)

@bot.command()
async def count(ctx):
    await ctx.send(bot.users)


bot.run(TOKEN)

The resulting code is more compact, readable and maintainable.生成的代码更加紧凑、可读和可维护。 You can check other examples here您可以在此处查看其他示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM