S
S
Stanislav Solovyov2021-01-18 16:58:09
Python
Stanislav Solovyov, 2021-01-18 16:58:09

How to get a list of server members and add them to the database?

Using Discord.py, I tried to add server members to the database, but only the bot itself.

@bot.event
async def on_ready():
    print("Bot Has been runned")#сообщение о готовности
    for guild in bot.guilds:#т.к. бот для одного сервера, то и цикл выводит один сервер
        print(guild.id)#вывод id сервера
        for member in guild.members:#цикл, обрабатывающий список участников
            print(member.name)
            cursor.execute(f"SELECT id FROM users WHERE id={member.id}")#проверка, существует ли участник в БД
            if cursor.fetchone()==None:#Если не существует
                cursor.execute(f"INSERT INTO users VALUES ({member.id}, '{member.name}', '<@{member.id}>', 50000, 'S','[]',0,0)")#вводит все данные об участнике в БД
            else:#если существует
                pass
            conn.commit()#применение изменений в БД

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-01-18
@MrAnanas

When creating a bot, you need to add all the intents to it.
https://discordpy.readthedocs.io/en/latest/intents.html
I don’t understand what are there, so you can just bang everything

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question