语法错误:名称'moneyArray'在全局声明之前使用

时间:2019-11-14 22:13:03

标签: discord.py

我正试图使我的不和谐机器人(discord.py)每天给人们钱,并读出他们的余额。问题是我对一般的编程和discord.py API相对缺乏经验。这是实现此功能的最佳选择(我尚未添加每日计时器)。问题是,当我运行此命令时,标题出现错误。在堆栈溢出中查找了类似的错误后,我尝试在该函数的开头添加一个全局定义,但它仍然给了我错误。谁能看到这个问题?帮助将不胜感激!

@client.event
async def on_message(message):
    global moneyArray
    moneyArray = []
    peopleArray = []
    peopleArray.append(message.author.name)
    print(peopleArray)
    for x in message.guild.members:
        moneyArray.append(money)
    peopleid = peopleArray.index(message.author.name)
    print(peopleArray)
    #blah blah some more code
    if message.content.startswith('ub!bal'):
        peopleid = peopleArray.index(message.author.name)
        await message.channel.send('You have {} points!'.format(moneyArray[peopleid]))
    if message.content.startswith('ub!daily'):
            global moneyArray
            moneyArray[peopleid] += 10
            print(moneyArray[peopleid])
            await message.channel.send('10 points added!')

如果需要,我可以共享我的整个代码

1 个答案:

答案 0 :(得分:0)

尝试删除第16行中重复的global moneyArray行。

您的代码应如下所示:

@client.event
async def on_message(message):
    global moneyArray
    moneyArray = []
    peopleArray = []
    peopleArray.append(message.author.name)
    print(peopleArray)
    for x in message.guild.members:
        moneyArray.append(money)
    peopleid = peopleArray.index(message.author.name)
    print(peopleArray)
    #blah blah some more code
    if message.content.startswith('ub!bal'):
        peopleid = peopleArray.index(message.author.name)
        await message.channel.send('You have {} points!'.format(moneyArray[peopleid]))
    if message.content.startswith('ub!daily'):
            moneyArray[peopleid] += 10
            print(moneyArray[peopleid])
            await message.channel.send('10 points added!')