WIP: poll command
This commit is contained in:
parent
9aa2b17d1c
commit
ea76629d6e
5 changed files with 201 additions and 22 deletions
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
@ -9,12 +8,10 @@ namespace Geekbot.net.Commands
|
||||||
{
|
{
|
||||||
public class Help : ModuleBase
|
public class Help : ModuleBase
|
||||||
{
|
{
|
||||||
private readonly CommandService _commands;
|
|
||||||
private readonly IErrorHandler _errorHandler;
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
|
||||||
public Help(CommandService commands, IErrorHandler errorHandler)
|
public Help(IErrorHandler errorHandler)
|
||||||
{
|
{
|
||||||
_commands = commands;
|
|
||||||
_errorHandler = errorHandler;
|
_errorHandler = errorHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
163
Geekbot.net/Commands/Poll.cs
Normal file
163
Geekbot.net/Commands/Poll.cs
Normal file
|
@ -0,0 +1,163 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Commands
|
||||||
|
{
|
||||||
|
[Group("poll")]
|
||||||
|
public class Poll : ModuleBase
|
||||||
|
{
|
||||||
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
private readonly IDatabase _redis;
|
||||||
|
private readonly IEmojiConverter _emojiConverter;
|
||||||
|
private readonly IUserRepository _userRepository;
|
||||||
|
|
||||||
|
public Poll(IErrorHandler errorHandler, IDatabase redis, IEmojiConverter emojiConverter, IUserRepository userRepository)
|
||||||
|
{
|
||||||
|
_errorHandler = errorHandler;
|
||||||
|
_redis = redis;
|
||||||
|
_emojiConverter = emojiConverter;
|
||||||
|
_userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command(RunMode = RunMode.Async)]
|
||||||
|
[Remarks(CommandCategories.Helpers)]
|
||||||
|
[Summary("Check status of the current poll")]
|
||||||
|
public async Task Dflt()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var currentPoll = GetCurrentPoll();
|
||||||
|
if (currentPoll.Question == null)
|
||||||
|
{
|
||||||
|
await ReplyAsync(
|
||||||
|
"There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var results = await getPollResults(currentPoll);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("create", RunMode = RunMode.Async)]
|
||||||
|
[Remarks(CommandCategories.Helpers)]
|
||||||
|
[Summary("Create a poll")]
|
||||||
|
public async Task Create([Remainder, Summary("question;option1;option2")] string rawPollString)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var pollList = rawPollString.Split(';').ToList();
|
||||||
|
if (pollList.Count <= 2)
|
||||||
|
{
|
||||||
|
await ReplyAsync(
|
||||||
|
"You need a question with atleast 2 options, a valid creation would look like this `question;option1;option2`");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var eb = new EmbedBuilder();
|
||||||
|
eb.Title = $":bar_chart: Poll by {Context.User.Username}";
|
||||||
|
var question = pollList[0];
|
||||||
|
eb.Description = question;
|
||||||
|
pollList.RemoveAt(0);
|
||||||
|
var i = 1;
|
||||||
|
pollList.ForEach(option =>
|
||||||
|
{
|
||||||
|
eb.AddInlineField($"Option {_emojiConverter.numberToEmoji(i)}", option);
|
||||||
|
i++;
|
||||||
|
});
|
||||||
|
var pollMessage = await ReplyAsync("", false, eb.Build());
|
||||||
|
i = 1;
|
||||||
|
pollList.ForEach(option =>
|
||||||
|
{
|
||||||
|
pollMessage.AddReactionAsync(new Emoji(_emojiConverter.numberToEmoji(i)));
|
||||||
|
i++;
|
||||||
|
});
|
||||||
|
var poll = new PollData()
|
||||||
|
{
|
||||||
|
Creator = Context.User.Id,
|
||||||
|
MessageId = pollMessage.Id,
|
||||||
|
IsFinshed = false,
|
||||||
|
Question = question,
|
||||||
|
Options = pollList
|
||||||
|
};
|
||||||
|
var pollJson = JsonConvert.SerializeObject(poll);
|
||||||
|
_redis.HashSet($"{Context.Guild.Id}:Polls", new HashEntry[] {new HashEntry(Context.Channel.Id, pollJson)});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("end", RunMode = RunMode.Async)]
|
||||||
|
[Remarks(CommandCategories.Helpers)]
|
||||||
|
[Summary("End the current poll")]
|
||||||
|
public async Task End()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var hasPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id);
|
||||||
|
if (hasPoll.IsNullOrEmpty)
|
||||||
|
{
|
||||||
|
await ReplyAsync(
|
||||||
|
"There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;answer1;answer2;answer3`");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PollData GetCurrentPoll()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var currentPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id);
|
||||||
|
return JsonConvert.DeserializeObject<PollData>(currentPoll.ToString());
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return new PollData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<List<PollResult>> getPollResults(PollData poll)
|
||||||
|
{
|
||||||
|
var message = (IUserMessage)(await Context.Channel.GetMessageAsync(poll.MessageId));
|
||||||
|
foreach (var r in message.Reactions)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{r.Key.Name}: {r.Value.ReactionCount}");
|
||||||
|
}
|
||||||
|
return new List<PollResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PollData
|
||||||
|
{
|
||||||
|
public ulong Creator { get; set; }
|
||||||
|
public ulong MessageId { get; set; }
|
||||||
|
public bool IsFinshed { get; set; }
|
||||||
|
public string Question { get; set; }
|
||||||
|
public List<string> Options { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PollResult
|
||||||
|
{
|
||||||
|
public string Option { get; set; }
|
||||||
|
public string VoteCount { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,13 +17,15 @@ namespace Geekbot.net.Commands
|
||||||
private readonly IErrorHandler _errorHandler;
|
private readonly IErrorHandler _errorHandler;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IUserRepository _userRepository;
|
private readonly IUserRepository _userRepository;
|
||||||
|
private readonly IEmojiConverter _emojiConverter;
|
||||||
|
|
||||||
public Rank(IDatabase redis, IErrorHandler errorHandler, ILogger logger, IUserRepository userRepository)
|
public Rank(IDatabase redis, IErrorHandler errorHandler, ILogger logger, IUserRepository userRepository, IEmojiConverter emojiConverter)
|
||||||
{
|
{
|
||||||
_redis = redis;
|
_redis = redis;
|
||||||
_errorHandler = errorHandler;
|
_errorHandler = errorHandler;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
|
_emojiConverter = emojiConverter;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("rank", RunMode = RunMode.Async)]
|
[Command("rank", RunMode = RunMode.Async)]
|
||||||
|
@ -73,7 +75,7 @@ namespace Geekbot.net.Commands
|
||||||
}
|
}
|
||||||
|
|
||||||
var highScore = new StringBuilder();
|
var highScore = new StringBuilder();
|
||||||
if (failedToRetrieveUser) highScore.AppendLine(":warning: I couldn't get all userdata, sorry! (bugfix coming soon:tm:)\n");
|
if (failedToRetrieveUser) highScore.AppendLine(":warning: I couldn't get all userdata, sorry!\n");
|
||||||
highScore.AppendLine($":bar_chart: **Highscore for {Context.Guild.Name}**");
|
highScore.AppendLine($":bar_chart: **Highscore for {Context.Guild.Name}**");
|
||||||
var highscorePlace = 1;
|
var highscorePlace = 1;
|
||||||
foreach (var user in highscoreUsers)
|
foreach (var user in highscoreUsers)
|
||||||
|
@ -82,12 +84,12 @@ namespace Geekbot.net.Commands
|
||||||
if (user.Key.Username != null)
|
if (user.Key.Username != null)
|
||||||
{
|
{
|
||||||
highScore.AppendLine(
|
highScore.AppendLine(
|
||||||
$"{NumerToEmoji(highscorePlace)} **{user.Key.Username}#{user.Key.Discriminator}** - {percent}% of total - {user.Value} messages");
|
$"{_emojiConverter.numberToEmoji(highscorePlace)} **{user.Key.Username}#{user.Key.Discriminator}** - {percent}% of total - {user.Value} messages");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
highScore.AppendLine(
|
highScore.AppendLine(
|
||||||
$"{NumerToEmoji(highscorePlace)} **{user.Key.Id}** - {percent}% of total - {user.Value} messages");
|
$"{_emojiConverter.numberToEmoji(highscorePlace)} **{user.Key.Id}** - {percent}% of total - {user.Value} messages");
|
||||||
}
|
}
|
||||||
highscorePlace++;
|
highscorePlace++;
|
||||||
}
|
}
|
||||||
|
@ -98,20 +100,6 @@ namespace Geekbot.net.Commands
|
||||||
_errorHandler.HandleCommandException(e, Context);
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string NumerToEmoji(int number)
|
|
||||||
{
|
|
||||||
var emojis = new string[] {":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:", ":keycap_ten:"};
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return emojis[number - 1];
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_logger.Warning(e, $"Can't provide emoji number {number}");
|
|
||||||
return ":zero:";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RankUserPolyfill
|
class RankUserPolyfill
|
||||||
|
|
28
Geekbot.net/Lib/EmojiConverter.cs
Normal file
28
Geekbot.net/Lib/EmojiConverter.cs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Lib
|
||||||
|
{
|
||||||
|
public class EmojiConverter : IEmojiConverter
|
||||||
|
{
|
||||||
|
public string numberToEmoji(int number)
|
||||||
|
{
|
||||||
|
if (number == 10)
|
||||||
|
{
|
||||||
|
return "🔟";
|
||||||
|
}
|
||||||
|
var emojiMap = new string[] {"0⃣", "1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣"};
|
||||||
|
var numbers = number.ToString().ToCharArray();
|
||||||
|
var returnString = new StringBuilder();
|
||||||
|
foreach (var n in numbers)
|
||||||
|
{
|
||||||
|
returnString.Append(emojiMap[int.Parse(n.ToString())]);
|
||||||
|
}
|
||||||
|
return returnString.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IEmojiConverter
|
||||||
|
{
|
||||||
|
string numberToEmoji(int number);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
|
@ -118,12 +119,14 @@ namespace Geekbot.net
|
||||||
var mediaProvider = new MediaProvider(randomClient, logger);
|
var mediaProvider = new MediaProvider(randomClient, logger);
|
||||||
var malClient = new MalClient(redis, logger);
|
var malClient = new MalClient(redis, logger);
|
||||||
var levelCalc = new LevelCalc();
|
var levelCalc = new LevelCalc();
|
||||||
|
var emojiConverter = new EmojiConverter();
|
||||||
|
|
||||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||||
services.AddSingleton(redis);
|
services.AddSingleton(redis);
|
||||||
services.AddSingleton<ILogger>(logger);
|
services.AddSingleton<ILogger>(logger);
|
||||||
services.AddSingleton<IUserRepository>(userRepository);
|
services.AddSingleton<IUserRepository>(userRepository);
|
||||||
services.AddSingleton<ILevelCalc>(levelCalc);
|
services.AddSingleton<ILevelCalc>(levelCalc);
|
||||||
|
services.AddSingleton<IEmojiConverter>(emojiConverter);
|
||||||
services.AddSingleton(randomClient);
|
services.AddSingleton(randomClient);
|
||||||
services.AddSingleton<IFortunesProvider>(fortunes);
|
services.AddSingleton<IFortunesProvider>(fortunes);
|
||||||
services.AddSingleton<IMediaProvider>(mediaProvider);
|
services.AddSingleton<IMediaProvider>(mediaProvider);
|
||||||
|
|
Loading…
Reference in a new issue