【问题标题】:python discord bot: multiple on_message?python不和谐机器人:多个on_message?
【发布时间】:2022-01-26 18:21:23
【问题描述】:

我对 python 很陌生,我正在尝试制作一个不和谐的机器人,只要它找到与食物相关的单词就会发送消息。这是我想出的,它在第 147 行给了我一个缩进错误。为什么会这样,我该怎么办?

编辑:我已将代码从 repl.it 复制粘贴到此处

### Alert bot to food words and reply with a message from a list ###
@bot.event
async def on_message(message):
    if 'spag' in message.content:
        await ctx.channel.send(random.choice(spaghet))
  if 'meat' in message.content:
    await ctx.channel.send(random.choice(meat))
  if 'dinner' in message.content:
    await ctx.channel.send(random.choice(dinner_words))
  if 'lunch' in message.content:
    await ctx.channel.send(random.choice(lunch_words))
  if 'breakfast' in message.content:
    await ctx.channel.send(random.choice(breakfast_words))
  if any (word in message.content for word in omit):
    return
  await bot.process_commands(message)

感谢您的帮助

enter image description here

【问题讨论】:

  • 您可能正在混合制表符和空格。错误的确切文本是什么?
  • @MattDMo "IndentationError: unindent 不匹配任何外部缩进级别。"不过,我没有使用任何空格。
  • edit您的问题并将您的代码复制到其中,使用代码格式化工具({})正确格式化。请不要更改任何内容,只需复制并粘贴即可。
  • 哦,将其复制粘贴到此处使我的错误可见。我看到我做了什么,我已经修复了它。谢谢@MattDMo

标签: python discord discord.py


【解决方案1】:
@bot.event
async def on_message(message):
  if 'spag' in message.content:
      await ctx.channel.send(random.choice(spaghet))
  if 'meat' in message.content:
      await ctx.channel.send(random.choice(meat))
  if 'dinner' in message.content:
      await ctx.channel.send(random.choice(dinner_words))
  if 'lunch' in message.content:
      await ctx.channel.send(random.choice(lunch_words))
  if 'breakfast' in message.content:
      await ctx.channel.send(random.choice(breakfast_words))
  if any (word in message.content for word in omit):
      return
  await bot.process_commands(message)

我希望这能解决它,如果它没有尝试选择代码并执行shift+tab 重置缩进并再次选择代码并按tab 重新分配缩进

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 2021-05-27
    • 2021-07-15
    • 2021-11-25
    • 2021-03-29
    • 2021-07-11
    • 2021-03-25
    • 2018-08-13
    相关资源
    最近更新 更多