Reputation: 167
so, i made command where i store user input into a json file, there's the code :
for user in client.users:
with open('suggestions.json', 'r') as f:
h = json.load(f)
h[str(f'{user.id} | {user.name} ')] = message.content
with open('suggestions.json', 'w') as f:
json.dump(h, f, indent=4)
The problem is that when two or more different users use the command all their input changes in the last one input like :
{
"ID| USER NAME ": "ol",
"ID | USER NAME ": "ol",
"ID | USER NAME ": "ol"
}
and for some reason is storing also the bot name and id with the user input and all members or bots that are typing in that second with the same value as the one who casted the command, i tried lots of things, i really don't have any idea, just maybe using a db but atm i don't have knowledge about this.
for a better view, my command looks like this at this moment :
@client.command()
async def suggestion(ctx):
await ctx.send("You want to submit a suggestion?")
message = await client.wait_for ('message' ,timeout=25, check = lambda m: m.author == ctx.author and m.channel == ctx.channel)
if message.content == "yes" :
await ctx.send("Good choice, now let's hear your suggestion : ")
message = await client.wait_for('message', timeout=25, check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
await ctx.send("Alirght, i will send this suggestion to the developer, thanks ! ")
for user in client.users:
with open('suggestions.json', 'r') as f:
h = json.load(f)
h[str(f'{user.id} | {user.name} ')] = message.content
with open('suggestions.json', 'w') as f:
json.dump(h, f, indent=4)
elif message.content == "no":
await ctx.send("Too bad, please use this command just if you have any ideas.")
Upvotes: 1
Views: 1674
Reputation: 1207
so, you are making a simple suggest command, this is the way used to store data in json perfectly:)
def saveJson(data, file):
json.dump(data, open(file, "w"), indent = 4)
_suggestions = open('./suggestions.json', 'r')
suggestions = json.load(f)
_suggestions.close()
@client.command()
async def suggest(ctx, *, message):
user = ctx.author.id
username = str(ctx.author)
for current_user in suggestions['users']:
if current_user['id'] == user:
current_user['suggestions'].append(message)
if username != current_user['name']:
current_user['name'] = username
break
else:
suggestions['users'].append({
'id':user,
'name':username,
'suggestions': [message]
})
saveJson(suggestions, "./suggestions.json")
your json file should look like this,
{
'users': [
{
'id': 123456789,
'name': 'billy#1234',
'suggestions: [
'be good',
'drink milk',
'eat cookies'
]
},
{
'id': 1234512345,
'name': 'willy#1234',
'suggestions: [
'don't be good',
'don't drink milk',
'don't eat cookies',
'drink orange juice'
]
}
}
and, by making a suggestions comman, you can retrieve all suggestions by a user, let me know, if you'd like to know how to do that :)
Upvotes: 1