Bot for Discord operational but not connecting to server

I’m having trouble with my Discord bot. I updated my code from Discord.NET API 0.9.6 to 1.0.1. This meant I had to rewrite a lot of stuff. The bot runs without errors but it’s not showing up online in my server.

Here’s what I did:

public class BotProgram
{
    private MessageHandler msgHandler;
    private BotClient botClient;
    private ServiceContainer serviceBox;

    async Task Initialize()
    {
        botClient = new BotClient();
        msgHandler = new MessageHandler();

        string secretKey = "actualBotTokenHere";

        serviceBox = new ServiceContainer()
                .CreateServiceBox();

        await SetupMessageHandling();

        await botClient.SignInAsync(AuthType.Bot, secretKey);
        await botClient.ActivateAsync();

        await Task.Delay(-1);
    }

    async Task SetupMessageHandling()
    {
        botClient.MessageReceived += ProcessCommand;
        await msgHandler.LoadModulesAsync(Assembly.GetExecutingAssembly());
    }

    async Task ProcessCommand(MessageObject msg)
    {
        if (msg == null || !(msg is UserMessage)) return;
        
        int cmdStart = 0;
        if (!(msg.StartsWithSymbol('!', ref cmdStart) || msg.MentionsBot(botClient.CurrentUser, ref cmdStart))) return;
        
        var cmdContext = new CommandContext(botClient, msg);
        var cmdResult = await msgHandler.RunCommandAsync(cmdContext, cmdStart, serviceBox);
        
        if (!cmdResult.Succeeded)
            await cmdContext.Channel.SendTextAsync(cmdResult.FailureReason);
    }
}

I also have a BotCommands class but it’s pretty empty right now. Any ideas why the bot isn’t connecting?

hey there, i had a similar issue. check if ur bot has the right permissions in discord. also, make sure ur using the correct token and haven’t accidentally used a client secret or somthing. sometimes restarting the bot or even recreating the token can help. good luck!

Have you verified that your bot is actually attempting to connect to the server? It might be worth adding some logging statements to your Initialize method to confirm the connection process is starting. Also, ensure you’re using the correct bot token and that it hasn’t been revoked or regenerated in the Discord Developer Portal.

Another thing to check is whether you’ve properly implemented the ReadyAsync event. This event fires when your bot successfully connects to Discord. Without it, your bot might appear offline even if it’s running. Try adding something like:

botClient.Ready += () =>
{
Console.WriteLine($“{botClient.CurrentUser} is connected!”);
return Task.CompletedTask;
};

This can help you confirm if the bot is actually connecting or if there’s an issue earlier in the process. If none of these solve it, consider reviewing your BotClient class implementation for any potential issues.

I’ve been through this exact situation when upgrading Discord.NET. One thing that caught me off guard was the need to explicitly add intents. Without them, your bot might run but won’t connect properly.

Try adding this before you initialize your client:

var config = new DiscordSocketConfig
{
    GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
};

botClient = new DiscordSocketClient(config);

This ensures your bot has the necessary permissions to function. Also, double-check that you’ve added the bot to your server using the OAuth2 URL from the Discord Developer Portal. Sometimes we forget this step after making changes.

If it’s still not working, enable debug logging. It’s been a lifesaver for me in tracking down connectivity issues.