Pandas, rip restsharp, stuff

This commit is contained in:
Runebaas 2017-09-19 20:39:49 +02:00
parent 3a5a0df846
commit d88e9a6f18
10 changed files with 182 additions and 73 deletions

View file

@ -1,43 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<ApplicationIcon>derp.ico</ApplicationIcon> <ApplicationIcon>derp.ico</ApplicationIcon>
<Version>1.1.0</Version> <Version>1.1.0</Version>
<Company>Pizza and Coffee Studios</Company> <Company>Pizza and Coffee Studios</Company>
<Authors>Pizza and Coffee Studios</Authors> <Authors>Pizza and Coffee Studios</Authors>
<Description>A Discord bot</Description> <Description>A Discord bot</Description>
<RepositoryUrl>https://github.com/pizzaandcoffee/Geekbot.net</RepositoryUrl> <RepositoryUrl>https://github.com/pizzaandcoffee/Geekbot.net</RepositoryUrl>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Discord.Net"> <PackageReference Include="Discord.Net">
<Version>1.0.2</Version> <Version>1.0.2</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Google.Apis.YouTube.v3"> <PackageReference Include="Google.Apis.YouTube.v3">
<Version>1.29.1.976</Version> <Version>1.29.1.976</Version>
</PackageReference> </PackageReference>
<PackageReference Include="HtmlAgilityPack.NetCore"> <PackageReference Include="HtmlAgilityPack.NetCore">
<Version>1.5.0.1</Version> <Version>1.5.0.1</Version>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="RestSharp.NetCore"> <PackageReference Include="RestSharp.NetCore">
<Version>105.2.3</Version> <Version>105.2.3</Version>
</PackageReference> </PackageReference>
<PackageReference Include="StackExchange.Redis"> <PackageReference Include="StackExchange.Redis">
<Version>1.2.6</Version> <Version>1.2.6</Version>
</PackageReference> </PackageReference>
<PackageReference Include="System.Net.Http"> <PackageReference Include="System.Net.Http">
<Version>4.3.2</Version> <Version>4.3.2</Version>
</PackageReference> </PackageReference>
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" /> <PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Json"> <PackageReference Include="System.Runtime.Serialization.Json">
<Version>4.3.0</Version> <Version>4.3.0</Version>
</PackageReference> </PackageReference>
<PackageReference Include="System.Runtime.Serialization.Primitives"> <PackageReference Include="System.Runtime.Serialization.Primitives">
<Version>4.3.0</Version> <Version>4.3.0</Version>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -9,7 +9,7 @@ namespace Geekbot.net.Lib
private readonly Random rnd; private readonly Random rnd;
private readonly int totalCheckEmImages; private readonly int totalCheckEmImages;
public CheckEmImageProvider() public CheckEmImageProvider(Random rnd)
{ {
var path = Path.GetFullPath("./Storage/checkEmPics"); var path = Path.GetFullPath("./Storage/checkEmPics");
if (File.Exists(path)) if (File.Exists(path))
@ -17,8 +17,8 @@ namespace Geekbot.net.Lib
var rawCheckEmPics = File.ReadAllText(path); var rawCheckEmPics = File.ReadAllText(path);
checkEmImageArray = rawCheckEmPics.Split("\n"); checkEmImageArray = rawCheckEmPics.Split("\n");
totalCheckEmImages = checkEmImageArray.Length; totalCheckEmImages = checkEmImageArray.Length;
rnd = new Random(); this.rnd = rnd;
Console.WriteLine($"- Loaded {totalCheckEmImages} CheckEm Images"); Console.WriteLine($"-- Loaded {totalCheckEmImages} CheckEm Images");
} }
else else
{ {

View file

@ -9,7 +9,7 @@ namespace Geekbot.net.Lib
private readonly Random rnd; private readonly Random rnd;
private readonly int totalFortunes; private readonly int totalFortunes;
public FortunesProvider() public FortunesProvider(Random rnd)
{ {
var path = Path.GetFullPath("./Storage/fortunes"); var path = Path.GetFullPath("./Storage/fortunes");
if (File.Exists(path)) if (File.Exists(path))
@ -17,8 +17,8 @@ namespace Geekbot.net.Lib
var rawFortunes = File.ReadAllText(path); var rawFortunes = File.ReadAllText(path);
fortuneArray = rawFortunes.Split("%"); fortuneArray = rawFortunes.Split("%");
totalFortunes = fortuneArray.Length; totalFortunes = fortuneArray.Length;
rnd = new Random(); this.rnd = rnd;
Console.WriteLine($"- Loaded {totalFortunes} Fortunes"); Console.WriteLine($"-- Loaded {totalFortunes} Fortunes");
} }
else else
{ {
@ -31,16 +31,10 @@ namespace Geekbot.net.Lib
{ {
return fortuneArray[rnd.Next(0, totalFortunes)]; return fortuneArray[rnd.Next(0, totalFortunes)];
} }
public string GetFortune(int id)
{
return fortuneArray[id];
}
} }
public interface IFortunesProvider public interface IFortunesProvider
{ {
string GetRandomFortune(); string GetRandomFortune();
string GetFortune(int id);
} }
} }

View file

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

View file

