/// <summary> /// Loads the settings XML file. /// </summary> /// <param name="filePath">File path of the settings XML file.</param> /// <returns>Returns loaded <see cref="HazeronDiscordBotSettings"/> object.</returns> public static HazeronDiscordBotSettings Load(string filePath) { XmlSerializer serializer = new XmlSerializer(typeof(HazeronDiscordBotSettings)); TextReader reader = new StreamReader(filePath); HazeronDiscordBotSettings settings = (HazeronDiscordBotSettings)serializer.Deserialize(reader); reader.Close(); return(settings); }
/// <summary> /// Checks if a settings XML file with the name exist and loads it, if one does not exist it is created. /// </summary> /// <param name="filePath">File path of the settings XML file.</param> /// <returns>Returns loaded or created <see cref="HazeronDiscordBotSettings"/> object.</returns> public static HazeronDiscordBotSettings LoadOrCreate(string filePath) { if (File.Exists(filePath)) { return(Load(filePath)); } else { HazeronDiscordBotSettings settings; settings = new HazeronDiscordBotSettings(); settings.Save(filePath); return(settings); } }
/// <summary> /// Bot running method. /// </summary> public async Task RunBotAsync() { _settingsXml = HazeronDiscordBotSettings.LoadOrCreate(SETTINGS); string botToken = _settingsXml.BotToken; // IF 'BotToken' is null or empty, give error and exit. if (string.IsNullOrEmpty(botToken)) { Console.WriteLine($"The 'BotToken' setting is null or empty."); Console.WriteLine($"Set the value in the '{SETTINGS}' file."); Console.WriteLine(); Console.WriteLine("Press any key to exit."); Console.ReadKey(); return; } _client = new DiscordSocketClient(); _commands = new CommandService(); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) .BuildServiceProvider(); // Event subscribtions. _client.Log += Log; await RegisterCommandsAsync(); // Login with bot token. await _client.LoginAsync(Discord.TokenType.Bot, botToken); await _client.StartAsync(); // Wait forever. await Task.Delay(-1); }