Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

python - How to change only one value of a list from repl.it database?

I'm trying to code a Discord game bot on repl.it and I use its database to store the players' data.

So I'm now making a command to let players rename themselves but then there is a problem. The following lines work but involve affecting a new value to the entire player_data list which stores the data of every player:

renamer = player_id.index(message.author.id)
new_name = message.content[len(prefix)+7:]
player_data = db["player_data"]
player_data[renamer][0] = new_name
db["player_data"] = player_data
await message.channel.send('You successfully renamed yourself into '+new_name+'.')

So I'm afraid that if someone starts playing while another is changing his nickname, the 'old' player_data will overwrite the new one, which has the new player's data.

I want something similar to these lines (which don't work) to only change the involved player's data:

renamer = player_id.index(message.author.id)
new_name = message.content[len(prefix)+7:]
db["player_data"][renamer][0] = new_name
await message.channel.send('You successfully renamed yourself into '+new_name+'.')
question from:https://stackoverflow.com/questions/65517015/how-to-change-only-one-value-of-a-list-from-repl-it-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So since I can create many keys in the repl.it database, I decided to create a key for every player, and those lines work without having to get the other players' data :

new_name = message.content[len(prefix)+7:]
player_data = db["player_data"+str(message.author.id)]
player_data[0] = new_name
db["player_data"+str(message.author.id)] = player_data
await message.channel.send('You successfully renamed yourself into '+new_name+'.')

If anyone has a solution for the initial problem, I'm still interested.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...