[英]discord py bot sees only one member
我正在尝试获取服务器上所有用户的数量,尽管机器人会响应其他人的消息,但它只能看到自己。
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)
该代码输出一个包含一个用户(机器人本身)的列表
您需要启用 Intent,它们丢失了。
确保在Discord 开发人员门户中为您的应用程序打开它们(设置 -> 机器人)。
要将它们实现到您的代码中,您可以使用以下内容:
intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents)
或者在你的情况下:
intents = discord.Intents.all() # Imports all the Intents
client = discord.Client(intents=intents)
您可以在此处的文档中阅读更多内容。
好吧,您遇到的问题是因为从版本 1.5(不是 1.6 开始,感谢 Łukasz Kwieciński)discord.py 需要您指定您的机器人需要的意图,以便从不和谐的 API 接收此类信息。
如果您更改此代码,您的代码将起作用:
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
尽管您需要 go 到您的机器人页面并启用成员意图,但它才能正常工作。
对于我在您的代码中看到的内容,您似乎正在尝试执行命令,我强烈建议您开始使用命令扩展名。 您的代码如下所示:
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)
生成的代码更加紧凑、可读和可维护。 您可以在此处查看其他示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.