M
M
maks_petrov92021-10-17 15:25:22
Python
maks_petrov9, 2021-10-17 15:25:22

How to create a sqlite3 database for all servers that have a discord.py bot?

I started writing a discord bot in python, connected it to a database, wrote a couple of economy teams and decided to make sure that each server where my bot is located will have its own database.
In the name of the table I tried to write the server ID first - it did not work.
Then I decided to write the name of the server - it did not work either.

I'm sure the solution is very simple, but I can't find it :c
I hope you can help me. Here is the code:


@Bot.event
async def on_ready():
  DiscordComponents(Bot)
  print('Готов служить OneLife™!')
  await Bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="за птичками"))

  for guild in Bot.guilds:

    cursor.execute("""CREATE TABLE IF NOT EXISTS {} (
    name TEXT,
    id INT,
    cash BIGINT,
    rep INT,
    lvl INT,
    server_id INT,
    aboutme TEXT,
    yo TEXT
    )""".format(guild.id))

    for member in guild.members:
      if cursor.execute("SELECT id FROM {} WHERE id = {}".format(guild.id, member.id)).fetchone() is None:
        cursor.execute(f"""INSERT INTO {guild.id} VALUES ('{member}', {member.id}, 0, 0, 1, {guild.id}, 'Не указано! `/aboutme`', 'Не указано! `/yo`')""")
      connect.commit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-17
@maks_petrov9

Wouldn't it be easier to include the server id in the appropriate tables?
Those. each user will have a composite id: own id + server id. Similarly with links to this user.
Because creating a separate database for each server is quite a chore - and it's not very clear what the benefit of this is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question