Example #1
0
        public static void CreateStronglyTypedSettings()
        {
            var settingsBuilder = StronglyTypedSettingsFileBuilder <ExampleSettingsDefinition>
                                  .FromFile(Environment.CurrentDirectory, "ExampleSettings.json"); // Overloaded to call Path.Combine() internally

            settingsBuilder.WithDefaultNullValueHandling(NullValueHandling.Include)                // Include any property that is set to null in the output JSON
            .WithFileNotFoundBehavior(SettingsFileNotFoundBehavior.ReturnDefault)                  // Return a default settings file instead of null
            .WithDefaultValueHandling(DefaultValueHandling.Populate);                              // Populate object with default values if the settings are not found in the JSON, or if we call CreateDefault().

            // If you want to create a default and save it, call:
            settingsBuilder.CreateDefault().Save();

            // Otherwise, if you want to load:
            var settings = settingsBuilder.Build();

            // Since we set FileNotFoundBehavior above, and also called CreateDefault(), one of those will have created a default version.
            // Since we DefaultValueHandling was set to populate, we should have a DefaultTrue property with the value set to True.
            bool shouldBeTrue = settings.DefaultTrue;
        }
Example #2
0
        /// <summary>
        /// Main application startup method
        /// </summary>
        /// <param name="args">List of command line arguments</param>
        public async void OnStartup(string[] args)
        {
            _logger.LogInformation("Starting the TRUSU CS Club Discord bot...");
            _logger.LogInformation($"Runtime directory: {Environment.CurrentDirectory}");

            CheckArgs(args);

            Settings = StronglyTypedSettingsFileBuilder <ApplicationSettings> .FromFile(Environment.CurrentDirectory, "settings.json")
                       .WithDefaultNullValueHandling(NullValueHandling.Include)
                       .WithDefaultValueHandling(DefaultValueHandling.Populate)
                       .WithFileNotFoundBehavior(SettingsFileNotFoundBehavior.ReturnDefault)
                       .WithEncoding(System.Text.Encoding.UTF8)
                       .Build();

            if (Settings.Token == "INSERT TOKEN HERE")
            {
                // And then quit. EDIT YOUR SHIT OWNER!
                _logger.LogError("You haven't set your token! You must edit settings.json and add your token before running the bot.");
                Settings.Save();
                Application.Current.Shutdown();
                return;
            }

            RoleManager = new RoleManager();

            var token = Debugger.IsAttached && !string.IsNullOrEmpty(Settings.DebugToken) ? Settings.DebugToken : Settings.Token;

            Discord = new DiscordClient(new DiscordConfiguration()
            {
                AutoReconnect = true,
                Token         = token,
                TokenType     = TokenType.Bot
            });

            DiscordCommands = Discord.UseCommandsNext(new CommandsNextConfiguration()
            {
                CaseSensitive = true, StringPrefixes = new[] { Settings.CommandPrefix }, EnableDms = true, Services = _serviceProvider
            });

            DiscordCommands.RegisterCommands <Commands.AdministrativeCommands>();
            DiscordCommands.RegisterCommands <Commands.AnnouncementCommands>();
            DiscordCommands.RegisterCommands <Commands.BoardCommands>();
            DiscordCommands.RegisterCommands <Commands.BotCommands>();
            DiscordCommands.RegisterCommands <Commands.GameNightSuggestionCommands>();
            DiscordCommands.RegisterCommands <Commands.InteractionCommands>();
            DiscordCommands.RegisterCommands <Commands.RoleCommands>();
            DiscordCommands.RegisterCommands <Commands.TestCommands>();
            DiscordCommands.RegisterCommands <Commands.WednesdayCommands>();

            if (Settings.RequireAccept)
            {
                DiscordCommands.RegisterCommands <Commands.WelcomeCommands>();
            }

            if (Settings.GameStatusMessages == null)
            {
                _logger.LogInformation("No game status messages were found in Settings.");
                Settings.GameStatusMessages = new List <string> {
                    $"Use {Settings.CommandPrefix}help for more info."
                };
                Settings.Save();
            }

            _activityList.Clear(); // just in case, for whatever reason it has items
            foreach (var message in Settings.GameStatusMessages)
            {
                _activityList.Add(new DiscordActivity(message)); // custom isn't supported on bots :(
            }

            _logger.LogInformation(_activityList.Count + " status message(s) loaded.");

            if (Settings.WelcomeMessages.Count <= 0)
            {
                Settings.WelcomeMessages.Add("Welcome {NICK}!");
                Settings.Save();
                _logger.LogInformation("No welcome messages found in settings; set default.");
            }

            SetupDiscordEvents();
            _logger.LogInformation("Connecting to Discord...");
            await Discord.ConnectAsync();

            _logger.LogInformation("Connected to Discord.");

            _logger.LogInformation("Load complete. Bot is now running.");
        }