Discord.py 重写 get_member() 函数,为除 bot 之外的所有用户返回 None

问题描述

几个月前我做了一个愚蠢的不和谐机器人来每分钟更改我朋友的名字,但我今天更新了它,现在 get_member 函数没有返回.

I made a stupid discord bot a few months ago to change my friends name every minute, but I updated it today and now the get_member function is returning none.

@client.event
async def on_ready():
    print('bot is ready!')
    status = discord.Activity(name="Sam be a loser", type=discord.ActivityType.watching)
    await client.change_presence(activity=status)

    name_change.start()

@tasks.loop(minutes=1)
async def name_change():
    server = client.get_guild(id=584112137132048387)
    user = server.get_member(376388277302984714)

    global english_words
    global wordNumber
    wordNumber = wordNumber + 1
    #changes nickname to current word
    await user.edit(nick=f"{english_words[wordNumber]} lookin' ass")
    print(f'Word number {wordNumber}: {english_words[wordNumber]}')
    #updates number in file
    file = open("currentWord.txt", "w")
    file.write(str(wordNumber))
    file.close

我对服务器中的每个用户都尝试了相同的功能,但它没有返回所有用户,除非我将机器人的 id 放入其中.我不知道为什么会发生这种情况,因为我什至没有编辑这部分代码.

I tried the same function with every user in the server, but it returned none for all of them except when I put the bot's id in. I have no clue why this is happening since I didn't even edit this part of the code.


解决方案

这很可能是由于最近的 discord.py 1.5 更新.声明 Bot 时需要配置 Intent.

This is most likely due to the recent discord.py 1.5 update. You are required to configure your intents when you're declaring your Bot.

什么是意图?1.5 版引入了意图.这是机器人编写方式的根本变化.意图基本上允许机器人订阅特定的事件桶.与每个意图相对应的事件记录在意图文档的单个属性中.这是一个例子:

What are intents? Version 1.5 comes with the introduction of intents. This is a radical change in how bots are written. An intent basically allows a bot to subscribe into specific buckets of events. The events that correspond to each intent is documented in the individual attribute of the Intents documentation. Here's an example:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True  # Subscribe to the privileged members intent.
bot = commands.Bot(command_prefix='!', intents=intents)

您还必须为公会相关事件启用特权意图:这里

You will also have to enable privileged intents for guild related events: Here

您可以阅读有关意图的更多信息:此处

You can read more about intents: Here

相关文章