我正在尝试使用discord.py将货币命令添加到我的Discord机器人。这是我的代码:
@client.command()
async def balance(ctx):
await open_account(ctx.message.member)
user = ctx.message.member
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=discord.Color.red())
em.add.field(name="Wallet balance", value=wallet_amt)
em.add.field(name="Bank balance", value=bank_amt)
await ctx.send(embed=em)
@client.command()
async def beg(ctx):
await open_account(ctx.message.member, )
users = await get_bank_data()
user = ctx.message.member
earnings = random.randrange(101)
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(ctx):
user = ctx.message.member
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)]["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
但是当我使用balance命令时,我得到了这个错误:
错误:命令引发异常:属性错误:“Message”对象没有属性“member”
2条答案
按热度按时间5uzkadbs1#
这是因为您需要使用
ctx.author
来获取消息作者。nfs0ujit2#
这是因为www.example.com中的
message
对象discord.py没有member
属性。因此,您需要使用message.author
。