Adding dBot and AJ

This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-06-02 20:51:35 -04:00
parent 9dfc55d1e9
commit ad7a75b38e
6 changed files with 179 additions and 6 deletions

View File

@ -5,8 +5,12 @@
<e p="C:\Users\sean\.Rider2019.3\system\resharper-host\local\Transient\ReSharperHost\v193\SolutionCaches\_ChaosBot.-595872383.00" t="ExcludeRecursive" />
<e p="C:\Users\sean\OneDrive\Documents\Development\DotNet\ChaosBot" t="IncludeFlat">
<e p="ChaosBot" t="IncludeRecursive">
<e p="appsettings.json" t="Include" />
<e p="bin" t="ExcludeRecursive" />
<e p="ChaosBot.csproj" t="IncludeRecursive" />
<e p="Discord" t="Include">
<e p="DBot.cs" t="Include" />
</e>
<e p="Logging.cs" t="Include" />
<e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include">

View File

@ -7,7 +7,16 @@
<ItemGroup>
<PackageReference Include="Discord.Net" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
<PackageReference Include="NLog" Version="4.7.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.4" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

72
ChaosBot/Discord/DBot.cs Normal file
View File

@ -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<string>("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;
}
}
}

View File

@ -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;
}
}
}
}

View File

@ -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<string>("Bot:Name")} v{Cfg.GetValue<string>("Bot:Version")}");
var DiscordBot = DBot.Connect();
await DiscordBot;
}
catch (Exception ex)
{
_logger.Error(ex, $"Stopped program because of exception");
}
}
}
}

32
ChaosBot/appsettings.json Normal file
View File

@ -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"
}
]
}
}