#Pulling discord id instead of discord username question

1 messages · Page 1 of 1 (latest)

jovial echo
#

Hello! When I run the slash_option and do OptionType.USER to get the user, it returns with their username. I checked the documentation and online and have been unable to find if it is possible to instead pull/convert the mentioned user's id instead (ex. 713201821681713263). In my database, I have it set up to handle them instead of usernames as there are still some people who have not moved over to the new username syntax. Thanks in advance!

dark summitBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

atomic sleet
jovial echo
#

Sure

#

Yeah it was just pulling the user tag

#
    name="addloan",
    description="Adds a loan for the specified user",    
)
@slash_option(
    name="loanee",
    description="Recipient of the Loan",
    required=True,
    opt_type=OptionType.USER
)
@slash_option(
    name="amount",
    description="Please enter the loan principal in dollars",
    opt_type=OptionType.INTEGER,
    required=True
)
@slash_option(
    name="interest",
    description="Please enter the interest rate (ex. 5 for 5 percent interest)",
    opt_type=OptionType.INTEGER,
    required=True
)
async def addloan(ctx:SlashContext,loanee:User,amount:int,interest:int):
    conn = sqlite3.connect('orwellv1_2')
    cursor = conn.cursor()
    principal = amount
    interest_rate= interest

    loan_date = datetime.utcnow()
    daily_interest = calculate_daily_interest(principal, interest_rate)
    remaining_principal = principal  # Initial remaining principal is the same as the principal
    cursor.execute(
        "INSERT INTO loans (discordid, principal, interest_rate, loan_date, daily_interest, remaining_principal) "
        "VALUES (?, ?, ?, ?, ?, ?)",
        (userid, principal, interest_rate, loan_date, daily_interest, remaining_principal)
    )
    conn.commit()
    conn.close()
    await ctx.send(f"Loan of ${principal:.2f} has been added for {loanee}.")```
atomic sleet
# jovial echo ```@slash_command( name="addloan", description="Adds a loan for the spec...

two things:

  • you are getting the object correctly - loanee is a user! im curious about the userid variable that isn't defined in your code itself - is that just loanee?
  • you should be using aiosqlite, not sqlite - sqlite is blocking, and so even moderately long queries can completely break your program. we'll have an entry about it sooner or later, but discord.py's entry isnt too bad: https://discordpy.readthedocs.io/en/latest/faq.html#what-does-blocking-mean
jovial echo
atomic sleet
#

well, i mean, if its unimportant to this function, then none of this code makes sense because it doesnt seem like you do anything with the loanee other than print it 😅

jovial echo
#

Okay so what I want it to do is basically have the user put in the person that is getting the loan, type in the amount the loan is for, the amount of the interest and then insert it into my sqlite db

atomic sleet
#

well, yes

#

but the userid variable youre putting seems utterly unrelated to loanee

#

theres no assignment for it in the code you give, and that's arguably the most important thing about this code

jovial echo
atomic sleet
#

ah, so you want to know what to put - i see

jovial echo
#

Bingo. That was where I was finding my main problem

atomic sleet
jovial echo
atomic sleet
#

yep

jovial echo
#

Ahh that makes much more sense when reading the site. Thanks for your help!