Code Cleanup, thanks resharper

This commit is contained in:
runebaas 2017-12-29 01:53:50 +01:00
parent 00035ac4b1
commit 813698394a
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
34 changed files with 298 additions and 325 deletions

View file

@ -14,12 +14,13 @@ namespace Geekbot.net.Commands
[Group("poll")]
public class Poll : ModuleBase
{
private readonly IEmojiConverter _emojiConverter;
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)
public Poll(IErrorHandler errorHandler, IDatabase redis, IEmojiConverter emojiConverter,
IUserRepository userRepository)
{
_errorHandler = errorHandler;
_redis = redis;
@ -41,6 +42,7 @@ namespace Geekbot.net.Commands
"There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`");
return;
}
await ReplyAsync("There is a poll running at the moment");
}
catch (Exception e)
@ -48,11 +50,12 @@ namespace Geekbot.net.Commands
_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)
public async Task Create([Remainder] [Summary("question;option1;option2")]
string rawPollString)
{
try
{
@ -62,6 +65,7 @@ namespace Geekbot.net.Commands
await ReplyAsync("You have not finished you last poll yet. To finish it use `!poll end`");
return;
}
var pollList = rawPollString.Split(';').ToList();
if (pollList.Count <= 2)
{
@ -88,7 +92,7 @@ namespace Geekbot.net.Commands
pollMessage.AddReactionAsync(new Emoji(_emojiConverter.numberToEmoji(i)));
i++;
});
var poll = new PollData()
var poll = new PollData
{
Creator = Context.User.Id,
MessageId = pollMessage.Id,
@ -97,14 +101,14 @@ namespace Geekbot.net.Commands
Options = pollList
};
var pollJson = JsonConvert.SerializeObject(poll);
_redis.HashSet($"{Context.Guild.Id}:Polls", new HashEntry[] {new HashEntry(Context.Channel.Id, pollJson)});
_redis.HashSet($"{Context.Guild.Id}:Polls", new[] {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")]
@ -118,18 +122,16 @@ namespace Geekbot.net.Commands
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}");
}
foreach (var result in results) sb.AppendLine($"{result.VoteCount} - {result.Option}");
await ReplyAsync(sb.ToString());
currentPoll.IsFinshed = true;
var pollJson = JsonConvert.SerializeObject(currentPoll);
_redis.HashSet($"{Context.Guild.Id}:Polls", new HashEntry[] {new HashEntry(Context.Channel.Id, pollJson)});
_redis.HashSet($"{Context.Guild.Id}:Polls", new[] {new HashEntry(Context.Channel.Id, pollJson)});
}
catch (Exception e)
{
@ -144,7 +146,7 @@ namespace Geekbot.net.Commands
var currentPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id);
return JsonConvert.DeserializeObject<PollData>(currentPoll.ToString());
}
catch
catch
{
return new PollData();
}
@ -152,14 +154,13 @@ namespace Geekbot.net.Commands
private async Task<List<PollResult>> getPollResults(PollData poll)
{
var message = (IUserMessage)(await Context.Channel.GetMessageAsync(poll.MessageId));
var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId);
var results = new List<PollResult>();
foreach (var r in message.Reactions)
{
try
{
var option = int.Parse(r.Key.Name.ToCharArray()[0].ToString());
var result = new PollResult()
var result = new PollResult
{
Option = poll.Options[option - 1],
VoteCount = r.Value.ReactionCount
@ -167,8 +168,8 @@ namespace Geekbot.net.Commands
results.Add(result);
}
catch {}
}
results.Sort((x,y) => y.VoteCount.CompareTo(x.VoteCount));
results.Sort((x, y) => y.VoteCount.CompareTo(x.VoteCount));
return results;
}
@ -180,7 +181,7 @@ namespace Geekbot.net.Commands
public string Question { get; set; }
public List<string> Options { get; set; }
}
private class PollResult
{
public string Option { get; set; }