1
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix=".")
bot.remove_command('help')
total_commands = 0
total_usage = 0
total_users = 0

@bot.event
async def on_ready():
  print("Bot is ready")

@bot.command()
async def help(ctx):
  await ctx.send("Which help page? say 'commands' for commands 'home' for home page")

  def check(msg):
    return msg.author == ctx.author and msg.channel == ctx.channel and \
        msg.content.lower() in ["home", "commands"]

  msg = await bot.wait_for("message", check=check)
  if msg.content.lower() == "home":
    embed = discord.Embed(title="**Home Menu**", description=f"Total usage: {total_usage}\n\nTotal Commands Ran: {total_commands}\n\nTotal Users: {total_users}", color=discord.Color.from_rgb(0, 191, 255))
    embed.set_footer(icon_url=f"{ctx.author.avatar_url}", text="Seppuku V2.0")
    await ctx.send(embed=embed)
    total_commands = total_commands + 1

  elif msg.content.lower() == "commands":
    embed = discord.Embed(title="**Coming Soon**", description="**Coming Soon**", color=discord.Color.from_rgb(0, 191, 255))
    embed.set_footer(icon_url=f"{ctx.author.avatar_url}", text="Seppuku V2.0")
    await ctx.send(embed=embed)
    total_commands = total_commands + 1

The error I am getting is with the total_commands = total_commands + 1 line:

local variable 'total_commands' defined in enclosing scope on line 8 referenced before assignment

If someone could help that'd be nice :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eon
  • 11
  • 1
  • I suspect it's due to you declaring the `total_commands` in the outer scope. you might want to bring it into the function using `global total_commands` but honestly exactly when you have to do this confuses even me. That said it didn't error on the line above so... weird. Try swapping it for `total_commands += 1` instead? – ch4rl1e97 May 27 '21 at 11:59

1 Answers1

-1

try placing your declaration/initialization of the total_commands variable on line 1 after import.

rom discord.ext import commands

total_commands=0
bot = commands.Bot(command_prefix=".")
bot.remove_command('help')
total_usage = 0
total_users = 0