My code:
reps = {}
with open("reps.json", "r") as f:
reps = json.load(f)
#client.command()
async def rep (ctx, member: discord.Member):
try:
count = reps[f'{ctx.guild.id}'][f'{member.id}']
reps[f'{ctx.guild.id}'][f'{member.id}'] = count + 1
with open('reps.json', 'w') as f:
json.dump(reps, f)
await ctx.message.add_reaction('<a:check12:810961073746345985>')
except:
reps[f'{ctx.guild.id}'] = {}
reps[f'{ctx.guild.id}'][f'{member.id}'] = 1
with open('reps.json', 'w') as f:
json.dump(reps, f)
await ctx.message.add_reaction('<a:check12:810961073746345985>')
The problem is that when I use this command my json file looks like:
{"GUILD_ID": {"MEMBER_ID": 1}}
It looks fine but when I try to use this command on a other member json file does not add another dictionary it just changes the name of a dict to a another member id so the file looks like this:
{"GUILD_ID": {"ANOTHER_MEMBER_ID": 1}}
And what I expect is:
{"GUILD_ID": {"MEMBER_ID": 1}}
And after using command on another member:
{"GUILD_ID": {"MEMBER_ID": 1, "ANOTHER_MEMBER_ID": 1}}
Related
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)
What i'm trying to achive is to make three simple commands:
!add nick
!delete nick
!list (of nicknames)
This is my not-so-well-working script:
import json
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
def add(name):
list.append( name )
#bot.command()
async def dele(ctx, *args):
with open('list.json', 'r') as f:
lines = json.loads(f.read())
for name in args:
lines.remove(name)
with open('list.json', 'w') as f:
f.write(json.dumps(lines))
#bot.command()
async def add(ctx, *args):
with open('list.json', 'r') as f:
lines = json.loads(f.read())
for name in args:
lines.append(name)
with open('list.json', 'w') as f:
f.write(json.dumps(lines))
#bot.command()
async def list(ctx):
with open('list.json', 'r') as f:
list = json.loads(f.read())
await ctx.channel.send(f"List {list}")
bot.run("MY TOKEN HERE")
Command !list output:
list.json
["test1", "test2", "test3", "test", "4", "test4", "name", "name", "test", "5", "test", "6"]
So... adding and deleting nicknames works fine (except that when i'm trying to add nickname with spaces its adding two separate keys).
But the biggest problem i got with the list command. Don't know how to format this to some kind of list with \n nicknames showing one by one or even with discord embed thing.
Please help.
Spaces
(except that when i'm trying to add nickname with spaces its adding two separate keys)
Remember that when you write two words separated by space, the commands.command takes them as two arguments. You must write them without spaces.
One parameter function
You can solve your problem editing your functions like this:
#Bot.command()
async def add(ctx, member: str):
with open('list.json', 'r') as f:
lines = json.loads(f.read())
lines.append(member)
with open('list.json', 'w') as f:
f.write(json.dumps(lines))
Embed
You can do something like this:
description = ""
for i in list:
description += f'{i}\n'
Or
description = '\n'.join (list)
Then,
emb = discord.Embed(title="List", description=description, color=discord.Color.random())
await ctx.send(embed=emb)
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]
}
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)
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!")