Visitar URL original
Guild count always returns 0? · Issue #1396 · DSharpPlus/DSharpPlus · GitHub
Skip to content

Guild count always returns 0? #1396

Closed
Closed

Description

@DatReki

Summary

Currently working on porting an old Discord.Net bot over to DSharpPlus and implementing slash commands when I noticed something strange. No matter what I do it seems to always say that the bot's server count is 0.
Now the "beta" version of the bot I'm using for the porting process is in at least 3 different servers.

Details

Here is an example of the code I'm running:

namespace Bot
{
    public class Bot
    {
        // Get all classes that use 'BaseCommandModule' so I can register them easily
        public static readonly List<BaseCommandModule> CommandModules = Reflection.InitializeClasses<BaseCommandModule>();
        // Get all classes that use 'ApplicationCommandModule' so I can register them easily
        public static readonly List<ApplicationCommandModule> SlashCommandModules = Reflection.InitializeClasses<ApplicationCommandModule>();
        // Get bot's credentials from a json file
        private static readonly Core.Config.Models.Credentials.CredentialsModel s_creds = Core.Config.Credentials.Creds;

        public static async Task Run()
        {
            Core.Config.Credentials.Set();

            DiscordShardedClient discord = new(new DiscordConfiguration()
            {
                Token = s_creds.Token,
                TokenType = TokenType.Bot,
                Intents = DiscordIntents.AllUnprivileged,
                AutoReconnect = true,
            });

            // Event handlers
            discord.MessageCreated += DiscordEventHandler.MessageCreated;
            discord.GuildCreated += DiscordEventHandler.GuildCreated;
            discord.GuildDeleted += DiscordEventHandler.GuildDeleted;
            discord.GuildMemberAdded += DiscordEventHandler.GuildMemberAdded;
            discord.GuildMemberRemoved += DiscordEventHandler.GuildMemberRemoved;

            // All the CommandsNext stuff is just for testing 
            IReadOnlyDictionary<int, CommandsNextExtension> commands = await discord.UseCommandsNextAsync(new CommandsNextConfiguration()
            {
                EnableDefaultHelp = false,
                EnableMentionPrefix = true,
                StringPrefixes = new[] { s_creds.Prefix }
            });

            // Register BaseCommandModule commands
            foreach (BaseCommandModule module in CommandModules)
            {
                commands.RegisterCommands(module.GetType());
            }

            // Setup command error handling for CommandsNext
            foreach (KeyValuePair<int, CommandsNextExtension> command in commands)
            {
                command.Value.CommandErrored += DiscordEventHandler.CommandErrored;
            }

            // Configure slash commands
            Task<IReadOnlyDictionary<int, SlashCommandsExtension>> slashCommands = discord.UseSlashCommandsAsync(new SlashCommandsConfiguration()
            {
                
            });

            IReadOnlyDictionary<int, SlashCommandsExtension> slashCommand = await slashCommands;

            // Register ApplicationCommandModule commands
            foreach (ApplicationCommandModule module in SlashCommandModules)
            {
                slashCommand.RegisterCommands(module.GetType());
            }

            // Setup command error handling for SlashCommands
            foreach (SlashCommandsExtension extension in slashCommand.Values)
            {
                extension.SlashCommandErrored += SlashEventHandler.CommandErrored;
            }

            discord.Ready += DiscordEventHandler.Ready;

            await discord.StartAsync();

            #region Example
            int guildsCount = 0;
            foreach (KeyValuePair<int, DiscordClient> shardClientPair in discord.ShardClients)
            {
                DiscordClient client = shardClientPair.Value;
                int count = client.Guilds.Count;
                Console.WriteLine($"Guild count for shard '{shardClientPair.Key}': {count}");
                guildsCount += count;
            }
            Console.WriteLine($"Total guilds: {guildsCount}");

            guildsCount = discord.ShardClients.Values.Select(e => e.Guilds.Count).ToList().Sum();
            Console.WriteLine($"Total guilds: {guildsCount}");
            #endregion

            //Core.Services.BotLists.PostStats(discord); Proper call

            await Task.Delay(-1);
        }
    }
}

Currently using DSharpPlus version 4.3.0-nightly-01173 but used 4.2.0 before I noticed/had this issue occurred. Bot targets .net 5.0.
My operating system is Windows 10 and using Visual Studio 2022.

Activity

  1. locked and limited conversation to collaborators on Sep 30, 2022
  2. converted this issue into a discussion #1397 on Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions