首页 > 解决方案 > 我是使用 python 制作不和谐机器人的新手,但我遇到了麻烦。我发送“$hello”以获得响应,但没有任何反应

问题描述

所以,我使用 replit.com 来制作机器人,并且机器人上线就好了。我输入 $hello 并期望得到“指挥官”的回复。我这样做了,但机器人没有回复。我根据Youtube 上的视频制作了这个脚本。我猜这可能与我的代码或不和谐机器人权限有关。任何帮助/建议将不胜感激,谢谢。

import discord
import os
import time 

client = discord.Client()

@client.event 
async def on_ready():
  print('We have broken into american server farms as {0.user}'.format(client))
  time.sleep(1)
  print("Accessing private data...")
  time.sleep(2)
  print("Installing malware...")
  time.sleep(2)
  print("Extracting stolen files...")
  time.sleep(2)
  print("Finishing up..")
  time.sleep(1)
  print("All objectives completed, State Messenger Bot is now online.")


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

      if message.content.startswith('$hello'):
        await message.channel.send('Commander')

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

标签: pythondiscorddiscord.py

解决方案


我不确定我是否完全理解这个问题,但我会尽力回答

import discord

client = commands.Bot(command_prefix = '$')

@client.event
async def on_ready():
    print("we have powered on, I an alive.")

@client.command()
async def hello(ctx):
    await ctx.send('Commander')

client.run('TOKEN')

最后一部分所做的是等到有人说 $hello 然后回复指挥官。前缀已在开始时定义,因此您无需为每个命令定义前缀。在async def hello(ctx):您可以将 hello 更改为您的命令的名称


推荐阅读