最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting a user ID from a mention - Stack Overflow

programmeradmin6浏览0评论

I want to get a Users ID from a mention. I am making a user info mand 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 mand into the mand itself and the mention but I cannot turn the mention into an 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.

I want to get a Users ID from a mention. I am making a user info mand 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 mand into the mand itself and the mention but I cannot turn the mention into an 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.

Share Improve this question edited Jul 21, 2018 at 17:12 Federico Grandi 6,8065 gold badges33 silver badges51 bronze badges asked Jul 21, 2018 at 11:56 OffstageAlmondOffstageAlmond 3234 gold badges6 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

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.

I know the poster already got their answer, but those looking to get all of the ids, there is a different approach:

// Listen for messages
client.on( Events.MessageCreate, async ( message ) => {

    // Get any mentions
    const mentions = message.mentions;
    if ( !mentions ) {
        return;
    }

    // Get the mentioned users
    const usersList = Array.from( mentions.users );

    // Iter the users
    usersList.forEach( function ( user ) {

        // Check if they mentioned the bot
        const userID = user[0];

        // ... Do what you want with each id
        
    } );
    return;
});
发布评论

评论列表(0)

  1. 暂无评论