1

I'm programming a discord bot using Discord API. I'm trying to do a translation command from English to Spanish, but when I run my code, I get the following error:

UnboundLocalError: local variable 'Spanish' referenced before assignment

My code:

if message.content.upper().startswith("P$LANG"):
    lang = message.content[7:]
    """
    user1 = {
        "Language" : lang = message.content[7:],
        "ID" : user == message.author.id,
    }
    """
    global Spanish
    Spanish = []

    if lang.upper() == "SPANISH" or lang.upper() == "SPAIN" or lang.upper() == "ESPAÑOL":
        xcm = "<@!" + message.author.id + "> Ahora Phantom está en Español. Ten en cuenta que los comandos seguiran siendo en Inglés, solo que la respuesta será en Español."
        Spanish.append(message.author.id)
        await client.send_message(channel, xcm)
if message.content.upper().startswith('P$POLLHELP'):
  # await client.add_reaction(message, ":radio_button:")
    if message.author.id in Spanish:
        await client.send_message(
            channel,
            "**Uso:** P$~<x>poll <pregunta> | reemplaza <x> con el número, min - 1 || máx - 5 |-|-| reemplaza <pregunta> con tu pregunta.*"
        )
    else:
        await client.send_message(
            channel,
            "**Usage:** P$~<x>poll <question> | replace the <x> with the number, min - 1 || max - 5 |-|-| replace <question> with your question.*"
        )
    print("P$pollhelp was used by " + str(author.name))

Expected Result: Adds user ID to list 'Spanish'.

Actual Result: It doesn't do anything

edit: When I add global Spanish to my code it gives me the following error:

NameError: name 'Spanish' is not defined
amodrono
  • 1,562
  • 2
  • 19
  • 40
  • 1
    You should define `Spanish` outside the the `if` statement, and if they are globals preferably after your imports at the beginning of the module. – metatoaster May 09 '18 at 06:35

1 Answers1

1

As metatoaster stated, you need define the global Spanish array outside of the if statement.

global Spanish
Spanish = []

if message.content.upper().startswith("P$LANG"):
lang = message.content[7:]
"""
user1 = {
    "Language" : lang = message.content[7:],
    "ID" : user == message.author.id,
}
"""


if lang.upper() == "SPANISH" or lang.upper() == "SPAIN" or lang.upper() == "ESPAÑOL":
    xcm = "<@!" + message.author.id + "> Ahora Phantom está en Español. Ten en cuenta que los comandos seguiran siendo en Inglés, solo que la respuesta será en Español."
    Spanish.append(message.author.id)
    await client.send_message(channel, xcm)
if message.content.upper().startswith('P$POLLHELP'):
  # await client.add_reaction(message, ":radio_button:")
    if message.author.id in Spanish:
        await client.send_message(
            channel,
            "**Uso:** P$~<x>poll <pregunta> | reemplaza <x> con el número, min - 1 || máx - 5 |-|-| reemplaza <pregunta> con tu pregunta.*"
        )
    else:
        await client.send_message(
            channel,
            "**Usage:** P$~<x>poll <question> | replace the <x> with the number, min - 1 || max - 5 |-|-| replace <question> with your question.*"
        )
    print("P$pollhelp was used by " + str(author.name))
WeDa Beast
  • 44
  • 2
  • 9