Home > OS >  How to increment a value to a specific list when that name is called
How to increment a value to a specific list when that name is called

Time:12-13

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.

CodePudding user response:

First this line should be updated if ctx.message.content == "Tali" or "tali": to if ctx.message.content in ("Tali", "tali"): the same for all other conditions.

I hope, I answered the question.

CodePudding user response:

I prefer using dictionary to do this

people = ["robert", "tali", "alex", "justin"]
people_dic = {}
for person in people:
    people_dic[person] = 0
@bot.command()
async def cap(ctx, arg):
    if arg.lower() in people:
        people_dic[arg.lower()]  = 1
        print(people_dic)
        await ctx.send(f'{arg} has capped {people_dic[arg.lower()]} time(s).')
    else:
        await ctx.send("I dont know this person")

but the problem that everything will be deleted after u close the bot if u want to save it u can ask me to do so

  • Related