0

I have a code right now that I'm working on and for some reason whenever I run it my error is "local variable 'buzzcoins' referenced before argument".

As you can see buzzcoins is defined out of a function therefore it should be global? If anyone could help me with this that would be a huge help!

MY CODE:

import os
import random
import time
from keepalive import keep_alive
from discord.ext import commands

#COINS
buzzcoins = 1000

#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")

#ON_READY MESSAGE
@client.event
async def on_ready():
  await client.change_presence(activity=discord.Game("#commands"))
  print("Bot is now running...")

#DICE COMMAND
@client.command()
async def dice(message, amount:int):
  #DICE COMMAND - CHECKS
  if amount > buzzcoins:
    await message.channel.send("You bet more Buzz Coins then this server has!")
  elif amount > 500:
    await message.channel.send("You cannot bet more than 500 Buzz Coins at a time!")
  elif amount == 0:
    await message.channel.send("You cannot bet 0 Buzz Coins!")
  elif amount < 0:
    await message.channel.send("You cannot bet less than 0 Buzz Coins!")
  else:
    await message.channel.send(f"Betting {amount} Buzz Coins!")
    time.sleep(1)
    await message.channel.send(":game_die: Rolling the dice... :game_die:")

    playerdicenumber1 = random.randint(1, 6)
    playerdicenumber2 = random.randint(1, 6)
    time.sleep(3)
    await message.channel.send(f"You roll a {playerdicenumber1} and a {playerdicenumber2}!")

    botdicenumber1 = random.randint(1, 6)
    botdicenumber2 = random.randint(1, 6)
    time.sleep(3)
    await message.channel.send(f"Your opponent rolls a {botdicenumber1} and a {botdicenumber2}!")
    time.sleep(1)
  
  #DICE COMMAND - WIN
  if playerdicenumber1 + playerdicenumber2 > botdicenumber1 + botdicenumber2:
    await message.channel.send(f"You won {amount*2} Buzz Coins! :smile:")
    buzzcoins = buzzcoins + amount
    time.sleep(1)
    await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")

  #DICE COMMAND - LOSE
  elif playerdicenumber1 + playerdicenumber2 < botdicenumber1 + botdicenumber2:
    await message.channel.send(f"You lost {amount} Buzz Coins! :cry:")
    buzzcoins = buzzcoins - amount
    time.sleep(1)
    await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")

  #DICE COMMAND - TIE
  elif playerdicenumber1 + playerdicenumber2 == botdicenumber1 + botdicenumber2:
    await message.channel.send(f"You tied! Your bet has been returned! :ok_hand:")
    time.sleep(1)
    await message.channel.send(f"This server now has {buzzcoins} Buzz Coins!")

#KEEP_ALIVE
keep_alive()
my_secret = os.environ['token']
client.run(my_secret)

3 Answers 3

1

You need to specify what it is global:

  ...
  async def dice(message, amount:int):
    #DICE COMMAND - CHECKS
    global buzzcoins # add this line
    if amount > buzzcoins:
  ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you Vladislav! I just did buzzcoins = globals()['buzzcoins'] and it worked!
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You need to add global buzzcoins inside the function. You are getting the error because right now, the interpreter is trying to find a definition of buzzcoins inside the function and not in the global scope.

Comments

0

Instead of using global, make your variable a class variable of the bot object

i.e: instead of this,

#COINS
buzzcoins = 1000

#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")

do this,

#COMMAND PREFIX
client = commands.Bot(command_prefix = "!")\

#COINS
client.buzzcoins = 1000

then you can access it without using the global keyword in your function:

async def dice(ctx, amount:int):
    #DICE COMMAND - CHECKS
    if amount > client.buzzcoins:
       client.buzzcoins += 100 # example

also message in your function is not a discord.Message object, but a commands.Context object. You can use shortcut for send, i.e, await ctx.send("blah blah") istead await ctx.channel.send("blah blah")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.