HTTPs
HTTPs

Reputation: 19

Change variable in comparison statement inside discord.py command

I need to make a command for a discord bot in Python, which will work according to this algorithm: when the command is executed, the bot will display a random message from the list, and then the content of this message will be checked in some way, if it matches the condition, changes will be applied to the variable. I did this by checking a variable with a random sentence for this very sentence in the list and then changing another membercount variable that the bot will output later. But the code does not work, an error appears saying that you cannot assign a value to the local variable membercount from the command. I tried to place if for a command, but then konfeticount is not found. It remained to take it out of the command so that it was updated automatically, I put it in the while True loop with a delay, but nothing came of it. Please help me deal with this problem. Thanks in advance! The code:

membercount = 0

job = ['Вы проработали в пиццерии и испекли 40 пицц за что получили **220** Монет.', 'Вы проработали мойщиком окон и получили **190** Монет.', 'Вы проработали кассиром в ларьке с мороженным и получили **100** Монет', 'Вы проработали мойщиком посуды в элитном ресторане и получили **280** Монет.', 'Вы проработали на стройке нося кирпичи и получили **230** Монет.', 'Вы проработали установщиком окон и получили **310** Монет.']

@bot.command()
async def j(ctx):
    konfeticount = str(random.choice(job))

    if konfeticount == job[0]:
        membercount += 220

    embed = discord.Embed(color = 0xff0000, description = f"{ctx.message.author.name} {konfeticount}", title = 'Работа')
    await ctx.send(embed = embed)

# Balance
@bot.command()
async def ballance(ctx):
    embed = discord.Embed(color = 0xff0000, description = f"{ctx.message.author.name} Ваш баланс - **{membercount}**", title = 'Баланс')
    await ctx.send(embed = embed)

Upvotes: 0

Views: 290

Answers (1)

Shunya
Shunya

Reputation: 2429

Your problem arises because membercount is a global variable, but in Python global variables are designed to be read only. So when you try to assign membercount += 220 what Python is doing is creating a new local variable called membercount.

The correct way to modify a global variable from inside a function is to declare it as global, so your code should be like this:

membercount = 0

job = ['Вы проработали в пиццерии и испекли 40 пицц за что получили **220** Монет.', 'Вы проработали мойщиком окон и получили **190** Монет.', 'Вы проработали кассиром в ларьке с мороженным и получили **100** Монет', 'Вы проработали мойщиком посуды в элитном ресторане и получили **280** Монет.', 'Вы проработали на стройке нося кирпичи и получили **230** Монет.', 'Вы проработали установщиком окон и получили **310** Монет.']

@bot.command()
async def j(ctx):
    global membercount   # declare membercount to be a global variable
    konfeticount = str(random.choice(job))

    if konfeticount == job[0]:
        membercount += 220  # here we assign a value to the global variable membercount

    embed = discord.Embed(color = 0xff0000, description = f"{ctx.message.author.name} {konfeticount}", title = 'Работа')
    await ctx.send(embed = embed)

# Balance
@bot.command()
async def ballance(ctx):
    embed = discord.Embed(color = 0xff0000, description = f"{ctx.message.author.name} Ваш баланс - **{membercount}**", title = 'Баланс')
    await ctx.send(embed = embed)

Upvotes: 1

Related Questions