【发布时间】:2022-02-07 01:40:06
【问题描述】:
我正在编写一个 Discord 机器人,并且根据方法具有一些功能。我有一个 bot.event 搜索所有消息并应用其功能,以及一个仅搜索命令 !balance 并应用其功能的 bot 命令。问题是 bot.command 无法正常工作,因为方法 on_message 首先起作用。我怎样才能在程序排除命令的消息上写这个方法??
@bot.event
async def on_message(ctx):
await open_account(ctx.author)
users = await get_bank_data()
user = ctx.author
earnings_1 = 1
users[str(user.id)]['wallet'] += earnings_1
with open('bank.json', 'w') as f:
json.dump(users, f)
@bot.command(pass_context=True)
async def balance(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
wallet_amt = users[str(user.id)]['wallet']
em = discord.Embed(
title = f"{ctx.author.name}´s balance:",
color = 0xf3e577
)
em.add_field(name = 'Wallet Balance', value = wallet_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'] = 0
users[str(user.id)]['bank'] = 0
with open('bank.json', 'w') as f:
json.dump(users, f)
return True
async def get_bank_data():
with open('bank.json','r') as f:
users = json.load(f)
return users
我应该使用其他方法吗?
【问题讨论】:
标签: python discord.py bots