user10307362
user10307362

Reputation:

Global variable isn't changing value inside event

The command !et_as creates an event where for 10 seconds whenever someone sends a message it increases a counter by one.
At the end of 10 seconds, the bot says how many messages were sent and starts a 3-hour cooldown.
The only problem is, the variable FeastActive that dictates whether or not the event is happening doesn't change after the first !et_as command is issued.

@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

Upvotes: 1

Views: 1013

Answers (1)

Tristo
Tristo

Reputation: 2398

You need to set your global variables outside of your event

FeastActive = False
Feast = True
FeastCount = 0

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

Here is the complete code that works on my end

@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

Upvotes: 1

Related Questions