Ejemplo n.º 1
0
        public DiscordBot CreateBot(string token)
        {
            var prefixProvider = new DefaultPrefixProvider()
                                 .AddPrefix('*')
                                 .AddMentionPrefix();

            var bot = new DiscordBot(TokenType.Bot, token, prefixProvider);

            bot.AddModule <PingModule>();

            return(bot);
        }
Ejemplo n.º 2
0
        public async Task <DiscordBotBase> ConfigureAsync()
        {
            var config = Config.ParseArguments(args);

            logger = new Logger(Logger.LogLevel.Info, Path.Combine(Config.FilesPath, "logs"));
            DbConnection.Initialize(config);

            // Ensure the database is migrated to the latest version prior to any other code execution.
            using (var db = new DataContext())
            {
                await db.Database.MigrateAsync();

                await db.SaveChangesAsync();
            }

            IServiceCollection botServiceCollection = new ServiceCollection()
                                                      .AddSingleton <HttpClient>()
                                                      .AddSingleton(new CachingMechanism.EntityCache(60));

            // Gets services marked with the Service attribute, adds them to the service collection
            var services = Assembly.GetExecutingAssembly().GetServices();

            foreach (var type in services)
            {
                botServiceCollection = botServiceCollection.AddSingleton(type);
            }

            var interactive = new InteractivityExtension();
            var token       = config.GetOrAddEntry(Config.Defaults.Token.ToString(), () =>
            {
                logger.Log(
                    $"Please input bot token, can be found at: " +
                    $"{Constants.DeveloperApplicationLink}",
                    Logger.Source.Bot);
                return(Console.ReadLine());
            });
            var prefix = config.GetOrAddEntry(Config.Defaults.Prefix.ToString(), () =>
            {
                logger.Log("Please input bot prefix",
                           Logger.Source.Bot);
                return(Console.ReadLine());
            });
            var shardCount = int.Parse(config.GetOrAddEntry(Config.Defaults.ShardCount.ToString(), () =>
            {
                logger.Log(
                    $"Please input desired shard count (discord allows a maximum of 2500 guilds per shard): ",
                    Logger.Source.Bot);

                string value;

                // Iterate until valid integer is provided.
                do
                {
                    value = Console.ReadLine();
                }while (!int.TryParse(value, out var x) || x <= 0);

                return(value);
            }));

            bool multiProcessSharding = false;

            if (shardCount > 1)
            {
                multiProcessSharding = bool.Parse(config.GetOrAddEntry("MultiProcess", () =>
                {
                    logger.Log(
                        $"Using multi-process sharding? (true/false): ",
                        Logger.Source.Bot);

                    string value;

                    do
                    {
                        value = Console.ReadLine();
                    }while (!bool.TryParse(value, out var x));

                    return(value);
                }));
            }

            var            prefixProvider = new DefaultPrefixProvider().AddPrefix(prefix);
            DiscordBotBase bot;

            if (!multiProcessSharding)
            {
                bot = new DiscordBotSharder(
                    TokenType.Bot,
                    token,
                    prefixProvider,
                    new DiscordBotSharderConfiguration
                {
                    ProviderFactory = bot => botServiceCollection
                                      .AddSingleton(bot)
                                      .AddSingleton(config)
                                      .AddSingleton(logger)
                                      .AddSingleton(interactive)
                                      .BuildServiceProvider(),
                    ShardCount = shardCount
                });
            }
            else
            {
                bot = new DiscordBot(TokenType.Bot, token, prefixProvider, new DiscordBotConfiguration()
                {
                    ProviderFactory = bot => botServiceCollection
                                      .AddSingleton(bot)
                                      .AddSingleton(config)
                                      .AddSingleton(logger)
                                      .AddSingleton(interactive)
                                      .BuildServiceProvider(),
                    ShardCount = shardCount,
                    ShardId    = int.Parse(config.GetOrAddEntry("ShardId", () =>
                    {
                        logger.Log(
                            $"Please input shard ID:",
                            Logger.Source.Bot);

                        string value;

                        // Iterate until valid integer is provided.
                        do
                        {
                            value = Console.ReadLine();
                        }while (!int.TryParse(value, out var x) || x < 0);

                        return(value);
                    }))
                });