Discord.py rewrite get_member() function returning None for all users except bot

谁说我不能喝 提交于 2020-12-27 06:14:27

问题


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

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.


回答1:


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.

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




回答2:


You may be able to not use Intents, Privileged or otherwise, by not using get_member() and instead use

guild.fetch_member()


来源:https://stackoverflow.com/questions/64221377/discord-py-rewrite-get-member-function-returning-none-for-all-users-except-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!