如果发出命令,我如何让discordbot发送消息



我正在使用Python制作一个discord bot,但应该运行的命令都没有运行。我已经追踪到下面的if命令:

if message.content.startswith("$hello"): # <<<<<< This command is causing issues!
print("Command detected: $hello")
await message.channel.send('Hello there! I am a bot. Thats it.')

$hello应该在聊天中说";你好!我是个机器人。就这样;然而,if命令不起作用,即使说$hello,它也不会运行它应该运行的内容。

这是整个代码本身:


import discord
import os
client = discord.Client(intents=discord.Intents(messages=True))

@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
print("Message detected.")
if message.author == client.user:
print("Returning...")
return
print("User message confirmed to not be a bot.")
if message.content.startswith("$hello"): #<<< This is causing issues!
print("Command detected: $hello")
await message.channel.send('Hello there! I am a bot. Thats it.')
# imagine there is a run command here, with the token
# im not putting it here since its a security risk
# the run bot command works

我尝试过使用不同类型的命令,比如startswith和==contains,但它不会打印hello消息。有人知道如何让它工作吗?

Intents.messagesIntents.message_content不同!

消息:

是否启用公会和直接消息相关事件。

这是设置或获取guild_messages和dm_messages的快捷方式。

message_content:

消息中是否提供消息内容、附件、嵌入和组件

您缺少message_content

import discord
import os
#make sure to set up your command syntax and set your intents so the bot can read messages
client= commands.Bot(command_prefix='$', intents=discord.Intents.default())
client.remove_command("help")

#don't use on_message for commands, it's gonna cause problems.
#instead, use the command funtion here
@client.command()
async def command(ctx):
ctx.channel.send("command is working!")
client.run("token")
``` that should work 

最新更新