0

Im programing a Discord bot and and have some functions depending on the method. I have one bot.event that search for all messages and apply its function, and one bot command that only search for the command !balance and apply its function. The problem is that the bot.command is not working as the method on_message is acting first. How can I write that on the method on message the program exclude the commands??

@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


should I use other method?

Park
  • 2,446
  • 1
  • 16
  • 25
  • 2
    Does this answer your question? [Why does on\_message stop commands from working?](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – Łukasz Kwieciński Jan 28 '22 at 08:50

1 Answers1

0

When you have a @bot.event decorated function and you want the bot to process commands too, you have to add this line at the end of your on_message function, making sure that this line is always reached by the program.

await bot.process_commands(message)

This will process the commands like if there was no event listener for message event.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28