0
votes

So I had a discord bot I was working on a while ago and I finally got around to moving it to my new pc but it randomly decided that my unchanged code had a problem when it worked perfectly before moving it.

This is the code in question:

import discord
import asyncio
from discord.ext import commands
import os
from discord.utils import get
from dotenv import load_dotenv

load_dotenv()

currentcreator = 0

DISCORD_TOKEN = os.getenv("Token")

print("Logged on")

bot = commands.Bot(command_prefix="/")

@bot.command(name="embed")
@commands.cooldown(1, 30, commands.BucketType.user)
async def some_crazy_function_name(ctx, arg):
        argument = arg
        embed=discord.Embed(title=argument, url="https://www.test.com/", description="Description", color=0x3b05ff)
        embed.set_author(name="Author Name")
        embed.add_field(name="Field Name", value="Field Value", inline=False)
        await ctx.send(embed=embed)

@bot.command(name="start")
async def some_crazy_function_name(ctx):
        if not currentcreator == 0:
                await message.channel.send("Someone is already making a profile, please wait")
        currentcreater = ctx.author
        dir = r'C:\\Users\\FiercePC\Desktop\DiscordMMO\User-Profiles'
        MessageAuthor = str(ctx.author.id)
        ProfileDIR = os.path.join(dir,MessageAuthor)
        doesExist = os.path.exists(ProfileDIR)
        if doesExist == False:
                embed=discord.Embed(title="Creating Profile", url="", description=MessageAuthor+", your profile is being created", color=0x3b05ff)
                await ctx.send(embed=embed)
                os.makedirs(ProfileDIR,1)
                file = os.path.join(ProfileDIR,"Level.txt")
                open(file,"x")
                openfile = open(file,"w")
                openfile.write("0")
                openfile.close()
                file = os.path.join(ProfileDIR,"EXP.txt")
                open(file,"x")
                embed=discord.Embed(title="Creating Profile", url="", description=MessageAuthor+", your profile is being created", color=0x3b05ff)
                openfile = open(file,"w")
                openfile.write("0")
                openfile.close()
                file = os.path.join(ProfileDIR,"Strength.txt")
                open(file,"x")
                openfile = open(file,"w")
                openfile.write("1")
                openfile.close()
                embed=discord.Embed(title="Basic Profile Created", url='', description=MessageAuthor+', please use "/races" for a list of races, use "/raceinfo (Insert race here)" to view what bonus races give and finally use "/setrace (Insert race here)" to choose your race"', color=0x3b05ff)
                currentcreator = 0

@bot.command(name="races")
async def some_crazy_function_name(ctx):
        embed=discord.Embed(title="Race List", url="", description="Human,", color=0x3b05ff)
        await ctx.send(embed=embed)                

@bot.command(name="raceinfo")
async def some_crazy_function_name(ctx, arg):
        raceInfo = arg
        if raceInfo == "Human" or "human":
                embed=discord.Embed(title="Human Information", url="", description="Humans spread in this galaxy from their home planet of Coruscant, they are very good at adapting to all conditions thrown at them. Humans begin with a bonus 2 points in natural protection (reduced damage to natural elements like blizzards, sandstorms, freezing, etc) and they also begin with an extra point in speed (increased walking speed). Humans have a 7% initial force chance but it only increses by 0.34% when they level up (this bonus is only added up to level 21 when the final force roll takes place, if it fails you are permanently force un-sensetive)", color=0x3b05ff)        
                await ctx.send(embed=embed)

@bot.command(name="setrace")
async def some_crazy_function_name(ctx, arg):
        embed=discord.Embed(title="Human Information", url="", description="Human,", color=0x3b05ff)
        await ctx.send(embed=embed)

                
@bot.command(name="test")
async def foo(ctx, arg):
        await ctx.send(arg)
        await ctx.send(ctx.author)

bot.run('Token')


  

And this is the error message:

Ignoring exception in command start: Traceback (most recent call last): File "C:\Users\FiercePC\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "C:\Users\FiercePC\OneDrive\Desktop\Star_Wars_Combine-cord\Scripts\Character_Creator.py", line 30, in some_crazy_function_name if not currentcreator == 0: UnboundLocalError: local variable 'currentcreator' referenced before assignment

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Users\FiercePC\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke await ctx.command.invoke(ctx) File "C:\Users\FiercePC\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\FiercePC\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'currentcreator' referenced before assignment

1

1 Answers

0
votes

You cannot refer to a global variable inside a function without explicitly specifying that:

@bot.command(name="start")
async def some_crazy_function_name(ctx):
        global currentcreator
        if not currentcreator == 0:

If you don't state that you're referring to a global variable, Python will look for that variable name only inside the function block, and throw an UnboundLocalError if it doesn't find the assignment.