I am making a bot for discord and I want add to someone's value every time a command is called and that specific user's name is used.
tali_cap = [0]
robert_cap = [0]
alex_cap = [0]
justin_cap = [0]
@bot.command()
async def cap(ctx, arg):
## !cap Robert
if ctx.message.content == "Tali" or "tali":
for t in range(len(tali_cap)):
tali_cap[t] = tali_cap[t] + 1
await ctx.send(f'{arg} has capped {tali_cap[-1]} time(s).')
elif ctx.message.content == "Robert" or "robert":
for r in range(len(robert_cap)):
robert_cap[r] = robert_cap[r] + 1
await ctx.send(f'{arg} has capped {robert_cap[-1]} time(s).')
elif ctx.message.content == "Alex" or "alex":
for a in range(len(alex_cap)):
alex_cap[a] = alex_cap[a] + 1
await ctx.send(f'{arg} has capped {alex_cap[-1]} time(s).')
elif ctx.message.content == "Justin" or "justin":
for j in range(len(justin_cap)):
justin_cap[j] = justin_cap[j] + 1
await ctx.send(f'{arg} has capped {justin_cap[-1]} time(s).')
else:
await ctx.send("I don't know that person.")
This is what I've tried. I've gotten the number to increase but that number is the same for every user. Ex. I would type !cap tali and the bot would output: tali has capped 1 times(s). But then I would type !cap robert and the bot would output: robert has capped 2 times(s), even though robert hasn't been called previously. I want it to be different for each user and increment independently.
it is simple. all u need to do to import json and dump the data in the file
import json
@client.command()
async def cap(ctx, arg):
#loading the file
try:
with open("caps.json","r") as f:
people_dic = json.load(f)
#checking for existance
except FileNotFoundError:
open('caps.json','w+').close()
people = ["robert", "tali", "alex", "justin"]
people_dic = {}
for person in people:
people_dic[person] = 0
if arg.lower() in people_dic:
people_dic[arg.lower()] += 1
print(people_dic)
await ctx.send(f'{arg} has capped {people_dic[arg.lower()]} time(s).')
with open("caps.json","w") as f:
people_dic = json.dump(people_dic,f,indent=2)
else:
await ctx.send("I dont know this person")