commit
0078de8a7f
9 changed files with 150 additions and 64 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -7,3 +7,5 @@ Backup/
|
|||
UpgradeLog.htm
|
||||
.idea
|
||||
.vscode
|
||||
Geekbot.net/Logs/*
|
||||
!/Geekbot.net/Logs/.keep
|
||||
|
|
|
@ -13,9 +13,7 @@
|
|||
<PackageReference Include="Discord.Net">
|
||||
<Version>1.0.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Google.Apis.YouTube.v3">
|
||||
<Version>1.29.1.976</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.29.1.991" />
|
||||
<PackageReference Include="HtmlAgilityPack.NetCore">
|
||||
<Version>1.5.0.1</Version>
|
||||
</PackageReference>
|
||||
|
@ -23,15 +21,14 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="RestSharp.NetCore">
|
||||
<Version>105.2.3</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog" Version="2.6.0-dev-00894" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
|
||||
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" />
|
||||
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
|
||||
<PackageReference Include="StackExchange.Redis">
|
||||
<Version>1.2.6</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.Http">
|
||||
<Version>4.3.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
||||
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
|
||||
<PackageReference Include="System.Runtime.Serialization.Json">
|
||||
<Version>4.3.0</Version>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Serilog;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
|
@ -9,7 +10,7 @@ namespace Geekbot.net.Lib
|
|||
private readonly Random rnd;
|
||||
private readonly int totalCheckEmImages;
|
||||
|
||||
public CheckEmImageProvider(Random rnd)
|
||||
public CheckEmImageProvider(Random rnd, ILogger logger)
|
||||
{
|
||||
var path = Path.GetFullPath("./Storage/checkEmPics");
|
||||
if (File.Exists(path))
|
||||
|
@ -18,12 +19,12 @@ namespace Geekbot.net.Lib
|
|||
checkEmImageArray = rawCheckEmPics.Split("\n");
|
||||
totalCheckEmImages = checkEmImageArray.Length;
|
||||
this.rnd = rnd;
|
||||
Console.WriteLine($"-- Loaded {totalCheckEmImages} CheckEm Images");
|
||||
logger.Information($"[Geekbot] [CheckEm] Loaded {totalCheckEmImages} CheckEm Images");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("checkEmPics File not found");
|
||||
Console.WriteLine($"Path should be {path}");
|
||||
logger.Error("checkEmPics File not found");
|
||||
logger.Error($"Path should be {path}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
32
Geekbot.net/Lib/ErrorHandler.cs
Normal file
32
Geekbot.net/Lib/ErrorHandler.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using Discord.Commands;
|
||||
using Serilog;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
public class ErrorHandler : IErrorHandler
|
||||
{
|
||||
private readonly ILogger logger;
|
||||
// private readonly IDMChannel botOwnerDmChannel;
|
||||
|
||||
public ErrorHandler(ILogger logger /*, IDMChannel botOwnerDmChannel*/)
|
||||
{
|
||||
this.logger = logger;
|
||||
// this.botOwnerDmChannel = botOwnerDmChannel;
|
||||
}
|
||||
|
||||
public async void HandleCommandException(Exception e, ICommandContext Context)
|
||||
{
|
||||
var errorMsg =
|
||||
$"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\"";
|
||||
logger.Error(e, errorMsg);
|
||||
// await botOwnerDmChannel.SendMessageAsync($"{errorMsg}```{e.StackTrace}```");
|
||||
// await Context.Channel.SendMessageAsync("Something went wrong...");
|
||||
}
|
||||
}
|
||||
|
||||
public interface IErrorHandler
|
||||
{
|
||||
void HandleCommandException(Exception e, ICommandContext Context);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Serilog;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
|
@ -9,7 +10,7 @@ namespace Geekbot.net.Lib
|
|||
private readonly Random rnd;
|
||||
private readonly int totalFortunes;
|
||||
|
||||
public FortunesProvider(Random rnd)
|
||||
public FortunesProvider(Random rnd, ILogger logger)
|
||||
{
|
||||
var path = Path.GetFullPath("./Storage/fortunes");
|
||||
if (File.Exists(path))
|
||||
|
@ -18,12 +19,12 @@ namespace Geekbot.net.Lib
|
|||
fortuneArray = rawFortunes.Split("%");
|
||||
totalFortunes = fortuneArray.Length;
|
||||
this.rnd = rnd;
|
||||
Console.WriteLine($"-- Loaded {totalFortunes} Fortunes");
|
||||
logger.Information($"[Geekbot] [Fortunes] Loaded {totalFortunes} Fortunes");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Fortunes File not found");
|
||||
Console.WriteLine($"Path should be {path}");
|
||||
logger.Error("Fortunes File not found");
|
||||
logger.Error($"Path should be {path}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Serilog;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
|
@ -9,7 +10,7 @@ namespace Geekbot.net.Lib
|
|||
private readonly Random rnd;
|
||||
private readonly int totalPandas;
|
||||
|
||||
public PandaProvider(Random rnd)
|
||||
public PandaProvider(Random rnd, ILogger logger)
|
||||
{
|
||||
var path = Path.GetFullPath("./Storage/pandas");
|
||||
if (File.Exists(path))
|
||||
|
@ -18,12 +19,12 @@ namespace Geekbot.net.Lib
|
|||
PandaArray = rawFortunes.Split("\n");
|
||||
totalPandas = PandaArray.Length;
|
||||
this.rnd = rnd;
|
||||
Console.WriteLine($"-- Loaded {totalPandas} Panda Images");
|
||||
logger.Information($"[Geekbot] [Pandas] Loaded {totalPandas} Panda Images");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Pandas File not found");
|
||||
Console.WriteLine($"Path should be {path}");
|
||||
logger.Error("Pandas File not found");
|
||||
logger.Error($"Path should be {path}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,7 +33,7 @@ namespace Geekbot.net.Lib
|
|||
return PandaArray[rnd.Next(0, totalPandas)];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IPandaProvider
|
||||
{
|
||||
string GetRandomPanda();
|
||||
|
|
0
Geekbot.net/Logs/.keep
Executable file
0
Geekbot.net/Logs/.keep
Executable file
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Serilog;
|
||||
|
||||
namespace Geekbot.net.Modules
|
||||
{
|
||||
|
@ -11,11 +12,15 @@ namespace Geekbot.net.Modules
|
|||
{
|
||||
private readonly ICheckEmImageProvider checkEmImages;
|
||||
private readonly Random rnd;
|
||||
private readonly ILogger logger;
|
||||
private readonly IErrorHandler errorHandler;
|
||||
|
||||
public CheckEm(Random RandomClient, ICheckEmImageProvider checkEmImages)
|
||||
public CheckEm(Random RandomClient, ICheckEmImageProvider checkEmImages, ILogger logger, IErrorHandler errorHandler)
|
||||
{
|
||||
rnd = RandomClient;
|
||||
this.rnd = RandomClient;
|
||||
this.checkEmImages = checkEmImages;
|
||||
this.logger = logger;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
[Command("checkem", RunMode = RunMode.Async)]
|
||||
|
@ -50,7 +55,7 @@ namespace Geekbot.net.Modules
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
using System;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net
|
||||
|
@ -19,36 +20,50 @@ namespace Geekbot.net
|
|||
private IServiceCollection services;
|
||||
private IServiceProvider servicesProvider;
|
||||
private RedisValue token;
|
||||
private ILogger logger;
|
||||
private ulong botOwnerId;
|
||||
|
||||
private static void Main()
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(@" ____ _____ _____ _ ______ ___ _____");
|
||||
Console.WriteLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|");
|
||||
Console.WriteLine(@"| | _| _| | _| | ' /| _ \| | | || |");
|
||||
Console.WriteLine(@"| |_| | |___| |___| . \| |_) | |_| || |");
|
||||
Console.WriteLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
||||
Console.WriteLine("=========================================");
|
||||
Console.WriteLine("* Starting...");
|
||||
var logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.LiterateConsole()
|
||||
.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true)
|
||||
.CreateLogger();
|
||||
|
||||
new Program().MainAsync().GetAwaiter().GetResult();
|
||||
var logo = new StringBuilder();
|
||||
logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____");
|
||||
logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|");
|
||||
logo.AppendLine(@"| | _| _| | _| | ' /| _ \| | | || |");
|
||||
logo.AppendLine(@"| |_| | |___| |___| . \| |_) | |_| || |");
|
||||
logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
||||
logo.AppendLine("=========================================");
|
||||
Console.WriteLine(logo.ToString());
|
||||
logger.Information("[Geekbot] Starting...");
|
||||
new Program().MainAsync(logger).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public async Task MainAsync()
|
||||
private async Task MainAsync(ILogger logger)
|
||||
{
|
||||
Console.WriteLine("* Initing Stuff");
|
||||
|
||||
client = new DiscordSocketClient();
|
||||
this.logger = logger;
|
||||
logger.Information("[Geekbot] Initing Stuff");
|
||||
|
||||
client = new DiscordSocketClient(new DiscordSocketConfig
|
||||
{
|
||||
LogLevel = LogSeverity.Verbose
|
||||
});
|
||||
client.Log += DiscordLogger;
|
||||
commands = new CommandService();
|
||||
|
||||
try
|
||||
{
|
||||
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
||||
redis = redisMultiplexer.GetDatabase(6);
|
||||
Console.WriteLine("-- Connected to Redis (db6)");
|
||||
logger.Information($"[Redis] Connected to db {redis.Database}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Console.WriteLine("Start Redis pls...");
|
||||
logger.Information("Start Redis pls...");
|
||||
Environment.Exit(102);
|
||||
}
|
||||
|
||||
|
@ -59,31 +74,39 @@ namespace Geekbot.net
|
|||
var newToken = Console.ReadLine();
|
||||
redis.StringSet("discordToken", newToken);
|
||||
token = newToken;
|
||||
|
||||
Console.Write("Bot Owner User ID: ");
|
||||
var ownerId = Console.ReadLine();
|
||||
redis.StringSet("botOwner", ownerId);
|
||||
}
|
||||
|
||||
var botOwner = redis.StringGet("botOwner");
|
||||
if (botOwner.IsNullOrEmpty)
|
||||
{
|
||||
Console.Write("Bot Owner User ID: ");
|
||||
botOwner = Console.ReadLine();
|
||||
redis.StringSet("botOwner", botOwner);
|
||||
}
|
||||
botOwnerId = (ulong) botOwner;
|
||||
|
||||
services = new ServiceCollection();
|
||||
var RandomClient = new Random();
|
||||
var fortunes = new FortunesProvider(RandomClient);
|
||||
var checkEmImages = new CheckEmImageProvider(RandomClient);
|
||||
var pandaImages = new PandaProvider(RandomClient);
|
||||
var fortunes = new FortunesProvider(RandomClient, logger);
|
||||
var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
|
||||
var pandaImages = new PandaProvider(RandomClient, logger);
|
||||
IErrorHandler errorHandler = new ErrorHandler(logger);
|
||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||
services.AddSingleton(redis);
|
||||
services.AddSingleton(RandomClient);
|
||||
services.AddSingleton<ILogger>(logger);
|
||||
services.AddSingleton<IFortunesProvider>(fortunes);
|
||||
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
||||
services.AddSingleton<IPandaProvider>(pandaImages);
|
||||
|
||||
Console.WriteLine("* Connecting to Discord");
|
||||
logger.Information("[Geekbot] Connecting to Discord");
|
||||
|
||||
await Login();
|
||||
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
|
||||
public async Task Login()
|
||||
private async Task Login()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -93,35 +116,34 @@ namespace Geekbot.net
|
|||
if (isConneted)
|
||||
{
|
||||
await client.SetGameAsync("Ping Pong");
|
||||
Console.WriteLine($"* Now Connected to {client.Guilds.Count} Servers");
|
||||
logger.Information($"[Geekbot] Now Connected to {client.Guilds.Count} Servers");
|
||||
|
||||
Console.WriteLine("* Registering Stuff");
|
||||
logger.Information("[Geekbot] Registering Stuff");
|
||||
|
||||
client.MessageReceived += HandleCommand;
|
||||
client.MessageReceived += HandleMessageReceived;
|
||||
client.UserJoined += HandleUserJoined;
|
||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||
services.AddSingleton(commands);
|
||||
servicesProvider = services.BuildServiceProvider();
|
||||
|
||||
Console.WriteLine("* Done and ready for use\n");
|
||||
this.servicesProvider = services.BuildServiceProvider();
|
||||
logger.Information("[Geekbot] Done and ready for use\n");
|
||||
}
|
||||
}
|
||||
catch (AggregateException)
|
||||
{
|
||||
Console.WriteLine("Could not connect to discord...");
|
||||
logger.Information("Could not connect to discord...");
|
||||
Environment.Exit(103);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> isConnected()
|
||||
private async Task<bool> isConnected()
|
||||
{
|
||||
while (!client.ConnectionState.Equals(ConnectionState.Connected))
|
||||
await Task.Delay(25);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task HandleCommand(SocketMessage messageParam)
|
||||
private async Task HandleCommand(SocketMessage messageParam)
|
||||
{
|
||||
var message = messageParam as SocketUserMessage;
|
||||
if (message == null) return;
|
||||
|
@ -144,24 +166,23 @@ namespace Geekbot.net
|
|||
var commandExec = commands.ExecuteAsync(context, argPos, servicesProvider);
|
||||
}
|
||||
|
||||
public async Task HandleMessageReceived(SocketMessage messsageParam)
|
||||
private async Task HandleMessageReceived(SocketMessage messsageParam)
|
||||
{
|
||||
var message = messsageParam;
|
||||
if (message == null) return;
|
||||
|
||||
|
||||
var statsRecorder = new StatsRecorder(message, redis);
|
||||
var userRec = statsRecorder.UpdateUserRecordAsync();
|
||||
var guildRec = statsRecorder.UpdateGuildRecordAsync();
|
||||
|
||||
|
||||
if (message.Author.Id == client.CurrentUser.Id) return;
|
||||
var channel = (SocketGuildChannel) message.Channel;
|
||||
Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " +
|
||||
message.Content);
|
||||
logger.Information($"[Message] {channel.Guild.Name} - {message.Channel} - {message.Author.Username} - {message.Content}");
|
||||
await userRec;
|
||||
await guildRec;
|
||||
}
|
||||
|
||||
public async Task HandleUserJoined(SocketGuildUser user)
|
||||
private async Task HandleUserJoined(SocketGuildUser user)
|
||||
{
|
||||
if (!user.IsBot)
|
||||
{
|
||||
|
@ -173,5 +194,31 @@ namespace Geekbot.net
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Task DiscordLogger(LogMessage message)
|
||||
{
|
||||
var logMessage = $"[{message.Source}] {message.Message}";
|
||||
switch (message.Severity)
|
||||
{
|
||||
case LogSeverity.Verbose:
|
||||
logger.Verbose(logMessage);
|
||||
break;
|
||||
case LogSeverity.Debug:
|
||||
logger.Debug(logMessage);
|
||||
break;
|
||||
case LogSeverity.Info:
|
||||
logger.Information(logMessage);
|
||||
break;
|
||||
case LogSeverity.Critical:
|
||||
case LogSeverity.Error:
|
||||
case LogSeverity.Warning:
|
||||
logger.Error(message.Exception, logMessage);
|
||||
break;
|
||||
default:
|
||||
logger.Information($"{logMessage} --- {message.Severity.ToString()}");
|
||||
break;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue