#python #python-3.x #discord #discord.py
#python #python-3.x #Discord #discord.py
Вопрос:
Я пытался создать бота discord, но похоже, что guild.members не работает. Вот мой код:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:n'
f'{guild.name}(id: {guild.id})n'
)
members = 'n - '.join([member.name for member in guild.members])
print(f'Guild Members:n - {members}')
client.run(TOKEN)
Согласно руководству (https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-the-developer-portal ) результат должен быть
RealPythonTutorialBot#9643 is connected to the following guild:
RealPythonTutorialServer(id: 571759877328732195)
Guild Members:
- aronq2
- RealPythonTutorialBot
Но в моем выводе отсутствуют все участники, кроме самого бота. Я буквально скопировал вставленный код из руководства, но результат остался прежним.
Кто-нибудь знает, что не так?
Заранее спасибо!
ОБНОВЛЕНИЕ (последний код)
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.ext.commands import Bot
intents = discord.Intents()
intents.members = True
bot = commands.Bot(command_prefix='!', intents =intents)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:n'
f'{guild.name}(id: {guild.id})n'
)
members = 'n - '.join([member.name for member in guild.members])
print(f'Guild Members:n - {members}')
print(intents.members)
client.run(TOKEN)
Ответ №1:
Попробуйте
from discord.ext import commands
from discord.ext.commands import Bot
intents = discord.Intents()
intents.members = True
bot = commands.Bot(command_prefix='!', intents =intents)
Комментарии:
1. Куда я должен это добавить? Если я вставлю его в свой код, он выдает: NameError: имя «команды» не определено
2. попробуйте еще раз, пожалуйста
3. К сожалению, результат не изменился
4. Я думаю, вам нужно поделиться своим последним кодом. Обновите вопрос с помощью последнего кода.