Adding Fortunes

This commit is contained in:
Runebaas 2017-09-15 00:31:13 +02:00
parent 40bbef9cd8
commit 35064cf90b
5 changed files with 9838 additions and 5 deletions

View file

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Geekbot.net.Lib
{
class Fortunes : IFortunes
{
private string[] fortuneArray;
private int totalFortunes;
private Random rnd;
public Fortunes()
{
var path = Path.GetFullPath("./fortunes");
if (File.Exists(path))
{
var rawFortunes= File.ReadAllText(path);
fortuneArray = rawFortunes.Split("%");
totalFortunes = fortuneArray.Length;
rnd = new Random();
}
else
{
Console.WriteLine("Fortunes File not found");
Console.WriteLine($"Path should be {path}");
}
}
public string GetRandomFortune()
{
return fortuneArray[rnd.Next(0, totalFortunes)];
}
public string GetFortune(int id)
{
return fortuneArray[id];
}
}
public interface IFortunes
{
string GetRandomFortune();
string GetFortune(int id);
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
namespace Geekbot.net.Modules
{
public class Fortune : ModuleBase
{
private readonly IFortunes fortunes;
public Fortune(IFortunes fortunes)
{
this.fortunes = fortunes;
}
[Command("fortune", RunMode = RunMode.Async), Summary("Get a random fortune")]
public async Task GetAFortune()
{
await ReplyAsync(fortunes.GetRandomFortune());
}
}
}

View file

@ -32,13 +32,14 @@ namespace Geekbot.net
Console.WriteLine(@"| |_| | |___| |___| . \| |_) | |_| || |");
Console.WriteLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
Console.WriteLine("=========================================");
Console.WriteLine("Starting...");
Console.WriteLine("* Starting...");
new Program().MainAsync().GetAwaiter().GetResult();
}
public async Task MainAsync()
{
Console.WriteLine("* Initing Stuff");
client = new DiscordSocketClient();
commands = new CommandService();
@ -67,11 +68,13 @@ namespace Geekbot.net
}
services = new ServiceCollection();
var fortunes = new Fortunes();
var RandomClient = new Random();
services.AddSingleton<IFortunes>(fortunes);
services.AddSingleton(RandomClient);
services.AddSingleton(redis);
Console.WriteLine("Connecting to Discord...");
Console.WriteLine("* Connecting to Discord");
await Login();
@ -88,9 +91,9 @@ namespace Geekbot.net
if (isConneted)
{
await client.SetGameAsync("Ping Pong");
Console.WriteLine($"Now Connected to {client.Guilds.Count} Servers");
Console.WriteLine($"* Now Connected to {client.Guilds.Count} Servers");
Console.WriteLine("Registering Stuff");
Console.WriteLine("* Registering Stuff");
client.MessageReceived += HandleCommand;
client.MessageReceived += HandleMessageReceived;
@ -98,7 +101,7 @@ namespace Geekbot.net
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
servicesProvider = services.BuildServiceProvider();
Console.WriteLine("Done and ready for use...\n");
Console.WriteLine("* Done and ready for use\n");
}
}
catch (AggregateException)

9760
Geekbot.net/fortunes Normal file

File diff suppressed because it is too large Load diff

BIN
derp.ico

Binary file not shown.

Before

Width:  |  Height:  |  Size: 361 KiB