Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I store data with my discord bot?

So I set up MySQL for my discord.py bot.
I’m planning to do some small project to test myself on my friend server.

I want it to count how many messages are sent on a week, but it doesn’t work.
It gives me this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Rui\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Rui\Desktop\Python\mudaenotifier\kyoreborn.py", line 40, in on_message
    cursor.execute(query,[(username)])
  File "C:\Users\Rui\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor_cext.py", line 257, in execute
    prepared = self._cnx.prepare_for_mysql(params)
  File "C:\Users\Rui\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection_cext.py", line 665, in prepare_for_mysql
    result = self._cmysql.convert_to_mysql(*params)
_mysql_connector.MySQLInterfaceError: Python type Member cannot be converted

What can I do with this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my code:

@client.event
async def on_message(message):
    username=message.author
    print(username)
    query="SELECT * FROM messages WHERE username=%s"
    cursor.execute(query,[(username)])
    results=cursor.fetchall()
    if results:
        query="UPDATE messages SET messagecount = messagecount + 1 WHERE username = %s"
        cursor.execute(query,[(username)])
        con.commit()
        con.close()
    else:
        query = "INSERT INTO `messages`(username,messagecount) VALUES (%s,1)"
        cursor.execute(query, [(username)])
        con.commit()
        con.close()
    await client.process_commands(message)

>Solution :

message.author returns a discord.Member instance, use str(message.author) instead to cast it to a string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading