Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何获得提到的用户';来自消息的s id | Discord.js_Node.js_Discord.js - Fatal编程技术网

Node.js 如何获得提到的用户';来自消息的s id | Discord.js

Node.js 如何获得提到的用户';来自消息的s id | Discord.js,node.js,discord.js,Node.js,Discord.js,因此,我正在尝试对我的discord.js bot实现一个警告功能,我想从提到的用户那里获得一个用户ID。我尝试了message.user.id,message.indications.users.first().id和message.indications[0].id,但没有成功 它们都返回TypeError:无法读取未定义的属性“id” 我如何正确地做到这一点 (编辑) 我忘了说我想向提到的用户发送DM。message.indications.users返回一个集合,如果没有提及或提到的用户

因此,我正在尝试对我的discord.js bot实现一个警告功能,我想从提到的用户那里获得一个用户ID。我尝试了
message.user.id
message.indications.users.first().id
message.indications[0].id
,但没有成功

它们都返回
TypeError:无法读取未定义的属性“id”

我如何正确地做到这一点

(编辑)
我忘了说我想向提到的用户发送DM。

message.indications.users返回一个集合,如果没有提及或提到的用户未缓存,则集合可能为空,另一种方法是尝试在消息内容上使用正则表达式

const id = message.content.match(/<?@?!?(\d{17,19})>?/)[1]

const-id=message.content.match(/这是因为消息中没有提及,所以基本上可以使用if语句(或三元运算符)来解决此问题

const user = message.mentions.users.first();
if (!user) return console.log('no user found');
const userID = user.id;
您还可以使用正则表达式,这样您就可以同时使用用户ID和提到的内容

const user = await message.client.users.fetch(/\D/g, '').catch(() => {});
if (!user) return console.log('no user found oof');
const userID = user.id;

regex
/\D/g
删除所有非数字字符,因为在discord
中这样提及,regex将删除
,并将ID与discord中的所有可用用户匹配,并返回承诺,因此使用wait(如果未找到用户,则返回false)

msg.indications.users.first().id
对我有效。只有在消息中没有提及时,它才会给我该错误。请尝试快速执行
控制台.log(message.content)
。你确定你的机器人看到的消息是正确的吗?请提供有关消息外观的详细信息。
message.indications.users.first().id
当您的邮件中提到某人时,只需工作即可。邮件为:st!warn testing此功能有效,因此我会将其标记为答案。非常感谢!@stationaryStation np