Discord.NET Bot Fails to Retrieve Guild Roles and Channels on Test Server Compared to Local

124 Views Asked by At

I am developing a Discord bot using Discord.NET.WebSocket Locally, it functions perfectly, retrieving all necessary guild information such as roles and text channels. However, when deployed to a test server, it fails to retrieve the guild roles and specific text channel information.

Here is the core part of the bot service where the issue occurs:

DiscordSocketClient _client = new DiscordSocketClient(new DiscordSocketConfig
{
    GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMembers
});

public async Task<IInviteMetadata> GenerateDiscordInvite()
{
    try
    {
        var guild = _client.GetGuild(Convert.ToUInt64(_discordServerSettings.ClientId));

        throw new DiscordBotException(
            $"guild id: {guild.Id}," +
            $"guild name: {guild.Name}," +
            $"guild roles: {string.Join(", ", guild.Roles.Select(role => role.Name))}," +
            $"guild channels: {guild.GetTextChannel(Convert.ToUInt64(_discordServerSettings.ChannelId))?.Name}," +
            $"client login state: {_client?.LoginState}," +
            $"client user name: {_client?.CurrentUser?.Username}," +
            $"guild is admin: {guild.CurrentUser?.GuildPermissions.Administrator}," +
            $"guild user name: {guild.CurrentUser?.Username}");
    }
    catch (Exception e)
    {
        throw a new DiscordBotException("Failed to generate invite: " + e.Message, e);
    }
}

Additionally, here is the StartAsync method that initializes the DiscordSocketClient:

public async Task StartAsync()
{
    await _client.LoginAsync(TokenType.Bot, _discordBotSettings.Token);
    await _client.StartAsync();
}

And the hosted service that invokes StartAsync:

public class BotHostedService : IHostedService
{
    private readonly IDiscordBotService _botService;

    public BotHostedService(IDiscordBotService botService)
    {
        _botService = botService;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await _botService.StartAsync();
    }
}

Example Responses:

  • Local Environment Response:
guild id: 123456789123456789,
guild name: TestGuild,
guild roles: Role1, Role2, Role3,
guild channels: General,
client login state: LoggedIn,
client user name: TestBot,
guild is admin: True,
guild user name: TestBot
  • Test Server Response:
guild id: 123456789123456789,
guild name: TestGuild,
guild roles: ,
guild channels: ,
client login state: LoggedIn,
client user name: TestBot,
guild is admin: False,
guild user name:

Problem:

The bot, when deployed on a test server, does not retrieve GuildRoles and GuildChannels, which is critical for its functionality. This issue does not occur in the local development environment. Both environments use the same Discord bot account with identical permissions.

Question:

What could cause the Discord bot to fail in retrieving GuildRoles, GuildChannels and guild current user on the test server, despite working as expected locally? Are there specific environmental or configuration aspects I might be overlooking?

I logged discord bot and server configs: clientId, token, clientSecret - they are identical on local and testing. Also, Discord server and bot settings in Discord are identical (since I use the same discord server and bot on both envs).

Any guidance or suggestions to resolve this issue would be greatly appreciated.

1

There are 1 best solutions below

0
Tito On

While I was unable to recreate your problem to verify, it has some similarity with this question client.GetGuild() returns null. Discord.NET - I believe my response was helpful for the asker, and could be worth a try for you (I'm assuming it's cache related and how client.GetGuild() works)