【问题标题】:Discord Bot. ON_MESSAGE How can I stablish exceptions?不和谐机器人。 ON_MESSAGE 如何建立异常?
【发布时间】: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


【解决方案1】:

当你有一个 @bot.event 装饰函数并且你希望 bot 也处理命令时,你必须在你的 on_message 函数的末尾添加这一行,确保这一行总是由程序。

await bot.process_commands(message)

这将处理类似message 事件没有事件监听器的命令。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 2018-03-20
    • 2020-12-28
    • 1970-01-01
    • 2023-03-31
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    相关资源
    最近更新 更多