Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Discord.py SyntaxError:name';moneyArray';在全局声明之前使用_Discord.py - Fatal编程技术网

Discord.py SyntaxError:name';moneyArray';在全局声明之前使用

Discord.py SyntaxError:name';moneyArray';在全局声明之前使用,discord.py,Discord.py,我试图让我的discord机器人(discord.py)每天给人们一笔钱,并读出他们的余额。问题是我对一般编程和discord.py API相对缺乏经验。这是我能想到的最好的实现方法(我还没有添加每日计时器)。问题是当我运行这个程序时,标题中会出现错误。在查找堆栈溢出的类似错误后,我试图在函数的开头添加一个全局定义,但它仍然给了我错误。有人看到问题了吗?非常感谢您的帮助 @client.event 异步def on_消息(消息): 全球货币阵列 moneyArray=[] peopleArray

我试图让我的discord机器人(discord.py)每天给人们一笔钱,并读出他们的余额。问题是我对一般编程和discord.py API相对缺乏经验。这是我能想到的最好的实现方法(我还没有添加每日计时器)。问题是当我运行这个程序时,标题中会出现错误。在查找堆栈溢出的类似错误后,我试图在函数的开头添加一个全局定义,但它仍然给了我错误。有人看到问题了吗?非常感谢您的帮助

@client.event
异步def on_消息(消息):
全球货币阵列
moneyArray=[]
peopleArray=[]
peopleArray.append(message.author.name)
打印(peopleArray)
对于message.guild.members中的x:
moneyArray.append(货币)
peopleid=peopleArray.index(message.author.name)
打印(peopleArray)
#废话,废话,更多的代码
如果message.content.startswith('ub!bal'):
peopleid=peopleArray.index(message.author.name)
wait message.channel.send('youhave{}points!'.format(moneyArray[peopleid]))
如果message.content.startswith('ub!daily'):
全球货币阵列
moneyArray[peopleid]+=10
打印(moneyArray[peopleid])
wait message.channel.send('增加10分!')

如果需要,我可以共享我的全部代码

尝试删除第16行重复的
全局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!')

尝试删除第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!')

包括整个回溯,或者至少指出哪一行导致了回溯。但是,似乎您只需去掉第二个“全局”即可使此代码正常工作。您只需在client.event外部将其声明为全局变量,我建议您更改存储积分的方式,因为如果两个玩家的名字相同,则可能会重置玩家的积分,从而导致整个回溯不一致,或者至少指出哪一行导致它可能有帮助。然而,似乎您只需去掉第二个“全局”即可使此代码正常工作。您只需在client.event外部将其声明为全局变量,我建议您更改存储点数的方式,因为如果两名玩家在discord上同名,则可能会重置一名玩家的点数