#DiscordSocketClient.Guilds is always empty, using dependency injection

1 messages · Page 1 of 1 (latest)

dapper saddle
#

Hi All,

Hoping someone can help me out here
Im using dependancy injection within my application

In my DiscordModule.cs I have

{
    GatewayIntents = global::Discord.GatewayIntents.AllUnprivileged,
    AlwaysDownloadUsers = true,
}));
services.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()));
services.AddSingleton<CommandHandler>();```

In my `ReadyAsync()` I have
```Console.WriteLine($"Connected as -> [{_client.CurrentUser}] :)");
Console.WriteLine("Discord: " + _client.Status.ToString());
Console.WriteLine("Guild Count: " + _client.Guilds.Count);```

And in an API Endpoint controller, I have
```public TestController(DiscordSocketClient discord, IServiceProvider provider)
{
    Console.WriteLine("In TestController Constructor");
     _discord = discord;
    _provider = provider;
}``` ```public string Get([FromRoute] TestModel model)
{
    Console.WriteLine("Discord: " + _discord.Status.ToString());
    Console.WriteLine("Guild Count: " + _discord.Guilds.Count);

    var guilds = _discord.Guilds;
    foreach (var guild in guilds)
    {
        Console.WriteLine("Guild: " + guild.Id);
    }
    return "TEST";
}```

When I start the application, I see that `ReadyAsync` gets called as I get
```In debug mode, adding commands to 6057XXXXXXXXXXXX0...
Connected as -> [?TestUser?#1234] :)
Discord: Online
Guild Count: 1
Guild: 6057XXXXXXXXXXXX0
16:16:53 Gateway     Ready```

But if I call the API endpoint I get
```Discord: Online
Guild Count: 0``` and `TEST` gets returned from the controller

From the API Controller, if I try `var test = _discord.GetGuild(6057XXXXXXXXXXXX0);` test is null

I really cant see what Im missing here and why the guild list is empty when I try from the API Controller


Thanks in advanced
lunar berry
#

@dapper saddle did you manage to solve this? Just built pretty much the same setup with minimal APIs.
While Guilds are avalable in OnReady, later via API its empty. Exactly like you described it.

dapper saddle
lunar berry
#

Well 😉

builder.Services.AddSingleton<BotBackgroundService>();
builder.Services.AddHostedService<BotBackgroundService>();
#

Sadly thats what i already do. But what i managed to figure out is that is has somethign to if i host it as HostedService or not

#

so my guess is that it has to do somethign with the thread its running on 🤷‍♂️

dapper saddle
#

My code is on GitHub, so you're welcome to take a look at it and see if it gives you any pointers.
What I have works 100%, so what ever I did, it now works

lunar berry
#

Would love to do that if can share the link

#

uff, i just figured it out^^

#
builder.Services.AddHostedService<BotBackgroundService>((serviceProvider) => serviceProvider.GetRequiredService<BotBackgroundService>());
#

sometime its the most simple stuff...

dapper saddle
#

Ahhh... just realised... While I originally started on Discord.Net, the project ended up switching to a different library (that was driving by the other guy working on the code at the same time)
But I did have it working on Discord.Net before the switch

Yeah... you need to get the service from the service provider when adding it as a HostedService