我正在尝试为我的不和谐机器人制作一个经济系统。我在 youtube 上学习了一个教程,唯一改变的是我在 cogs 中添加了它。
错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'open_account' is not defined
追溯:
Traceback (most recent call last):
File "C:\Users\liene\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 965, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\liene\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 970, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\liene\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 196, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'open_account' is not defined
错误消息告诉我错误所在的位置:
class Economy(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
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"] = 0
bank_amt = users[str(user.id)]["bank"] = 0
em = discord.Embed(title=f"{ctx.author.name}'s balance", colour = discord.Color.green())
em.add_field(name = "Wallet balance", value = wallet_amt)
em.add_field(name = "Bank balance", value = bank_amt)
await ctx.send(embed=em)
完整代码:
import discord
from discord.ext import commands
import json
import os
import random
from discord.ext.commands import Cog, BucketType
from discord.ext.commands import BadArgument
from discord.ext.commands import command, cooldown
os.chdir("C:\\Users\\liene\\OneDrive\\Dators\\Gabriels\\bot\\Rocketman")
class Economy(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
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"] = 0
bank_amt = users[str(user.id)]["bank"] = 0
em = discord.Embed(title=f"{ctx.author.name}'s balance", colour = discord.Color.green())
em.add_field(name = "Wallet balance", value = wallet_amt)
em.add_field(name = "Bank balance", value = bank_amt)
await ctx.send(embed=em)
@commands.command()
async def beg(self, ctx):
await open_account(ctx.author)
users = await get_bank_data()
user = ctx.author
earnings = random.randrange(123)
await ctx.send(f"Someone gave you {earnings} coins!")
users[str(user.id)]["wallet"] += earnings
with open("mainbank.json","w") as f:
json.dump(users,f)
async def open_account(self, user):
users = await get_bank_data()
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
users[str(user.id)]["wallet"] = 0
users[str(user.id)]["wallet"] = 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
def setup(client):
client.add_cog(Economy(client))
答案 0 :(得分:2)
您的 open_account
函数是 Economy
类的方法。要从同一类的不同方法调用它,您必须执行 await self.open_account(ctx.author)
。