how to check if id exist in json - python

each time i use .register it will keep just making the account and not checking the id exist and if it does it will say the account is already made.
i've tryed if ctx.author.id not in jsonfile
and geting it from the file with other code too
just too much too add
#bot.command()
#commands.has_permissions(manage_guild=True)
async def register(ctx):
with open('Configs/amounts.json') as f:
usersidlel = json.loads(f.read())
if ctx.message.author.id not in usersidlel:
with open('Configs/amounts.json') as f:
data = json.loads(f.read())
data[str(ctx.author.id)] = {}
data[str(ctx.author.id)]['cash'] = 100
data[str(ctx.author.id)]['bank'] = 100
data = json.dumps(data, indent=4, sort_keys=True)
with open('Configs/amounts.json', 'w') as f:
f.write(data)
await ctx.send(f"```Account Registered!```")
else:
await ctx.send(f"Hey {ctx.author.name} you already got an account you silly person!")
No error messages just not sure how to check if the id exist or not.

You're testing a different variable than you're adding. Also, if ctx.author.id is an integer, you need to convert it to a string before testing.
There's also no need to load the JSON twice, just use the data returned by the first load.
async def register(ctx):
with open('Configs/amounts.json') as f:
usersidlel = json.load(f)
if str(ctx.author.id) not in usersidlel:
usersidlel[str(ctx.author.id)] = {'cash': 100, 'bank': 100}
with open('Configs/amounts.json', 'w') as f:
json.dump(usersidlel, f, indent=4, sort_keys=True)
await ctx.send(f"```Account Registered!```")
else:
await ctx.send(f"Hey {ctx.author.name} you already got an account you silly person!")

Related

Discord.py: Expecting value: line 1 column 1 (char 0)

