【发布时间】:2018-05-09 15:01:27
【问题描述】:
我正在使用 Discord API 编写一个不和谐机器人。我正在尝试执行从英语到西班牙语的翻译命令,但是当我运行我的代码时,出现以下错误:
UnboundLocalError: local variable 'Spanish' referenced before assignment
我的代码:
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))
预期结果: 将用户 ID 添加到列表“西班牙语”。
实际结果: 它没有任何作用
编辑:
当我将global Spanish 添加到我的代码中时,它给了我以下错误:
NameError: name 'Spanish' is not defined
【问题讨论】:
-
您应该在
if语句之外定义Spanish,如果它们是全局变量,最好在模块开头的导入之后。
标签: python discord.py