@bot.command()
@commands.has_role('Administrator')
async def addmoney(userid, amount, user):
try:
test = False
cursorobj = db.cursor()
read = cursorobj.fetchall()
for i in read:
if userid in i:
currency = int(i[2])
currency += amount
test = True
if not test:
data_insert = """INSERT INTO currency (NAME, ID, CURRENCY) VALUES (?, ?, ?);"""
data = (user, userid, amount)
cursorobj.execute(data_insert, data)
db.commit()
return f'Success! <@{userid}> has earned {amount} (1)'
data_insert = """Update currency set CURRENCY = ? where userid = ?"""
data = (amount, userid)
cursorobj.execute(data_insert, data)
db.commit()
return f'Success! <@{userid}> has earned {amount} (2)'
except Exception as e:
print(e)
#π not able to save to database
58 messages Β· Page 1 of 1 (latest)
@proud solstice
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
does that fix the error?
except Exception as e:
print(e)
Don't ever do that. At least print the traceback and not just the exception. In this case though, I don't see why you don't just let the command crash.
whats the error?
welp lemme try that
mfs be printing the last line of the error then go "hmm i wonder what the issue is π€"
its just not saving into the database
I don't really get what you're doing here with test and cursorobj.fetchall
the cursor doesnt even retrieve anything
the test is meant to check if the person already exists
Actually, the more I look at this command the more confused I am.
- does returning a string from a command even do anything? don't you need to explicitly send a message instead?
- where's the Context parameter?
wdym by context parameter?
|| ctx ||
what library is this?
returning a string doesnt do anything, yes. you're right
looks like dpy to me π€·
im using this for a discord bot thing, the string is the message output from the bot
would that fix it?
returning a string doesnt give it bot output...
well a number of things need fixing
the bot reads the message sent by the person and checks if its a command, if its the addmoney command, it executes this functions, and returns the message, and sends the success message
- add the
ctxparameter at the start - use
ctx.sendinstead ofreturn - use the cursor to actually retrieve the data
- no idea why you're using a
forloop when you can just use theWHEREclause - remove the
try-exceptblock - no idea what
testis supposed to do
this isnt the full code
why does it have command decorators then?
test is meant to check if the person is in the database, not to sure how to check
@bot.command()```
this turns your function into a `Command` object
ye its in there
just use the WHERE clause
the bot output message works, its just that it doesnt save into the message
!code
for context: sql SELECT price FROM groceries WHERE name = 'apple';
@proud solstice put your code in here
!paste
Pasting large amounts of code
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
full code is a lil too long
wait how do I use the WHERE to check if a person exists?
you shouldnt be using on_message listeners for this
the whole reason for commands is that you can run them like <prefix>[command]
yea, I was using a random tutorial to learn about the discord bot, first time using this and databases, wanted to use this as practice
alr lemme check it out
if you need no data but only to check if there's such a row, you'd do a SELECT 1 FROM ... WHERE ....
oh thats actually kinda handy ngl
what would you pt after the WHERE part though
whatever you're searching for. user_id = ? or whatever
ohh
oh wait use this instead. it looks a lot more beginner friendly: https://www.sqlshack.com/exploring-the-sql-where-statement/
wait thn whats the from part used for
oh wait nvm
select something from table where this condition is met
wait how do I check if a erson exists, then check 1 column and set it to a variable if they exist?
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.