geekbot/Geekbot.net/Lib/Media/FortunesProvider.cs
2018-04-30 23:44:19 +02:00

37 lines
No EOL
1 KiB
C#

using System;
using System.IO;
namespace Geekbot.net.Lib.Media
{
internal class FortunesProvider : IFortunesProvider
{
private readonly string[] _fortuneArray;
private readonly int _totalFortunes;
public FortunesProvider(IGeekbotLogger logger)
{
var path = Path.GetFullPath("./Storage/fortunes");
if (File.Exists(path))
{
var rawFortunes = File.ReadAllText(path);
_fortuneArray = rawFortunes.Split("%");
_totalFortunes = _fortuneArray.Length;
logger.Debug("Geekbot", "Loaded {totalFortunes} Fortunes");
}
else
{
logger.Information("Geekbot", $"Fortunes File not found at {path}");
}
}
public string GetRandomFortune()
{
return _fortuneArray[new Random().Next(0, _totalFortunes)];
}
}
public interface IFortunesProvider
{
string GetRandomFortune();
}
}