简体   繁体   English

我的 discord 机器人没有响应我的命令

[英]My discord bot is not responding to my commands

import discord
import os

client = discord.Client(intents=discord.Intents.default())


@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))


@client.event
async def on_message(message):
  if message.author == client.user:
    return

    if message.content.startswith('$hello'):
      channel = message.channel
      await channel.send('Hello!')


client.run(os.getenv('TOKEN'))

I tried to make a discord bot using discord.py. The bot comes online and everything but does not respond to my messages.我尝试使用 discord.py 制作一个 discord 机器人。该机器人上线并且一切正常,但不回复我的消息。 Can you tell me what is wrong?你能告诉我哪里出了问题吗?

You seem to have an indentation error:您似乎有缩进错误:

async def on_message(message):
  if message.author == client.user:
    return

    if message.content.startswith('$hello'):
      channel = message.channel
      await channel.send('Hello!')

The last if-statement is never going to be executed.最后一个 if 语句永远不会被执行。 Instead, move it one indentation back such that:相反,将它向后移动一个缩进,这样:

async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('$hello'):
    channel = message.channel
    await channel.send('Hello!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM