python - 我是使用 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'))
解决方案
我不确定我是否完全理解这个问题,但我会尽力回答
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 更改为您的命令的名称
推荐阅读
- azure-cosmosdb - 从 NiFi 发布到 Azure Cosmos Db
- sql - 是否可以将与不同行名关联的值添加在一起,同时保持其他行不变?
- ios - 如何为 SCNSphere 选择合适的半径?
- java - 无法在 MacOS 版本 Mojave 10.14.1 上创建新的 netbeans 8.2 项目
- c - 使用 strcpy() 时出现分段错误
- c++ - 带有 tinyfsm 的 C++ 循环类依赖 - 前向声明不起作用
- python - 如何在python中对包含eigvalsh的复杂代码进行矢量化
- javascript - 您可以从 i 元素 onclick 访问事件数据吗?
- c - C Hangman 程序调试辅助(中止陷阱:6 错误)
- oracle - 我们可以创建一个给出随机值的 PLSQL 序列吗?