N
N
Nikita Mokhovikov2021-07-12 22:39:47
Python
Nikita Mokhovikov, 2021-07-12 22:39:47

How to add all server members to the database?

Hello! I am writing my little economic bot for my server.

A piece of code
import sqlite3
from discord.ext import commands
from config import settings

client = commands.Bot(command_prefix=settings['PREFIX'])

connection = sqlite3.connect('server.db')
cursor = connection.cursor()

@client.event
async def on_ready():
    cursor.execute("""CREATE TABLE IF NOT EXISTS users (
        name TEXT,
        id INT,
        money BIGINT,
        rep INT,
        lvl INT
    )""")

    for guild in client.guilds:
        for member in guild.members:
            if cursor.execute(f"SELECT id FROM users WHERE id = {member.id}").fetchone() is None:
                cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 0, 0, 1)")
            else:
                pass
    
    connection.commit()
    print(f'{client.user} - бот успешно запущен')
  
client.run(settings['TOKEN'])

If the participant is not in the database, then when the bot starts, it must add it, but the bot adds only itself, although there are 5 people on the server
60ec99f532956579895374.png

. What could be the error? I looked through all the code, I did not find an error. Bot, if anything, is connected to the server

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-07-13
@mohovoy

open developer portal => select your app => go to bot tab => scroll down to Privileged Gateway Intents => enable PRESENCE INTENT and SERVER MEMBERS INTENT => click Save Changes and restart your bot.

O
Oleg, 2021-07-12
@402d

VALUES ('{member}', {member.id},
according to the structure of the database, your first field is a string for the name. And put the object there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question