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!
#Pulling discord id instead of discord username question
1 messages · Page 1 of 1 (latest)
Hey! Once your issue is solved, press the button below to close this thread!
can we see code? the option should be returning a proper user object, though it may appear as just the users tag when you print it
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}.")```
two things:
- you are getting the object correctly -
loaneeis a user! im curious about theuseridvariable that isn't defined in your code itself - is that justloanee? - 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
So right now I am trying to convert my server's current bot over so it was an old variable that is used elsewhere. Realistically I was going to change it back when it was all up and running
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 😅
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
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
Yeah, I was going to use userid as the user id of the person and plug in whatever the fix was of converting the username of the person to the user id as the variable userid
ah, so you want to know what to put - i see
Bingo. That was where I was finding my main problem
to note about this code so far: str(loanee) == loanee.tag, and using fstrings to put in variables into a string implicitly calls str(loanee). so whatever youre seeing is just the tag property and not the whole user object - thankfully, loanee is sill a User (or Member in a guild) and so still has an id attribute
yep
Ahh that makes much more sense when reading the site. Thanks for your help!