javascript 从提及中获取用户 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51455815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:33:38  来源:igfitidea点击:

Getting a user ID from a mention

javascriptdiscorddiscord.js

提问by OffstageAlmond

I want to get a Users ID from a mention. I am making a user info command so the author of the message will not always be them so I cannot do message.author.id. I have used substrings to split the command into the command itself and the mention but I cannot turn the mention into an ID.

我想从提及中获取用户 ID。我正在制作一个用户信息命令,所以消息的作者并不总是他们,所以我不能做message.author.id。我使用子字符串将命令拆分为命令本身和提及,但我无法将提及转换为 ID。

I am aware that discords mentions are actually <@USERID>, which is another way I could get the ID. Please tell me if I am able to get rid of the <@>so I am left with the ID, or turn the mention itself into an one using another method. I have looked through the docs and I am not aware of anything that could help me.

我知道 discords 提到的实际上是<@USERID>,这是我获得 ID 的另一种方式。请告诉我我是否能够摆脱<@>这样我留下的 ID,或者使用另一种方法将提及本身变成一个。我已经浏览了文档,但我不知道有什么可以帮助我的。

回答by Federico Grandi

You can do that in two ways: if you want to use the mention as an argument, you can remove the mention characters with regex, like this: yourstring.replace(/[\\<>@#&!]/g, "");.
The other way to do that is to get the first mention in the message and then the user's id: message.mentions.users.first().id.

你可以这样做有两种方式:如果你想使用提及作为参数,你可以用正则表达式中删除提及的人物,像这样:yourstring.replace(/[\\<>@#&!]/g, "");
另一种方法是获取消息中的第一个提及,然后获取用户的 id: message.mentions.users.first().id