当前位置: 首页 > 知识库问答 >
问题:

discord.py机器人看不到成员

锺高翰
2023-03-14

我试图做一个不和谐机器人,它创建一个类别,并为角色和用户设置权限,将用户id作为参数:

for x in range(2, len(args)):
member= client.get_user(int(args[x]))
await message.guild.categories[-1].set_permissions(member, send_messages = True)

其中args是包含命令、类别名称和用户ID的字符串数组。用法应为:

!create category-name 0000000 1111111

我对此有一些问题,因为就像机器人看不到服务器的成员,他唯一可以添加的用户是我,如果在第一个参数中指定服务器的所有者(在我的例子中是000000)。如果我把别人的ID,机器人不会在类别中添加该用户的权限。我发现,也许机器人看不到其他用户,事实上,如果我把线:

print(message.guild.members)

它将只打印机器人作为成员,

打印此文件:[

我不知道为什么它计算成员却看不到其他用户,我能怎么办?

共有3个答案

单于煌
2023-03-14

您不需要这样拆分它,我想您不知道,但您应该从这里签出命令,为async def函数指定参数,然后您可以执行以下操作:

from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
token = "YourBotsTokenHere"
bot = commands.Bot(command_prefix='!', intents=intents) # Or whatever prefix you want

@bot.event
async def on_ready():
    print(f"Bot {bot.user} has connected to Discord!")

@bot.command()
async def create(ctx, type_, member: discord.Member, categoryName=None, *, name):
    # member is going to return a member object so u can do stuff like member.id or member.name, etc...
    if type_.lower() == "category":
        # Code to create a category with the name variable
        # some pseudo-code that might look similar to what u need
        role = get(member.server.roles, name="NameOfTheRole")
        await bot.add_roles(member, role)
        # insert a part of ur own code here for category thingy

    elif type_.lower() == "channel":
        # Code to create a channel with the name variable, category name must be also specified so we can check to what category the user is reffering to, also check if the user has the role to create that channel
        if categoryName == None: return
        catgName = categoryName.replace('-', ' ') # Replaces "-" with an empty space

bot.run(token)

至于其他东西,启用成员意图可以很好地工作,我已经在示例中编写了它

钦宏义
2023-03-14

安装1.4版。库的第2部分:pip安装-U discord。py==1.4。2

淳于熙云
2023-03-14

由于derw链接的答案不再可用;

正如OP所指出的,我设法通过实例化不和谐机器人客户端来解决这个问题,如下所示:

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

此外,我还必须在Discord developer portal中的bot设置下启用“服务器成员意图”特权网关意图:

启用服务器成员意图

 类似资料:
  • 我正在学习如何使用python创建一个discord bot,但我在使用这个命令时遇到了问题。我试图做的是踢一个特定的用户,然后使用bot将邀请发送回discord服务器。这是一个愚蠢的想法,但我真的想让它发挥作用。 我特别遇到的问题是如何踢一个特定的用户(带有用户标识),然后将该用户DM。 谢谢! 这里的代码: 这样做的目的是,如果具有适当角色类型的人一个特定的不一致用户id()被踢,机器人会自

  • 我是一个初学者程序员(或者在我看来是这样),我需要帮助在音乐机器人中实现一个队列。 目前,只有当队列中有一首歌时,队列才能正常工作。如果有更多的歌曲,那么递归开始(在=wait serverQueue(语音,消息)之后从def play开始并等待play(queue.pop(0),语音,消息)从队列开始),并且所有歌曲只是跳过。 我试图以不同的方式解决这个问题,例如,引入了第二个变量,但这并没有带

  • 下面是代码: 出于某种原因,即使我使用用户id,bot也总是输出“未找到用户”。机器人有权这么做,有人知道解决办法吗?

  • 我有一个关于不和的问题。皮耶。我运行我的bot所在的两个独立服务器:测试服务器和主服务器。问题是,当我在测试服务器中发送消息时,bot会将其消息发送到主服务器,而不会将其发送回调用命令的服务器(仅在函数中)。 例如: 如果我在测试服务器中键入上述内容,我的bot将以“你好!”在测试服务器中。但是,如果我尝试将此代码放入函数并调用它: 通道ID显然设置为特定服务器。因此,假设我将ID“1234”作为

  • 我目前正在制作一个discord bot,并希望在服务器加入时在服务器的通道中发送一条消息,这是我到目前为止的代码。 当我运行这段代码时,什么都不会发生。我没有得到任何ERORR或输出。 如果有人能帮忙,那就太棒了。谢谢

  • 我有错误,我检查了这个-discord bot-userinfo命令“IndentationError:unexpected indent”,但我不知道如何修复我的代码 此行出错- 这是我所有的功能