From d88e9a6f188a9135b038f8d644ac156904e5d708 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 19 Sep 2017 20:39:49 +0200 Subject: [PATCH] Pandas, rip restsharp, stuff --- Geekbot.net/Geekbot.net.csproj | 84 ++++++++++++------------- Geekbot.net/Lib/CheckEmImageProvider.cs | 6 +- Geekbot.net/Lib/FortunesProvider.cs | 12 +--- Geekbot.net/Lib/PandaProvider.cs | 40 ++++++++++++ Geekbot.net/Modules/Cat.cs | 25 ++++++-- Geekbot.net/Modules/CheckEm.cs | 1 - Geekbot.net/Modules/Dog.cs | 23 +++++-- Geekbot.net/Modules/Panda.cs | 23 +++++++ Geekbot.net/Program.cs | 30 ++++++--- Geekbot.net/Storage/pandas | 11 ++++ 10 files changed, 182 insertions(+), 73 deletions(-) create mode 100644 Geekbot.net/Lib/PandaProvider.cs create mode 100644 Geekbot.net/Modules/Panda.cs create mode 100644 Geekbot.net/Storage/pandas diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 4e2e121..5e543ab 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -1,43 +1,43 @@ - - - Exe - netcoreapp2.0 - derp.ico - 1.1.0 - Pizza and Coffee Studios - Pizza and Coffee Studios - A Discord bot - https://github.com/pizzaandcoffee/Geekbot.net - - - - 1.0.2 - - - 1.29.1.976 - - - 1.5.0.1 - - - - - - - 105.2.3 - - - 1.2.6 - - - 4.3.2 - - - - 4.3.0 - - - 4.3.0 - - + + + Exe + netcoreapp2.0 + derp.ico + 1.1.0 + Pizza and Coffee Studios + Pizza and Coffee Studios + A Discord bot + https://github.com/pizzaandcoffee/Geekbot.net + + + + 1.0.2 + + + 1.29.1.976 + + + 1.5.0.1 + + + + + + + 105.2.3 + + + 1.2.6 + + + 4.3.2 + + + + 4.3.0 + + + 4.3.0 + + \ No newline at end of file diff --git a/Geekbot.net/Lib/CheckEmImageProvider.cs b/Geekbot.net/Lib/CheckEmImageProvider.cs index 5ead4a2..b29a6a2 100644 --- a/Geekbot.net/Lib/CheckEmImageProvider.cs +++ b/Geekbot.net/Lib/CheckEmImageProvider.cs @@ -9,7 +9,7 @@ namespace Geekbot.net.Lib private readonly Random rnd; private readonly int totalCheckEmImages; - public CheckEmImageProvider() + public CheckEmImageProvider(Random rnd) { var path = Path.GetFullPath("./Storage/checkEmPics"); if (File.Exists(path)) @@ -17,8 +17,8 @@ namespace Geekbot.net.Lib var rawCheckEmPics = File.ReadAllText(path); checkEmImageArray = rawCheckEmPics.Split("\n"); totalCheckEmImages = checkEmImageArray.Length; - rnd = new Random(); - Console.WriteLine($"- Loaded {totalCheckEmImages} CheckEm Images"); + this.rnd = rnd; + Console.WriteLine($"-- Loaded {totalCheckEmImages} CheckEm Images"); } else { diff --git a/Geekbot.net/Lib/FortunesProvider.cs b/Geekbot.net/Lib/FortunesProvider.cs index 614d018..2c6a2e5 100644 --- a/Geekbot.net/Lib/FortunesProvider.cs +++ b/Geekbot.net/Lib/FortunesProvider.cs @@ -9,7 +9,7 @@ namespace Geekbot.net.Lib private readonly Random rnd; private readonly int totalFortunes; - public FortunesProvider() + public FortunesProvider(Random rnd) { var path = Path.GetFullPath("./Storage/fortunes"); if (File.Exists(path)) @@ -17,8 +17,8 @@ namespace Geekbot.net.Lib var rawFortunes = File.ReadAllText(path); fortuneArray = rawFortunes.Split("%"); totalFortunes = fortuneArray.Length; - rnd = new Random(); - Console.WriteLine($"- Loaded {totalFortunes} Fortunes"); + this.rnd = rnd; + Console.WriteLine($"-- Loaded {totalFortunes} Fortunes"); } else { @@ -31,16 +31,10 @@ namespace Geekbot.net.Lib { return fortuneArray[rnd.Next(0, totalFortunes)]; } - - public string GetFortune(int id) - { - return fortuneArray[id]; - } } public interface IFortunesProvider { string GetRandomFortune(); - string GetFortune(int id); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/PandaProvider.cs b/Geekbot.net/Lib/PandaProvider.cs new file mode 100644 index 0000000..9a392e5 --- /dev/null +++ b/Geekbot.net/Lib/PandaProvider.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; + +namespace Geekbot.net.Lib +{ + public class PandaProvider : IPandaProvider + { + private readonly string[] PandaArray; + private readonly Random rnd; + private readonly int totalPandas; + + public PandaProvider(Random rnd) + { + var path = Path.GetFullPath("./Storage/pandas"); + if (File.Exists(path)) + { + var rawFortunes = File.ReadAllText(path); + PandaArray = rawFortunes.Split("\n"); + totalPandas = PandaArray.Length; + this.rnd = rnd; + Console.WriteLine($"-- Loaded {totalPandas} Panda Images"); + } + else + { + Console.WriteLine("Pandas File not found"); + Console.WriteLine($"Path should be {path}"); + } + } + + public string GetRandomPanda() + { + return PandaArray[rnd.Next(0, totalPandas)]; + } + } + + public interface IPandaProvider + { + string GetRandomPanda(); + } +} \ No newline at end of file diff --git a/Geekbot.net/Modules/Cat.cs b/Geekbot.net/Modules/Cat.cs index fe134ad..bf6fa81 100644 --- a/Geekbot.net/Modules/Cat.cs +++ b/Geekbot.net/Modules/Cat.cs @@ -1,6 +1,8 @@ -using System.Threading.Tasks; +using System; +using System.Net.Http; +using System.Threading.Tasks; using Discord.Commands; -using RestSharp; +using Newtonsoft.Json; namespace Geekbot.net.Modules { @@ -10,10 +12,23 @@ namespace Geekbot.net.Modules [Summary("Return a random image of a cat.")] public async Task Say() { - var catClient = new RestClient("http://random.cat"); - var request = new RestRequest("meow.php", Method.GET); + using (var client = new HttpClient()) + { + try + { + client.BaseAddress = new Uri("http://random.cat"); + var response = await client.GetAsync("/meow.php"); + response.EnsureSuccessStatusCode(); - catClient.ExecuteAsync(request, async response => { await ReplyAsync(response.Data.file); }); + var stringResponse = await response.Content.ReadAsStringAsync(); + var catFile = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(catFile.file); + } + catch (HttpRequestException e) + { + await ReplyAsync($"Seems like the dog cought the cat (error occured)\r\n{e.Message}"); + } + } } } diff --git a/Geekbot.net/Modules/CheckEm.cs b/Geekbot.net/Modules/CheckEm.cs index 0d72241..1751462 100644 --- a/Geekbot.net/Modules/CheckEm.cs +++ b/Geekbot.net/Modules/CheckEm.cs @@ -28,7 +28,6 @@ namespace Geekbot.net.Modules var dubtriqua = ""; var ns = GetIntArray(number); - Console.WriteLine(ns.Length); if (ns[7] == ns[6]) { dubtriqua = "DUBS"; diff --git a/Geekbot.net/Modules/Dog.cs b/Geekbot.net/Modules/Dog.cs index 80fd250..326e4ac 100644 --- a/Geekbot.net/Modules/Dog.cs +++ b/Geekbot.net/Modules/Dog.cs @@ -1,7 +1,8 @@ using System; +using System.Net.Http; using System.Threading.Tasks; using Discord.Commands; -using RestSharp; +using Newtonsoft.Json; namespace Geekbot.net.Modules { @@ -11,11 +12,23 @@ namespace Geekbot.net.Modules [Summary("Return a random image of a dog.")] public async Task Say() { - var dogClient = new RestClient("http://random.dog"); - var request = new RestRequest("woof.json", Method.GET); - Console.WriteLine(dogClient.BaseUrl); + using (var client = new HttpClient()) + { + try + { + client.BaseAddress = new Uri("http://random.dog"); + var response = await client.GetAsync("/woof.json"); + response.EnsureSuccessStatusCode(); - dogClient.ExecuteAsync(request, async response => { await ReplyAsync(response.Data.url); }); + var stringResponse = await response.Content.ReadAsStringAsync(); + var dogFile = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(dogFile.url); + } + catch (HttpRequestException e) + { + await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}"); + } + } } } diff --git a/Geekbot.net/Modules/Panda.cs b/Geekbot.net/Modules/Panda.cs new file mode 100644 index 0000000..515dfed --- /dev/null +++ b/Geekbot.net/Modules/Panda.cs @@ -0,0 +1,23 @@ +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Lib; + +namespace Geekbot.net.Modules +{ + public class Panda : ModuleBase + { + private readonly IPandaProvider pandaImages; + + public Panda(IPandaProvider pandaImages) + { + this.pandaImages = pandaImages; + } + + [Command("panda", RunMode = RunMode.Async)] + [Summary("Get a random panda")] + public async Task PandaCommand() + { + await ReplyAsync(pandaImages.GetRandomPanda()); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 38bea6a..edb18c1 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Net.NetworkInformation; using System.Reflection; using System.Threading.Tasks; using Discord; @@ -35,6 +36,15 @@ namespace Geekbot.net public async Task MainAsync() { Console.WriteLine("* Initing Stuff"); + + var ping = new Ping().Send("8.8.8.8"); + if(ping.Status != IPStatus.Success) + { + Console.WriteLine("It seems that you are offline"); + Console.WriteLine("Please connect to the Internet"); + Environment.Exit(101); + } + client = new DiscordSocketClient(); commands = new CommandService(); @@ -42,12 +52,12 @@ namespace Geekbot.net { var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); redis = redisMultiplexer.GetDatabase(6); - Console.WriteLine("- Connected to Redis (db6)"); + Console.WriteLine("-- Connected to Redis (db6)"); } catch (Exception) { Console.WriteLine("Start Redis pls..."); - Environment.Exit(1); + Environment.Exit(102); } token = redis.StringGet("discordToken"); @@ -64,13 +74,15 @@ namespace Geekbot.net } services = new ServiceCollection(); - var fortunes = new FortunesProvider(); - var checkEmImages = new CheckEmImageProvider(); var RandomClient = new Random(); + var fortunes = new FortunesProvider(RandomClient); + var checkEmImages = new CheckEmImageProvider(RandomClient); + var pandaImages = new PandaProvider(RandomClient); services.AddSingleton(redis); services.AddSingleton(RandomClient); services.AddSingleton(fortunes); services.AddSingleton(checkEmImages); + services.AddSingleton(pandaImages); Console.WriteLine("* Connecting to Discord"); @@ -106,7 +118,7 @@ namespace Geekbot.net catch (AggregateException) { Console.WriteLine("Could not connect to discord..."); - Environment.Exit(1); + Environment.Exit(103); } } @@ -137,7 +149,7 @@ namespace Geekbot.net if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return; var context = new CommandContext(client, message); - Task.Run(async () => await commands.ExecuteAsync(context, argPos, servicesProvider)); + var commandExec = commands.ExecuteAsync(context, argPos, servicesProvider); } public async Task HandleMessageReceived(SocketMessage messsageParam) @@ -146,13 +158,15 @@ namespace Geekbot.net if (message == null) return; var statsRecorder = new StatsRecorder(message, redis); - Task.Run(async () => await statsRecorder.UpdateUserRecordAsync()); - Task.Run(async () => await statsRecorder.UpdateGuildRecordAsync()); + 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); + await userRec; + await guildRec; } public async Task HandleUserJoined(SocketGuildUser user) diff --git a/Geekbot.net/Storage/pandas b/Geekbot.net/Storage/pandas new file mode 100644 index 0000000..9d5046c --- /dev/null +++ b/Geekbot.net/Storage/pandas @@ -0,0 +1,11 @@ +https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/Grosser_Panda.JPG/1200px-Grosser_Panda.JPG +https://c402277.ssl.cf1.rackcdn.com/photos/13100/images/featured_story/BIC_128.png?1485963152 +https://nationalzoo.si.edu/sites/default/files/styles/slide_1400x700/public/support/adopt/giantpanda-03.jpg?itok=3EdEO0Vi +https://media4.s-nbcnews.com/j/newscms/2016_36/1685951/ss-160826-twip-05_8cf6d4cb83758449fd400c7c3d71aa1f.nbcnews-ux-2880-1000.jpg +https://ichef-1.bbci.co.uk/news/660/cpsprodpb/169F6/production/_91026629_gettyimages-519508400.jpg +https://cdn.history.com/sites/2/2017/03/GettyImages-157278376.jpg +https://www.pandasinternational.org/wptemp/wp-content/uploads/2012/10/slider1.jpg +https://tctechcrunch2011.files.wordpress.com/2015/11/panda.jpg +http://www.nationalgeographic.com/content/dam/magazine/rights-exempt/2016/08/departments/panda-mania-12.jpg +http://animals.sandiegozoo.org/sites/default/files/2016-09/panda1_10.jpg +http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Mammals/A-G/giant-panda-eating.adapt.945.1.jpg \ No newline at end of file