【问题标题】:Global variable isn't changing value inside event全局变量没有改变事件内的值
【发布时间】:2019-02-08 02:02:37
【问题描述】:

命令!et_as 会创建一个事件,在此事件中,每当有人发送消息时,它会在 10 秒内将计数器加一。
在 10 秒结束时,机器人会显示发送了多少条消息并开始 3 小时的冷却时间。
唯一的问题是,指示事件是否发生的变量 FeastActive 在发出第一个 !et_as 命令后不会改变。

@theclient.event
async def on_message(message):
    global FeastActive
    global Feast
    global FeastCount

    if message.author == theclient.user:
        return

    if FeastActive == True: 
        FeastCount += 1
        await theclient.send_message(message.channel, ' NOM NOM NOM')

    if message.content.startswith('!hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await theclient.send_message(message.channel, msg)

    if message.content.startswith('!joke'):
        r = requests.get('https://icanhazdadjoke.com', headers={"Accept":"application/json"}).text
        l = json.loads(r)
        await theclient.send_message(message.channel, "<@" + message.author.id + "> " + l['joke'])

    if message.content.startswith('!et_as'):
        if Feast == True and FeastActive == False:

            Feast = False
            FeastActive = True
            await theclient.send_message(message.channel, "<@" + message.author.id + "> has begun a feast! Hurry, 10 seconds!")
            time2.sleep(10)
            FeastActive = False
            await theclient.send_message(message.channel, "@everyone We have feasted on " + str(FeastCount) +" as! Next feast can start in 3 hours")
            time2.sleep(10800)
            Feast = True

【问题讨论】:

    标签: python function global-variables discord discord.py


    【解决方案1】:

    您需要在事件之外设置全局变量

    FeastActive = False
    Feast = True
    FeastCount = 0
    
    @theclient.event
    async def on_message(message):
      global FeastActive
      global Feast
      global FeastCount        
      #rest of your code
    

    这是我的完整代码

    @theclient.event
    async def on_message(message):
      global FeastActive
      global Feast
      global FeastCount
    
      if message.author == theclient.user:
        return
    
      if FeastActive == True: 
        FeastCount += 1
        await theclient.send_message(message.channel, ' NOM NOM NOM')
    
      if message.content.startswith('!hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await theclient.send_message(message.channel, msg)
    
      if message.content.startswith('!joke'):
        r = requests.get('https://icanhazdadjoke.com', headers={"Accept":"application/json"}).text
        l = json.loads(r)
        await theclient.send_message(message.channel, "<@" + message.author.id + "> " + l['joke'])
    
      if message.content.startswith('!et_as'):
        if Feast == True and FeastActive == False:
    
          Feast = False
          FeastActive = True
          await theclient.send_message(message.channel, "<@" + message.author.id + "> has begun a feast! Hurry, 10 seconds!")
          await asyncio.sleep(10)
          FeastActive = False
          await theclient.send_message(message.channel, "@everyone We have feasted on " + str(FeastCount) +" as! Next feast can start in 3 hours")
          await asyncio.sleep(5)
          Feast = True
    

    【讨论】:

    • 对不起,这只是整个代码的 sn-p。我已经在函数之外声明了它。
    • @VaibhavShrivathsa 他们的初始化方式是和我一样还是不同,因为它似乎对我有用
    • 是的,它的初始化方式相同
    • 我已经编辑了我的答案并将适用于我的代码放在最后。试试看是否有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2019-09-15
    • 2016-06-04
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多