Поэтому я пытаюсь сделать так, чтобы вы могли видеть количество команд, которые были запущены на боте, но я получаю сообщение об ошибке

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

Ошибка, которую я получаю, связана со строкой total_commands = total_commands + 1:

локальная переменная «total_commands», определенная в охватывающей области в строке 8, на которую ссылается перед назначением

Если бы кто-то мог помочь, было бы здорово :)


person Eon    schedule 26.05.2021    source источник
comment
Я подозреваю, что это связано с тем, что вы объявили total_commands во внешней области. вы можете захотеть внести это в функцию, используя global total_commands, но, честно говоря, когда именно вам нужно это сделать, это смущает даже меня. Тем не менее, это не ошибка в строке выше, так что... странно. Попробуйте поменять его на total_commands += 1 вместо этого?   -  person ch4rl1e97    schedule 27.05.2021


Ответы (1)


попробуйте разместить объявление/инициализацию переменной total_commands в строке 1 после импорта.

rom discord.ext import commands

total_commands=0
bot = commands.Bot(command_prefix=".")
bot.remove_command('help')
total_usage = 0
total_users = 0
person coding_bot    schedule 26.05.2021
comment
теперь он дает мне локальную переменную ошибки «total_commands», определенную в охватывающей области в строке 3, на которую ссылается перед назначением - person Eon; 26.05.2021
comment
Вам нужно global total_commands - person Barmar; 01.06.2021