Finalize poll command and add emojify command (the fuck am i doing with my life)

This commit is contained in:
Runebaas 2017-11-08 01:30:50 +01:00
parent ea76629d6e
commit f96954a7e1
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
3 changed files with 124 additions and 14 deletions

View file

@ -0,0 +1,37 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
namespace Geekbot.net.Commands
{
public class Emojify : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly IEmojiConverter _emojiConverter;
public Emojify(IErrorHandler errorHandler, IEmojiConverter emojiConverter)
{
_errorHandler = errorHandler;
_emojiConverter = emojiConverter;
}
[Command("emojify", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Helpers)]
[Summary("Emojify text")]
public async Task Dflt([Remainder, Summary("text")] string text)
{
try
{
var sb = new StringBuilder();
await ReplyAsync($"*{Context.User.Username}#{Context.User.Discriminator} said:*");
await ReplyAsync(_emojiConverter.textToEmoji(text));
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
@ -34,14 +35,13 @@ namespace Geekbot.net.Commands
try
{
var currentPoll = GetCurrentPoll();
if (currentPoll.Question == null)
if (currentPoll.Question == null || currentPoll.IsFinshed)
{
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);
await ReplyAsync("There is a poll running at the moment");
}
catch (Exception e)
{
@ -65,7 +65,7 @@ namespace Geekbot.net.Commands
}
var eb = new EmbedBuilder();
eb.Title = $":bar_chart: Poll by {Context.User.Username}";
eb.Title = $"Poll by {Context.User.Username}";
var question = pollList[0];
eb.Description = question;
pollList.RemoveAt(0);
@ -106,15 +106,21 @@ namespace Geekbot.net.Commands
{
try
{
var hasPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id);
if (hasPoll.IsNullOrEmpty)
var currentPoll = GetCurrentPoll();
if (currentPoll.Question == null || currentPoll.IsFinshed)
{
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`");
await ReplyAsync("There is no ongoing poll at the moment");
return;
}
var results = await getPollResults(currentPoll);
var sb = new StringBuilder();
sb.AppendLine("**Poll Results**");
sb.AppendLine(currentPoll.Question);
foreach (var result in results)
{
sb.AppendLine($"{result.VoteCount} - {result.Option}");
}
await ReplyAsync(sb.ToString());
}
catch (Exception e)
{
@ -138,11 +144,19 @@ namespace Geekbot.net.Commands
private async Task<List<PollResult>> getPollResults(PollData poll)
{
var message = (IUserMessage)(await Context.Channel.GetMessageAsync(poll.MessageId));
var results = new List<PollResult>();
foreach (var r in message.Reactions)
{
Console.WriteLine($"{r.Key.Name}: {r.Value.ReactionCount}");
var option = int.Parse(r.Key.Name.ToCharArray()[0].ToString());
var result = new PollResult()
{
Option = poll.Options[option - 1],
VoteCount = r.Value.ReactionCount
};
results.Add(result);
}
return new List<PollResult>();
results.Sort((x,y) => y.VoteCount.CompareTo(x.VoteCount));
return results;
}
private class PollData
@ -157,7 +171,7 @@ namespace Geekbot.net.Commands
private class PollResult
{
public string Option { get; set; }
public string VoteCount { get; set; }
public int VoteCount { get; set; }
}
}
}

View file

@ -1,4 +1,5 @@
using System.Text;
using System.Collections;
using System.Text;
namespace Geekbot.net.Lib
{
@ -19,10 +20,68 @@ namespace Geekbot.net.Lib
}
return returnString.ToString();
}
public string textToEmoji(string text)
{
var emojiMap = new Hashtable
{
['A'] = ":regional_indicator_a:",
['B'] = ":b:",
['C'] = ":regional_indicator_c:",
['D'] = ":regional_indicator_d:",
['E'] = ":regional_indicator_e:",
['F'] = ":regional_indicator_f:",
['G'] = ":regional_indicator_g:",
['H'] = ":regional_indicator_h:",
['I'] = ":regional_indicator_i:",
['J'] = ":regional_indicator_j:",
['K'] = ":regional_indicator_k:",
['L'] = ":regional_indicator_l:",
['M'] = ":regional_indicator_m:",
['N'] = ":regional_indicator_n:",
['O'] = ":regional_indicator_o:",
['P'] = ":regional_indicator_p:",
['Q'] = ":regional_indicator_q:",
['R'] = ":regional_indicator_r:",
['S'] = ":regional_indicator_s:",
['T'] = ":regional_indicator_t:",
['U'] = ":regional_indicator_u:",
['V'] = ":regional_indicator_v:",
['W'] = ":regional_indicator_w:",
['X'] = ":regional_indicator_x:",
['Y'] = ":regional_indicator_y:",
['Z'] = ":regional_indicator_z:",
['!'] = ":exclamation:",
['?'] = ":question:",
['#'] = ":hash:",
['*'] = ":star2:",
['+'] = ":heavy_plus_sign:",
['0'] = ":zero:",
['1'] = ":one:",
['2'] = ":two:",
['3'] = ":three:",
['4'] = ":four:",
['5'] = ":five",
['6'] = ":six",
['7'] = ":seven:",
['8'] = ":eight",
['9'] = ":nine:",
[' '] = " "
};
var letters = text.ToUpper().ToCharArray();
var returnString = new StringBuilder();
foreach (var n in letters)
{
var emoji = emojiMap[n] ?? n;
returnString.Append(emoji);
}
return returnString.ToString();
}
}
public interface IEmojiConverter
{
string numberToEmoji(int number);
string textToEmoji(string text);
}
}