Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

discord.py - Command not found in cogs

I was trying to make my code a little cleaner by using cogs but it doesn't seem to work. When I do +balance this error comes up: Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "balance" is not found: Here's the code part in main.py:

for file in os.listdir("./cogs"): # lists all the cog files inside the cog folder.
    if file.endswith(".py"): # It gets all the cogs that ends with a ".py".
        name = file[:-3] # It gets the name of the file removing the ".py"
        bot.load_extension(f"cogs.{name}") # This loads the cog.

And from the balance file:

bot = commands.Bot(command_prefix='+')

class Balance(commands.Cog):
    def __init__(self, client):
        self.bot = bot

@commands.command(aliases = ["bal"])
async def balance(self, ctx):
    await open_account(ctx.author)
    user = ctx.author
    users = await get_bank_data()

    wallet_amt = users[str(user.id)]["wallet"]
    #bank_amt = users[str(user.id)]["bank"]

    em = discord.Embed(title = f"{ctx.author.name}'s balance",color = (0x95a5a6), description = f"{wallet_amt} dogecoins")
    #em.add_field(name = "Bank balance",value = bank_amt)
    await ctx.send(embed = em)

async def open_account(user):
  
  users = await get_bank_data()

  if str(user.id) in users:
    return False
  else:
    users[str(user.id)] = {}
    users[str(user.id)]["wallet"] = 250
    users[str(user.id)]["bank"] = 0


  with open("mainbank.json","w") as f:
    json.dump(users,f)
  return True

async def get_bank_data():
    with open("mainbank.json","r") as f:
      users = json.load(f)

    return users


async def update_bank(user,change = 0,mode = "wallet"):
  users = await get_bank_data()
  
  users[str(user.id)][mode] += change

  with open("mainbank.json","w") as f:
    json.dump(users,f)
  return True

def setup(bot):
    bot.add_cog(Balance(bot))

I'm new to coding and cogs especially so if anyone knows how to fix this, please help me.

question from:https://stackoverflow.com/questions/65929468/command-not-found-in-cogs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can't tell if the commands and functions you're showing are inside the class or not. Also, why are you creating a new Bot instance, and why does your init function take an argument called client and then set self.bot to bot?
Try replacing client in your init function with bot, or vice versa.
Do you get any errors when you try to run your main file?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...