0

I am making a Groot discord bot, and it is supposed to grow every time someone asks it a question. Every hundred questions, Groot goes up a year. I am using replit, and I am not that advanced when it comes to coding.

import os
import requests
import json
import random



client = discord.Client()

i_am_groot = ("Hey Groot", "Groot", "Twig", "Tree")


client = discord.Client()

groot_age = 0
groot_pings = 100

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
        return
  if message.content.startswith(i_am_groot):
        await message.channel.send('I am groot')
        groot_pings = groot_pings + 1
        groot_age = groot_pings // 100
        await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='Mr Blue Sky and Dancing | Age: ' + str(groot_age) + ' | Pings: ' + str(groot_pings)))
  if message.content.startswith ('grootage'):
        await message.channel.send ('Translation: ' + str(groot_age))
  if message.content.startswith ('grootpings'):
        await message.channel.send ('Translation: ' + str(groot_pings))



import keep_alive

keep_alive.keep_alive()
    



client.run(os.getenv('TOKEN'))

I get this error:

[pyflakes] local variable 'groot_pings' defined in enclosing scope on line 19 referenced before assignment

but it is defined.

Is there any way to fix this?

Help?

  • 1
    Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – c2huc2hu Mar 31 '21 at 18:45

1 Answers1

0

Try this code:

import os
import requests
import json
import random



client = discord.Client()

i_am_groot = ("Hey Groot", "Groot", "Twig", "Tree")


client = discord.Client()

groot_age = 0
groot_pings = 100

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  global groot_age
  global groot_pings
  if message.author == client.user:
        return
  if message.content.startswith(i_am_groot):
        await message.channel.send('I am groot')
        groot_pings = groot_pings + 1
        groot_age = groot_pings // 100
        await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='Mr Blue Sky and Dancing | Age: ' + str(groot_age) + ' | Pings: ' + str(groot_pings)))
  if message.content.startswith ('grootage'):
        await message.channel.send ('Translation: ' + str(groot_age))
  if message.content.startswith ('grootpings'):
        await message.channel.send ('Translation: ' + str(groot_pings))



import keep_alive

keep_alive.keep_alive()
    



client.run(os.getenv('TOKEN'))
Jellyfish
  • 304
  • 3
  • 13