Pandas, rip restsharp, stuff
This commit is contained in:
parent
3a5a0df846
commit
d88e9a6f18
10 changed files with 182 additions and 73 deletions
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
40
Geekbot.net/Lib/PandaProvider.cs
Normal file
40
Geekbot.net/Lib/PandaProvider.cs
Normal 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();
|
||||
}
|
||||
}
|
|
@ -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<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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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<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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
23
Geekbot.net/Modules/Panda.cs
Normal file
23
Geekbot.net/Modules/Panda.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<IFortunesProvider>(fortunes);
|
||||
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
||||
services.AddSingleton<IPandaProvider>(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)
|
||||
|
|
11
Geekbot.net/Storage/pandas
Normal file
11
Geekbot.net/Storage/pandas
Normal 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
|
Loading…
Reference in a new issue