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'))
我尝试使用discord.py创建一个不和谐机器人。机器人上线了,但不回应我的信息。你能告诉我怎么了吗?
你似乎有一个缩进错误:
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')
最后一个if语句永远不会被执行。相反,将它向后移动一个缩进,这样:
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')