H
H
happyjuic2022-02-28 13:15:05
Python
happyjuic, 2022-02-28 13:15:05

How to add nicknames of people from discord (discord.py) to sqlite3 database?

I want to create a command in which the nickname of the user who entered the command will be entered into the database. Also, along with his nickname, the n-th amount of experience and the n-th amount of coins (ie 3 columns) will be entered, please help! How to do it? My code:

from os import curdir
from discord.ext import commands
import discord
from random import *
import sqlite3

TOKEN = ""

# Задать переменные
bot = commands.Bot(command_prefix=('!'))
bot.remove_command( 'help' )

# Создание переменной connect и объекта cursor
connect = sqlite3.connect("eco.db")
cursor = connect.cursor()

# Создание базы данных
cursor.execute("CREATE TABLE IF NOT EXISTS userinfos(id TEXT INTEGER)")
connect.commit()

@bot.command()
async def старт(ctx):
    author = ctx.message.author
    cursor.execute(f"""INSERT INTO userinfos(<{author.mention}>)""")
    connect.commit()#применение изменений в БД

bot.run(TOKEN)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
happyjuic, 2022-02-28
@happyjuic

I have a guess how to do it:
@bot.command()
async def start(ctx):
author = ctx.message.author
cursor.execute("INSERT INTO userinfos ({author.mention})")
But doesn't work

V
Vindicar, 2022-02-28
@Vindicar

Firstly, the idea itself is bad, since the nickname can be changed. Use the user's numeric id instead of a nickname.
Secondly, never form the query text through string formatting. Use parameter substitution .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question