-1

So, I created a minigame bot on telegram. The bot just contains a fishing game, and it's already running. I want if a user fishes and gets a fish, the fish will be stored in a database. So the user can see what he got while fishing. Does this is require SQL?

I haven't tried anything, because I don't understand about storing data in python. If there is a tutorial related to this, please share it in the comments. Thank you

Ravi
  • 20
  • 1

2 Answers2

1

You can use anything to store user data, including text files.

The simplest approaches to storing data can be serializing a dictionary to JSON with the builtin json module:

DATABASE = 'database.json'  # (name or extension don't actually matter)

import json

# loading
with open(DATABASE, 'r', encoding='utf-8') as fd:
    user_data = json.load(fd)

user_data[1234] = 5  # pretend user 1234 scored 5 points

# saving
with open(DATABASE, 'w', encoding='utf-8') as fd:
    json.dump(user_data, fd)

This would only support simple data-types. If you need to store custom classes, as long as you don't upgrade your Python version, you can use the built-in pickle module:

DATABASE = 'database.pickle'  # (name or extension don't actually matter)

import pickle

# loading
with open(DATABASE, 'rb') as fd:
    user_data = pickle.load(fd)

user_data[1234] = 5  # pretend user 1234 scored 5 points

# saving
with open(DATABASE, 'wb') as fd:
    pickle.dump(user_data, fd)

Whether this is a good idea or not depends on how many users you expect your bot to have. If it's even a hundred, these approaches will work just fine. If it's in the thousands, perhaps you could use separate files per user, and still be okay. If it's more than that, then yes, using any database, including the built-in sqlite3 module, would be a better idea. There are many modules for different database engines, but using SQLite is often enough (and there are also libraries that make using SQLite easier).

Telethon itself uses the sqlite3 module to store the authorization key, and a cache for users it has seen. It's not recommended to reuse that same file for your own needs though. It's better to create your own database file if you choose to use sqlite3.

Lonami
  • 5,945
  • 2
  • 20
  • 38
  • Then, how do you make the .txt file appear when someone sends the command /myscore. And how to apply the +1 score to each item obtained? Oh ya, users are only about 40 people. Because it's played in a private group – Ravi Dec 01 '22 at 02:44
  • You can send the file to the chat directly if you want to. But in my example `user_data` is loaded as a variable which you can access and format into a message string to send which is probably more useful. How to do that depends on how the loaded data looks like. – Lonami Dec 01 '22 at 18:08
0

Using a txt file as database is a terrible idea, go with SQL

  • Or even MYSQL because its free and supports millions of rows. – easleyfixed Nov 30 '22 at 17:46
  • Also if its a one time thing .. a database would be silly, just store it in a session variable. But if you need to track it so when they come back it remembers them a database is the way to go. Pro Tip .. download MS Workbench and start making tables as if it was an Excel file. – easleyfixed Nov 30 '22 at 17:47
  • If I use a computer, I can do that. However, I build and run code only on my mobile phone. Is there any alternative? – Ravi Nov 30 '22 at 17:58