discord.net The server responded with error 50001: Missing Access

Viewed 902

I would like to retrieve users in discord guild but I get an error with cryptic message.

The server responded with error 50001: Missing Access

The bot has Administrator permission, that's what it states in Server settings > Roles > Permissions

Perhaps something else is needed to tick off in configuration.

Your thoughts?

Thanks for help.

Code

  class Program
    {
        private static IDiscordClient _client;
        private static bool _isRunning = false;
        private static Timer _timer;

        static async Task Main(string[] args)
        {
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProcessExit);
            var client = new DiscordSocketClient(new DiscordSocketConfig()
            {
                AlwaysDownloadUsers = true,
                MessageCacheSize = 200,
            });
            var botToken = "...";

            await client.LoginAsync(TokenType.Bot, botToken);
            await client.StartAsync();
            client.Ready += Ready;
            _client = client;
            await Task.Delay(Timeout.InfiniteTimeSpan);
        }

        private static async Task Ready()
        {
            _timer = new Timer(GetUsers, null, TimeSpan.Zero, TimeSpan.FromSeconds(10));
        }

        private static async void GetUsers(object state)
        {
            if (_isRunning)
            {
                return;
            }

            _isRunning = true;

            try
            {
                var guilds = await _client.GetGuildsAsync();
                var guild = guilds.First();

                var users = await guild.GetUsersAsync();
                Console.WriteLine(users.Count);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            _isRunning = false;

        }

        static void ProcessExit(object sender, EventArgs e)
        {
            (_client as DiscordSocketClient).LogoutAsync().GetAwaiter().GetResult();
        }
    }

Discord Developer Portal - Scopes and bot permissions under OAuth2 configuration

enter image description here

Link which I used to invite bot to server.

https://discord.com/oauth2/authorize?client_id=<>&permissions=8&scope=applications.commands%20bot
3 Answers

Glad OP has already solved their issue; I came from a search and solved it by making sure my bot invite link has &scope=bot%20applications.commands at the end, as well as the "Use slash commands" permission (or have your bot be admin).

The Bot's invite link is in the OAuth > URL Generator in the Discord Developer Portal -- https://discord.com/developers/applications/<your_app_id>/oauth2/url-generator

Also, make sure the REQUIRES OAUTH2 CODE GRANT option in the Bot menu is off, otherwise you'll get a "Bot requires a code grant" response from the OAuth invite.

Apparently server member intent must be toggled to see guild members.

developer portal bot

In my case the server role order was wrong. The bot was able to assign any roles that was below the bot's role on the role list, but nothing that was above.

I went to "Server Settings" -> "Roles" -> and moved up the bots role to the top, Clicked save and it was working after that.

It can be tested by clicking on "More" -> "View server as role" and then try to assign the role you want.

Related