如何让机器人用(@)ping回复用户



我正试图让一个机器人在消息中用ping来回应人们,例如:"用户";,但我所尝试的一切都给了我CCD_ 1错误或CCD_ 2错误。我能找到的所有关于它的东西要么对discord.jsv14来说已经过时了,要么是对discord.py来说

这是我的代码:

client.on("messageCreate", (message) => {
if (message.content.startsWith("test")) {
const user = message.author.userId();
message.channel.reply(`Hello <@${user}>`)
}
});

我还尝试了.userId()部分的变体,如.tag.user.id.username,但所有这些都返回了某种undefined错误。我知道上面说userId是disconnect.js上的雪花,但我不确定如何使用它,因为我对javascript和disconnect.js还很陌生。此外,请知道我正在使用Replit来托管机器人,并安装了not a function0。

因此,这里有几个问题。

第1期

message.author.userId()不是一个函数。当试图获取用户的ID时,您想知道每个属性返回的是什么。

message->返回Message对象,该对象包含消息的数据。message.author->返回User对象,该对象包含用户配置文件的数据。message.member->返回成员对象,该对象包含公会成员配置文件的数据。

等等。

在这种情况下,您将想要获得User对象:即message.author。你已经想明白了。

现在,User对象有自己的一组属性(您可以在文档中找到https://discord.js.org/)。

您要查找的属性是:.id,或者在您的用例中是message.author.id

在其他尝试中,undefined0返回标记,message.author.user.id将抛出错误,message.author.username返回用户的用户名。

第2期

第二个问题是您正在使用的.reply()方法。Channel对象没有.reply()方法,但消息有。

那么,你将如何编写这个代码:

client.on("messageCreate", async(message) => {
if(message.content.toLowerCase().startsWith("test")) {
// Note that I added the .toLowerCase() method to ensure you can do tEST and it still works!
message.reply({ content: `Hello, ${message.author}!` });
}
});

此外,discord.js的一个很酷的功能是,您可以为消息的内容提供UserMember对象,它会自动提到所需的人。

希望这能有所帮助!

最新更新