discord.io icon indicating copy to clipboard operation
discord.io copied to clipboard

How to get userid from username

Open Unity123 opened this issue 5 years ago • 3 comments

I need to get the userid from the username so that my bot can mention a person given by the user.

Unity123 avatar Jun 29 '19 20:06 Unity123

you can use any other bot or put a code block around the username to cause it not to work and give the user ID

ClearlyElevated avatar Sep 14 '19 23:09 ClearlyElevated

this isnt a lib issue

ClearlyElevated avatar Sep 14 '19 23:09 ClearlyElevated

    function getUserId(bot, serverId, username) {
        const server = bot.servers[serverId];
        const userMatch = Object.values(bot.users).find(user => user.username.toLowerCase() === username.toLowerCase());
        if (userMatch) {
            return userMatch.id;
        }
        const nickMatch = Object.values(server.members).find(member => member.nick && member.nick.toLowerCase() === username.toLowerCase());
        if (nickMatch) {
            return nickMatch.id;
        }
        return null;
    }

soryy708 avatar Dec 28 '19 19:12 soryy708