>poll fixed

This commit is contained in:
Kwoth 2016-10-24 17:34:30 +02:00
parent 19db9c6e46
commit a1056c9194
2 changed files with 18 additions and 23 deletions

View File

@ -1,5 +1,6 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
@ -52,8 +53,9 @@ namespace NadekoBot.Modules.Games
public class Poll public class Poll
{ {
private readonly IUserMessage umsg; private readonly IUserMessage umsg;
private readonly IGuild guild;
private readonly string[] answers; private readonly string[] answers;
private ConcurrentDictionary<IUser, int> participants = new ConcurrentDictionary<IUser, int>(); private ConcurrentDictionary<ulong, int> participants = new ConcurrentDictionary<ulong, int>();
private readonly string question; private readonly string question;
private DateTime started; private DateTime started;
private CancellationTokenSource pollCancellationSource = new CancellationTokenSource(); private CancellationTokenSource pollCancellationSource = new CancellationTokenSource();
@ -61,6 +63,7 @@ namespace NadekoBot.Modules.Games
public Poll(IUserMessage umsg, string question, IEnumerable<string> enumerable) public Poll(IUserMessage umsg, string question, IEnumerable<string> enumerable)
{ {
this.umsg = umsg; this.umsg = umsg;
this.guild = ((ITextChannel)umsg.Channel).Guild;
this.question = question; this.question = question;
this.answers = enumerable as string[] ?? enumerable.ToArray(); this.answers = enumerable as string[] ?? enumerable.ToArray();
} }
@ -69,9 +72,7 @@ namespace NadekoBot.Modules.Games
{ {
started = DateTime.Now; started = DateTime.Now;
NadekoBot.Client.MessageReceived += Vote; NadekoBot.Client.MessageReceived += Vote;
var msgToSend = $@"📃**{umsg.Author.Username}** has created a poll which requires your attention: var msgToSend = $"📃**{umsg.Author.Username}** has created a poll which requires your attention:\n\n**{question}**\n";
**{question}**\n";
var num = 1; var num = 1;
msgToSend = answers.Aggregate(msgToSend, (current, answ) => current + $"`{num++}.` **{answ}**\n"); msgToSend = answers.Aggregate(msgToSend, (current, answ) => current + $"`{num++}.` **{answ}**\n");
msgToSend += "\n**Private Message me with the corresponding number of the answer.**"; msgToSend += "\n**Private Message me with the corresponding number of the answer.**";
@ -107,31 +108,30 @@ namespace NadekoBot.Modules.Games
} }
} }
private Task Vote(IMessage imsg) private async Task Vote(IMessage imsg)
{ {
// has to be a user message // has to be a user message
var msg = imsg as IUserMessage; var msg = imsg as IUserMessage;
if (msg == null || msg.Author.IsBot) if (msg == null || msg.Author.IsBot)
return Task.CompletedTask; return;
// channel must be private // channel must be private
IPrivateChannel ch; IPrivateChannel ch;
if ((ch = msg.Channel as IPrivateChannel) == null) if ((ch = msg.Channel as IPrivateChannel) == null)
return Task.CompletedTask; return;
var guildUsers = await guild.GetUsersAsync().ConfigureAwait(false);
if (!guildUsers.Any(u => u.Id == imsg.Author.Id))
return;
// has to be an integer // has to be an integer
int vote; int vote;
if (!int.TryParse(msg.Content, out vote)) return Task.CompletedTask; if (!int.TryParse(msg.Content, out vote)) return;
if (vote < 1 || vote > answers.Length)
var t = Task.Run(async () => return;
if (participants.TryAdd(msg.Author.Id, vote))
{ {
if (vote < 1 || vote > answers.Length) try { await ((IMessageChannel)ch).SendMessageAsync($"Thanks for voting **{msg.Author.Username}**.").ConfigureAwait(false); } catch { }
return; }
if (participants.TryAdd(msg.Author, vote)) return;
{
try { await ((IMessageChannel)ch).SendMessageAsync($"Thanks for voting **{msg.Author.Username}**.").ConfigureAwait(false); } catch { }
}
});
return Task.CompletedTask;
} }
} }
} }

View File

@ -87,11 +87,6 @@ namespace NadekoBot.Modules.Searches
url += $"/{width}/{height}"; url += $"/{width}/{height}";
//using (var http = new HttpClient())
//{
// var res = await http.GetStreamAsync(url).ConfigureAwait(false);
// await channel.SendFileAsync()
//}
await channel.SendMessageAsync(url).ConfigureAwait(false); await channel.SendMessageAsync(url).ConfigureAwait(false);
} }
} }