izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

How to get userid from username #317

Open Unity123 opened 5 years ago

Unity123 commented 5 years ago

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

ClearlyElevated commented 4 years ago

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 commented 4 years ago

this isnt a lib issue

soryy708 commented 4 years ago
    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;
    }