我试图通过使用齿轮使我的代码更简洁,但它似乎不起作用。当我执行 +balance 时,出现此错误:Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "balance" is not found:
这是 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.
来自余额文件:
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))
我是编码和齿轮方面的新手,所以如果有人知道如何解决这个问题,请帮助我。
答案 0 :(得分:0)
我不知道你展示的命令和函数是否在类中。另外,您为什么要创建一个新的 Bot 实例,为什么您的 init 函数采用一个名为 client
的参数,然后将 self.bot
设置为 bot
?
尝试将 init 函数中的 client
替换为 bot
,反之亦然。
尝试运行主文件时是否遇到任何错误?