@ -1,6 +1,8 @@
using System.Threading.Tasks; using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using RestSharp; using Newtonsoft.Json;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
@ -10,10 +12,23 @@ namespace Geekbot.net.Modules
[Summary("Return a random image of a cat.")] [Summary("Return a random image of a cat.")]
public async Task Say() public async Task Say()
{ {
var catClient = new RestClient("http://random.cat"); using (var client = new HttpClient())
var request = new RestRequest("meow.php", Method.GET); {
try
{
client.BaseAddress = new Uri("http://random.cat");
var response = await client.GetAsync("/meow.php");
response.EnsureSuccessStatusCode();
catClient.ExecuteAsync<CatResponse>(request, async response => { await ReplyAsync(response.Data.file); }); var stringResponse = await response.Content.ReadAsStringAsync();
var catFile = JsonConvert.DeserializeObject<CatResponse>(stringResponse);
await ReplyAsync(catFile.file);
}
catch (HttpRequestException e)
{
await ReplyAsync($"Seems like the dog cought the cat (error occured)\r\n{e.Message}");
}
}
} }
} }

View file

@ -28,7 +28,6 @@ namespace Geekbot.net.Modules
var dubtriqua = ""; var dubtriqua = "";
var ns = GetIntArray(number); var ns = GetIntArray(number);
Console.WriteLine(ns.Length);
if (ns[7] == ns[6]) if (ns[7] == ns[6])
{ {
dubtriqua = "DUBS"; dubtriqua = "DUBS";

View file

@ -1,7 +1,8 @@
using System; using System;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using RestSharp; using Newtonsoft.Json;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
@ -11,11 +12,23 @@ namespace Geekbot.net.Modules
[Summary("Return a random image of a dog.")] [Summary("Return a random image of a dog.")]
public async Task Say() public async Task Say()
{ {
var dogClient = new RestClient("http://random.dog"); using (var client = new HttpClient())
var request = new RestRequest("woof.json", Method.GET); {
Console.WriteLine(dogClient.BaseUrl); try
{
client.BaseAddress = new Uri("http://random.dog");
var response = await client.GetAsync("/woof.json");
response.EnsureSuccessStatusCode();
dogClient.ExecuteAsync<DogResponse>(request, async response => { await ReplyAsync(response.Data.url); }); var stringResponse = await response.Content.ReadAsStringAsync();
var dogFile = JsonConvert.DeserializeObject<DogResponse>(stringResponse);
await ReplyAsync(dogFile.url);
}
catch (HttpRequestException e)
{
await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}");
}
}
} }
} }

View file

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

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Net.NetworkInformation;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
@ -35,6 +36,15 @@ namespace Geekbot.net
public async Task MainAsync() public async Task MainAsync()
{ {
Console.WriteLine("* Initing Stuff"); 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(); client = new DiscordSocketClient();
commands = new CommandService(); commands = new CommandService();
@ -42,12 +52,12 @@ namespace Geekbot.net
{ {
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
redis = redisMultiplexer.GetDatabase(6); redis = redisMultiplexer.GetDatabase(6);
Console.WriteLine("- Connected to Redis (db6)"); Console.WriteLine("-- Connected to Redis (db6)");
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine("Start Redis pls..."); Console.WriteLine("Start Redis pls...");
Environment.Exit(1); Environment.Exit(102);
} }
token = redis.StringGet("discordToken"); token = redis.StringGet("discordToken");
@ -64,13 +74,15 @@ namespace Geekbot.net
} }
services = new ServiceCollection(); services = new ServiceCollection();
var fortunes = new FortunesProvider();
var checkEmImages = new CheckEmImageProvider();
var RandomClient = new Random(); var RandomClient = new Random();
var fortunes = new FortunesProvider(RandomClient);
var checkEmImages = new CheckEmImageProvider(RandomClient);
var pandaImages = new PandaProvider(RandomClient);
services.AddSingleton(redis); services.AddSingleton(redis);
services.AddSingleton(RandomClient); services.AddSingleton(RandomClient);
services.AddSingleton<IFortunesProvider>(fortunes); services.AddSingleton<IFortunesProvider>(fortunes);
services.AddSingleton<ICheckEmImageProvider>(checkEmImages); services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
services.AddSingleton<IPandaProvider>(pandaImages);
Console.WriteLine("* Connecting to Discord"); Console.WriteLine("* Connecting to Discord");
@ -106,7 +118,7 @@ namespace Geekbot.net
catch (AggregateException) catch (AggregateException)
{ {
Console.WriteLine("Could not connect to discord..."); 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) || if (!(message.HasCharPrefix('!', ref argPos) ||
message.HasMentionPrefix(client.CurrentUser, ref argPos))) return; message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
var context = new CommandContext(client, message); 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) public async Task HandleMessageReceived(SocketMessage messsageParam)
@ -146,13 +158,15 @@ namespace Geekbot.net
if (message == null) return; if (message == null) return;
var statsRecorder = new StatsRecorder(message, redis); var statsRecorder = new StatsRecorder(message, redis);
Task.Run(async () => await statsRecorder.UpdateUserRecordAsync()); var userRec = statsRecorder.UpdateUserRecordAsync();
Task.Run(async () => await statsRecorder.UpdateGuildRecordAsync()); var guildRec = statsRecorder.UpdateGuildRecordAsync();
if (message.Author.Id == client.CurrentUser.Id) return; if (message.Author.Id == client.CurrentUser.Id) return;
var channel = (SocketGuildChannel) message.Channel; var channel = (SocketGuildChannel) message.Channel;
Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " +
message.Content); message.Content);
await userRec;
await guildRec;
} }
public async Task HandleUserJoined(SocketGuildUser user) public async Task HandleUserJoined(SocketGuildUser user)

View file

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