diff --git a/.idea/.idea.ChaosBot/.idea/contentModel.xml b/.idea/.idea.ChaosBot/.idea/contentModel.xml index ebea5c4..465b175 100644 --- a/.idea/.idea.ChaosBot/.idea/contentModel.xml +++ b/.idea/.idea.ChaosBot/.idea/contentModel.xml @@ -5,8 +5,12 @@ + + + + diff --git a/ChaosBot/ChaosBot.csproj b/ChaosBot/ChaosBot.csproj index ac4c040..c24a155 100644 --- a/ChaosBot/ChaosBot.csproj +++ b/ChaosBot/ChaosBot.csproj @@ -7,7 +7,16 @@ + + + + + + + + PreserveNewest + diff --git a/ChaosBot/Discord/DBot.cs b/ChaosBot/Discord/DBot.cs new file mode 100644 index 0000000..ec07364 --- /dev/null +++ b/ChaosBot/Discord/DBot.cs @@ -0,0 +1,72 @@ +using NLog; +using System; +using Discord; +using Discord.WebSocket; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; + + +namespace ChaosBot.Discord +{ + public class DBot + { + public static DiscordSocketClient _client; + private static Logger _logger = Program._logger; + + public static async Task Connect() + { + try + { + /* + * Perform Connection + */ + _client = new DiscordSocketClient(); + _client.Log += Log; + + await _client.LoginAsync(TokenType.Bot, Program.Cfg.GetValue("Discord:Token")); + await _client.StartAsync(); + + /* + * Let's keep the Task open and happy. + */ + await Task.Delay(-1); + + } + catch (Exception ex) + { + _logger.Error($"dBot.Connect: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + + private static Task Log(LogMessage msg) + { + switch (msg.Severity) + { + case LogSeverity.Critical: + _logger.Fatal(msg.Message); + break; + case LogSeverity.Debug: + _logger.Debug(msg.Message); + break; + case LogSeverity.Error: + _logger.Error(msg.Message); + break; + case LogSeverity.Info: + _logger.Info(msg.Message); + break; + case LogSeverity.Warning: + _logger.Warn(msg.Message); + break; + case LogSeverity.Verbose: + _logger.Trace(msg.Message); + break; + default: + _logger.Trace(msg.Message); + break; + } + + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/ChaosBot/Logging.cs b/ChaosBot/Logging.cs index ba2baed..438049a 100644 --- a/ChaosBot/Logging.cs +++ b/ChaosBot/Logging.cs @@ -1,7 +1,24 @@ -namespace ChaosBot +using System; +using NLog; +using NLog.Extensions.Logging; + +namespace ChaosBot { public class Logging { - + public static Logger GenLog() + { + try + { + LogManager.Configuration = new NLogLoggingConfiguration(Program.Cfg.GetSection("NLog")); + + return LogManager.GetCurrentClassLogger(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } } } \ No newline at end of file diff --git a/ChaosBot/Program.cs b/ChaosBot/Program.cs index d71fe68..873365e 100644 --- a/ChaosBot/Program.cs +++ b/ChaosBot/Program.cs @@ -1,12 +1,51 @@ -using System; +using NLog; +using System; +using Discord; +using Discord.WebSocket; +using System.Threading.Tasks; +using ChaosBot.Discord; +using Microsoft.Extensions.Configuration; + namespace ChaosBot { - class Program + internal class Program { - static void Main(string[] args) + public static IConfiguration Cfg { get; set; } + public static Logger _logger; + + public static void Main(string[] args) + => new Program().MainFunction().GetAwaiter().GetResult(); + + private async Task MainFunction() { - Console.WriteLine("Hello World!"); + try + { + /* + * Load configuration from AppSettings.Json and save as Cfg + */ + Cfg = new ConfigurationBuilder() + .SetBasePath(System.IO.Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build(); + + /* + * Initialize the _logger for logging purposes + */ + _logger = Logging.GenLog(); + + /* + * Initialize the Discord Client and Login + */ + _logger.Info($"Starting Up {Cfg.GetValue("Bot:Name")} v{Cfg.GetValue("Bot:Version")}"); + + var DiscordBot = DBot.Connect(); + + await DiscordBot; + } + catch (Exception ex) + { + _logger.Error(ex, $"Stopped program because of exception"); + } } } } \ No newline at end of file diff --git a/ChaosBot/appsettings.json b/ChaosBot/appsettings.json new file mode 100644 index 0000000..b9f31ee --- /dev/null +++ b/ChaosBot/appsettings.json @@ -0,0 +1,32 @@ +{ + "Bot": { + "Name": "ChaosBot", + "Version": "1.0" + }, + "Discord": { + "Secret": "TPtJeNzPXPM0vptm3igbh8_G3KEfbPdA", + "ClientID": "717523478890414090", + "Token": "NzE3NTIzNDc4ODkwNDE0MDkw.XtbkDw.RpsISqttv27m2Pknq3c5nEVvXWg" + }, + "NLog": { + "internalLogLevel": "Info", + "internalLogFile": "c:\\temp\\internal-nlog.txt", + "extensions": { + "NLog.Web.AspNetCore": { + "assembly": "NLog.Web.AspNetCore" + } + }, + "targets": { + "loggerconsole": { + "type": "console" + } + }, + "rules": [ + { + "logger": "*", + "minLevel": "Trace", + "writeTo": "loggerconsole" + } + ] + } +} \ No newline at end of file