private DiscordConfiguration SetupDiscordConfig(ConfigJson config) { Console.WriteLine("Configuring client"); // Setup loglevel based on config file string LogLevel ll = new LogLevel(); switch (config.LogLevel) { case "debug": ll = LogLevel.Debug; break; case "info": ll = LogLevel.Info; break; case "critical": ll = LogLevel.Critical; break; case "error": ll = LogLevel.Error; break; case "warning": ll = LogLevel.Warning; break; default: ll = LogLevel.Info; break; } // Setup discord config object DiscordConfiguration cfg = new DiscordConfiguration { Token = config.Token, TokenType = TokenType.Bot, AutoReconnect = true, LogLevel = ll, UseInternalLogHandler = true }; return(cfg); }
private void SetupCommands(ConfigJson config) { // Setup command configuration Client.DebugLogger.LogMessage(LogLevel.Debug, "SetupCommands", $"Configuring commands", DateTime.Now); CommandsNextConfiguration ccfg = new CommandsNextConfiguration { StringPrefix = config.CommandPrefix, CaseSensitive = false, EnableDms = true, EnableMentionPrefix = true }; this.Commands = this.Client.UseCommandsNext(ccfg); // Hook up some command events Client.DebugLogger.LogMessage(LogLevel.Debug, "SetupCommands", $"Hooking up events", DateTime.Now); this.Commands.CommandExecuted += StaticEvents.Command_Executed; this.Commands.CommandErrored += StaticEvents.Command_Errored; // Next, load/register our commands Client.DebugLogger.LogMessage(LogLevel.Debug, "SetupCommands", $"Registering commands", DateTime.Now); this.Commands.RegisterCommands <Fun>(); this.Commands.RegisterCommands <Utility>(); this.Commands.RegisterCommands <Searches>(); this.Commands.RegisterCommands <Games>(); this.Commands.RegisterCommands <Emotes>(); // Setup our help command formatter Client.DebugLogger.LogMessage(LogLevel.Debug, "SetupCommands", $"Setting up HelpFormatter", DateTime.Now); this.Commands.SetHelpFormatter <HelpFormatter>(); }
private ConfigJson ReadConfigFile(string path) { Console.WriteLine($"Reading {path}"); string data = ""; try { using (FileStream fs = File.OpenRead(path)) using (StreamReader sr = new StreamReader(fs, new UTF8Encoding(false))) { data = sr.ReadToEnd(); } } catch (Exception e) { Console.WriteLine($"Error reading config file: {e.GetType()}: {e.Message}"); Console.Read(); Environment.Exit(1); } ConfigJson cfgjson = JsonConvert.DeserializeObject <ConfigJson>(data); return(cfgjson); }
// Main bot method public async Task RunBotAsync() { Console.WriteLine($"PotatoBot Version {VERSION}"); // Load config file & configure client ConfigJson cfgjson = ReadConfigFile(configPath); DiscordConfiguration cfg = SetupDiscordConfig(cfgjson); Console.WriteLine("Initiating discord client . . . "); this.Client = new DiscordClient(cfg); // Setup client events and commands SetupInteractivity(); SetupClientEvents(); SetupCommands(cfgjson); // Finally lets connect to discord Client.DebugLogger.LogMessage(LogLevel.Debug, "PotatoBot", "Connecting to discord server", DateTime.Now); await this.Client.ConnectAsync(); // Prevent immature async quiting await Task.Delay(-1); }