I'm trying to make a bot that re-adds the "muted" role to whoever rejoined the server while muted. Here's the .py code:
#bot.event
async def on_member_join(member):
with open('users.json', 'r') as fp:
data = json.load(fp)
mute = discord.utils.get(member.guild.roles, name = "mute role")
if member.id in data['muted']:
await member.add_roles(mute)
class Moderation(commands.Cog):
#bot.command()
async def mute(ctx, member:discord.Member):
mute = ctx.guild.get_role(THE ROLE'S ID)
await member.edit(roles=[mute])
with open('users.json', 'r+') as fp:
data = json.load(fp)
if member.id not in data['muted']:
data['muted'].append(member.id)
json.dump(data, fp)
And here's the .json code:
{
"muted" : []
}
Every time I mute someone, I get an error saying "JSONDecodeError: Expecting value: line 1 column 1 (char 0)" and the person's ID won't save. I've looked on many other posts for answers but none of them answered anything.
When you open with r+ and read it, you're now seeking to the end. Then, when you write to it, you're actually appending to the file rather than overwriting it.
You can see the behavior here:
>>> with open('_delete_me.txt', 'w') as f:
... f.write('this is a test.')
...
15
>>> with open('_delete_me.txt', 'r+') as f:
... print(f.read())
... f.write('another test')
...
this is a test.
5
>>> with open('_delete_me.txt', 'r') as f:
... print(f.read())
...
this is a test.another test
To overwrite it, it's possible to just open the file again in w write mode:
with open('users.json', 'r') as fp:
data = json.load(fp)
if member.id not in data['muted']:
data['muted'].append(member.id)
with open('users.json', 'w') as fp:
json.dump(data, fp)

How to save user id only once in json file discord.py

The code I have saves the same user in the json file again:
#client.command()
async def Shibaku1(ctx, coin1, coin2, coin3, coin4, coin5, coin6):
with open('Shibaku1.json', 'r') as f:
coins_data = json.load(f)
coins_data[ctx.author.id] = (coin1, coin2, coin3, coin4, coin5, coin6)
with open('Shibaku1.json', 'w') as f:
json.dump(coins_data, f)
I tried making an if statement in order to not happen, but it didn't seem to work
Python automatically updates keys in dictionary if they exist.
You should convert ctx.author.id to str using str(ctx.author.id):
#client.command()
async def Shibaku1(ctx, coin1, coin2, coin3, coin4, coin5, coin6):
with open('Shibaku1.json', 'r') as f:
coins_data = json.load(f)
coins_data[str(ctx.author.id)] = (coin1, coin2, coin3, coin4, coin5, coin6)
with open('Shibaku1.json', 'w') as f:
json.dump(coins_data, f)

How can I check if a member ID exists in a JSON?

I am building a bot that should store each member and the corresponding guild in a JSON. This works in the code without problems, but I just can not get one thing right.
Here is the code at first:
async def on_member_join(self, member):
with open("src/member.json", "r", encoding='utf-8') as f:
mlist = json.load(f)
guild = str(member.id)
#try:
#mlist[str(member.guild.id)] = guild
#except KeyError:
#print("KEY ERROR")
#mlist[str(member.guild.id)] = guild
if guild in mlist:
return print("IN LIST")
else:
mlist[str(member.guild.id)] = guild
print("NOT IN LIST")
with open('src/member.json', 'w', encoding='utf-8') as f:
json.dump(mlist, f, indent=2)
When I enter the server with my account the entry is created, but when I then re-enter the server my print("NOT IN LIST") still comes up.
This is how my JSON looks like:
{
"731855252277690XXX": "704712637988077XXX" # GuildID : UserID
}
Does anyone here see my error?
I have already tried the following:
Reverse search: if guild not in raidlist
try and except statements included
removed str and converted member.id or guild.id to something else
What you're doing right now is storing each guild and user as a single key-value pair. In other words each guild can only store one member. What we can do is to use lists:
async def on_member_join(self, member):
with open("src/member.json", "r", encoding='utf-8') as f:
mlist = json.load(f)
# changed to member id so it's less confusing
memberid = str(member.id)
guildid = str(member.guild.id)
if guildid in mlist:
if memberid in mlist[guildid]: # the member has already been recorded
return
mlist[guildid].append(memberid) # add the member to that guild's list
else:
mlist[guildid] = [memberid] # making a list with 1 element
with open('src/member.json', 'w', encoding='utf-8') as f:
json.dump(mlist, f, indent=2)
You should clear the JSON before running this code because it's incompatible with the format you had before.
This way the json will look like:
{
'GUILDID': ['MEMBERID1', 'MEMBERID2]
}

How can I store various users in my json file for a discord.py command?

This is my code, and the current problem is that it only saves the latest change to my users.json file.
#commands.command()
async def command(self, ctx):
userid = ctx.message.author.id
with open('users.json', "r", encoding="utf8") as f:
userdata = json.load(f)
try:
with open('users.json', "w", encoding="utf8") as f:
userdata[str(userid)]["coins"] = userdata[str(userid)]["coins"] + 1
json.dump(userdata, f, sort_keys=True, indent=4, ensure_ascii=False)
except:
with open('users.json', "w", encoding="utf8") as f:
userdata = {}
userdata[str(userid)] = {}
userdata[str(userid)]["coins"] = 0
json.dump(userdata, f, sort_keys=True, indent=4, ensure_ascii=False)
Example: I use the command and this is what is stored in the file:
{
"idnumber": {
"coins": 0
}
}
But if someone else does it my variable coins is replaced by theirs leaving just:
{
"theiridnumber": {
"coins": 0
}
}
Any fix?
From your code it appears that you have used try-except just to see if a key is not present etc. This kind of usage is quite unnecessary (and needlessly complex too) considering you can do the same quite simply with various dictionary methods get, setdefault etc.
Try this:
#commands.command()
async def command(self, ctx):
userid = str(ctx.message.author.id)
with open('users.json', "r", encoding="utf8") as f:
userdata = json.load(f)
userdata.setdefault(userid, {}) # if key exists do nothing else set as dictionary, also returns value for key
# dict.get(key, alternate) either gets the value for key or returns the alternate given
userdata[userid]["coins"] = userdata[userid].get("coins", -1) + 1
with open('users.json', "w", encoding="utf8") as f:
json.dump(userdata, f, sort_keys=True, indent=4, ensure_ascii=False)

discord.py : Sort message ID in a JSON file by user

I'm currently in the process of making a kind of ticket system. I want to do that through a reaction.
When a user clicks on the reaction, a message is sent to a specific channel. This message ID is saved in a JSON file and should be assigned to the user who clicked on the reaction. But if I save the user and the message individually in the JSON file, I don't know which user the message belongs to. How can I assign the message ID to a user?
My Code:
#client.event
async def on_raw_reaction_add(reaction):
ticketmess = 716263100638035969
ticketlog = client.get_channel(716265124771397662)
user = client.get_user(reaction.user_id)
if reaction.message_id == ticketmess and reaction.emoji.id == 680827797165572117:
with open(r'./tickets.json', 'r')as f:
ticketchannels = json.load(f)
if not f"{reaction.user_id}" in ticketchannels:
ticketmess = await ticketlog.send('Ein User braucht Hilfe!')
with open(r'./tickets.json', 'r')as f:
ticketmessages = json.load(f)
ticketmessages[f"{reaction.user_id}"] = f'{user} in {ticketmess.id}'
with open(r'./tickets.json', 'w') as f:
json.dump(ticketmessages, f, indent=4)
The code would look something like:
#client.event
async def on_raw_reaction_add(reaction):
ticketmsg = 716263100638035969
ticketlog = client.get_channel(716265124771397662)
user = client.get_user(reaction.user_id)
if reaction.message_id == ticketmsg and reaction.emoji.id == 680827797165572117:
# you already have loaded it in once, you don't need to read it again
with open("tickets.json", "r") as fp:
data = json.load(fp)
if f"{reaction.user_id}" not in data.keys():
ticketmsg = await ticketlog.send("Ein User braucht Hilfe!")
# you can just assign the message id to the user, no need for a string
data[f"{reaction.user_id}"] = ticketmsg.id
with open("tickets.json", "w+") as fp:
json.dump(data, fp, sort_keys=True, indent=4) # kwargs are for beautification
You don't need the f"{user} in {ticketmsg.id}" because you can get the user from the key. Besides, usernames can change, so putting it in the file would prove to be not very useful.
If you wish to get the user's name, just load in the keys from the json and use client.get_user(ID_HERE).
If you want to search for a user based off of a specific message ID, you can iterate through a dictionary like so:
my_dict = {"key": "value", "foo": "bar"}
for k, v in my_dict.items():
if v == "bar":
print(k) # selecting the key based off of its value
# output:
# foo
I'm hoping I understood your question correctly, if not, I'll be happy to edit my answer after some clarification.

Categories