-1
@bot.command()
async def strike(ctx, member : discord.Member):
    count = 0
    if (member.id == <id here>) and (count < 3): 
       count = count + 1

i am pretty new to python in general and have only used it to create a discord bot. When the strike command is run, it sets count = to 0 and therefore the count variable cannot go higher than 1. If I move the variable assignment outside of the command I get an error saying that the variable was referenced before it was assigned.

How can I change my code to add 1 to the variable every time the command is run without reassigning the variable every time the command is run?

I removed a lot of the code so the question is less cluttered, I believe this is the only issue preventing this command from working.

Le Tank
  • 1
  • 3
  • Does this answer your question? [UnboundLocalError on local variable when reassigned after first use](https://stackoverflow.com/questions/370357/unboundlocalerror-on-local-variable-when-reassigned-after-first-use) – TheFungusAmongUs Aug 07 '22 at 07:10
  • Is `count` also a global somewhere? Can't repro, works fine when I tried it. – Eric Jin Aug 07 '22 at 12:46

1 Answers1

0

You need to make your variable as a global variable.

bot.count = 0

@bot.command()
async def strike(ctx, member : discord.Member):
    if (member.id == <id here>) and (bot.count < 3): 
       bot.count += 1

You can also do:

count = 0
@bot.command()
async def strike(ctx, member : discord.Member):
    global count
    if (member.id == <id here>) and (count < 3): 
       count += 1

I don't know what are you trying to do here but I'm only fixing your code

Ruri
  • 139
  • 12