From e27e1005eb3443549663cfaed89b69ef2ed35ac1 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 24 Jun 2017 07:23:59 +0200 Subject: [PATCH] poll is now public poll, private poll removed --- .../Modules/Games/Commands/PollCommands.cs | 19 ++---- src/NadekoBot/NadekoBot.cs | 20 +++++- .../Services/Administration/SelfService.cs | 64 +++++++++---------- src/NadekoBot/Services/Games/Poll.cs | 46 +++---------- src/NadekoBot/Services/Games/PollService.cs | 14 +--- .../Services/Utility/ConverterService.cs | 1 - .../Services/Utility/PatreonRewardsService.cs | 4 -- src/NadekoBot/ShardsCoordinator.cs | 2 +- 8 files changed, 64 insertions(+), 106 deletions(-) diff --git a/src/NadekoBot/Modules/Games/Commands/PollCommands.cs b/src/NadekoBot/Modules/Games/Commands/PollCommands.cs index 27dbb03c..0d6b6cb2 100644 --- a/src/NadekoBot/Modules/Games/Commands/PollCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/PollCommands.cs @@ -26,13 +26,7 @@ namespace NadekoBot.Modules.Games [RequireUserPermission(GuildPermission.ManageMessages)] [RequireContext(ContextType.Guild)] public Task Poll([Remainder] string arg = null) - => InternalStartPoll(arg, false); - - [NadekoCommand, Usage, Description, Aliases] - [RequireUserPermission(GuildPermission.ManageMessages)] - [RequireContext(ContextType.Guild)] - public Task PublicPoll([Remainder] string arg = null) - => InternalStartPoll(arg, true); + => InternalStartPoll(arg); [NadekoCommand, Usage, Description, Aliases] [RequireUserPermission(GuildPermission.ManageMessages)] @@ -44,15 +38,10 @@ namespace NadekoBot.Modules.Games await Context.Channel.EmbedAsync(poll.GetStats(GetText("current_poll_results"))); } - //todo enable private polls, or completely remove them - private async Task InternalStartPoll(string arg, bool isPublic = false) + + private async Task InternalStartPoll(string arg) { - if (isPublic == false) - { - await ReplyErrorLocalized($"Temporarily disabled. Use `{Prefix}ppoll`"); - return; - } - if(await _polls.StartPoll((ITextChannel)Context.Channel, Context.Message, arg, isPublic) == false) + if(await _polls.StartPoll((ITextChannel)Context.Channel, Context.Message, arg) == false) await ReplyErrorLocalized("poll_already_running").ConfigureAwait(false); } diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 26cf6d91..24033c1f 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -290,9 +290,23 @@ namespace NadekoBot Task SetClientReady() { - var _ = Task.Run(() => + var _ = Task.Run(async () => { clientReady.TrySetResult(true); + try + { + await Task.WhenAll((await Client.GetDMChannelsAsync()) + .Select(x => x.CloseAsync())) + .ConfigureAwait(false); + } + catch + { + // ignored + } + finally + { + + } }); return Task.CompletedTask; } @@ -304,8 +318,8 @@ namespace NadekoBot try { - Client.LoginAsync(TokenType.Bot, token).GetAwaiter().GetResult(); - Client.StartAsync().GetAwaiter().GetResult(); + await Client.LoginAsync(TokenType.Bot, token).ConfigureAwait(false); + await Client.StartAsync().ConfigureAwait(false); Client.Ready += SetClientReady; await clientReady.Task.ConfigureAwait(false); Client.Ready -= SetClientReady; diff --git a/src/NadekoBot/Services/Administration/SelfService.cs b/src/NadekoBot/Services/Administration/SelfService.cs index 336f9970..2a12ffca 100644 --- a/src/NadekoBot/Services/Administration/SelfService.cs +++ b/src/NadekoBot/Services/Administration/SelfService.cs @@ -63,44 +63,44 @@ namespace NadekoBot.Services.Administration _client.Guilds.SelectMany(g => g.Users); - //todo load owner channels - //LoadOwnerChannels(); + if(client.ShardId == 0) + LoadOwnerChannels(); }); } - //private void LoadOwnerChannels() - //{ - // var hs = new HashSet(_creds.OwnerIds); - // var channels = new Dictionary>(); + private void LoadOwnerChannels() + { + var hs = new HashSet(_creds.OwnerIds); + var channels = new Dictionary>(); - // if (hs.Count > 0) - // { - // foreach (var g in _client.Guilds) - // { - // if (hs.Count == 0) - // break; + if (hs.Count > 0) + { + foreach (var g in _client.Guilds) + { + if (hs.Count == 0) + break; - // foreach (var u in g.Users) - // { - // if (hs.Remove(u.Id)) - // { - // channels.Add(u.Id, new AsyncLazy(async () => await u.CreateDMChannelAsync())); - // if (hs.Count == 0) - // break; - // } - // } - // } - // } - - // ownerChannels = channels.OrderBy(x => _creds.OwnerIds.IndexOf(x.Key)) - // .Select(x => x.Value) - // .ToImmutableArray(); + foreach (var u in g.Users) + { + if (hs.Remove(u.Id)) + { + channels.Add(u.Id, new AsyncLazy(async () => await u.CreateDMChannelAsync())); + if (hs.Count == 0) + break; + } + } + } + } - // if (!ownerChannels.Any()) - // _log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file."); - // else - // _log.Info($"Created {ownerChannels.Length} out of {_creds.OwnerIds.Length} owner message channels."); - //} + ownerChannels = channels.OrderBy(x => _creds.OwnerIds.IndexOf(x.Key)) + .Select(x => x.Value) + .ToImmutableArray(); + + if (!ownerChannels.Any()) + _log.Warn("No owner channels created! Make sure you've specified correct OwnerId in the credentials.json file."); + else + _log.Info($"Created {ownerChannels.Length} out of {_creds.OwnerIds.Length} owner message channels."); + } // forwards dms public async Task LateExecute(DiscordSocketClient client, IGuild guild, IUserMessage msg) diff --git a/src/NadekoBot/Services/Games/Poll.cs b/src/NadekoBot/Services/Games/Poll.cs index 1a6bbf20..5358e139 100644 --- a/src/NadekoBot/Services/Games/Poll.cs +++ b/src/NadekoBot/Services/Games/Poll.cs @@ -21,13 +21,10 @@ namespace NadekoBot.Services.Games private readonly DiscordSocketClient _client; private readonly NadekoStrings _strings; private bool running = false; - private HashSet _guildUsers; public event Action OnEnded = delegate { }; - public bool IsPublic { get; } - - public Poll(DiscordSocketClient client, NadekoStrings strings, IUserMessage umsg, string question, IEnumerable enumerable, bool isPublic = false) + public Poll(DiscordSocketClient client, NadekoStrings strings, IUserMessage umsg, string question, IEnumerable enumerable) { _client = client; _strings = strings; @@ -36,7 +33,6 @@ namespace NadekoBot.Services.Games _guild = ((ITextChannel)umsg.Channel).Guild; _question = question; answers = enumerable as string[] ?? enumerable.ToArray(); - IsPublic = isPublic; } public EmbedBuilder GetStats(string title) @@ -82,13 +78,7 @@ namespace NadekoBot.Services.Games var msgToSend = GetText("poll_created", Format.Bold(_originalMessage.Author.Username)) + "\n\n" + Format.Bold(_question) + "\n"; var num = 1; msgToSend = answers.Aggregate(msgToSend, (current, answ) => current + $"`{num++}.` **{answ}**\n"); - if (!IsPublic) - msgToSend += "\n" + Format.Bold(GetText("poll_vote_private")); - else - msgToSend += "\n" + Format.Bold(GetText("poll_vote_public")); - - if (!IsPublic) - _guildUsers = new HashSet((await _guild.GetUsersAsync().ConfigureAwait(false)).Select(x => x.Id)); + msgToSend += "\n" + Format.Bold(GetText("poll_vote_public")); await _originalMessage.Channel.SendConfirmAsync(msgToSend).ConfigureAwait(false); running = true; @@ -114,36 +104,16 @@ namespace NadekoBot.Services.Games return false; IMessageChannel ch; - if (IsPublic) - { - //if public, channel must be the same the poll started in - if (_originalMessage.Channel.Id != msg.Channel.Id) - return false; - ch = msg.Channel; - } - else - { - //if private, channel must be dm channel - if ((ch = msg.Channel as IDMChannel) == null) - return false; - - // user must be a member of the guild this poll is in - if (!_guildUsers.Contains(msg.Author.Id)) - return false; - } + //if public, channel must be the same the poll started in + if (_originalMessage.Channel.Id != msg.Channel.Id) + return false; + ch = msg.Channel; //user can vote only once if (_participants.TryAdd(msg.Author.Id, vote)) { - if (!IsPublic) - { - await ch.SendConfirmAsync(GetText("thanks_for_voting", Format.Bold(msg.Author.Username))).ConfigureAwait(false); - } - else - { - var toDelete = await ch.SendConfirmAsync(GetText("poll_voted", Format.Bold(msg.Author.ToString()))).ConfigureAwait(false); - toDelete.DeleteAfter(5); - } + var toDelete = await ch.SendConfirmAsync(GetText("poll_voted", Format.Bold(msg.Author.ToString()))).ConfigureAwait(false); + toDelete.DeleteAfter(5); return true; } return false; diff --git a/src/NadekoBot/Services/Games/PollService.cs b/src/NadekoBot/Services/Games/PollService.cs index ce8852a7..ba6fee19 100644 --- a/src/NadekoBot/Services/Games/PollService.cs +++ b/src/NadekoBot/Services/Games/PollService.cs @@ -23,7 +23,7 @@ namespace NadekoBot.Services.Games _strings = strings; } - public async Task StartPoll(ITextChannel channel, IUserMessage msg, string arg, bool isPublic = false) + public async Task StartPoll(ITextChannel channel, IUserMessage msg, string arg) { if (string.IsNullOrWhiteSpace(arg) || !arg.Contains(";")) return null; @@ -31,7 +31,7 @@ namespace NadekoBot.Services.Games if (data.Length < 3) return null; - var poll = new Poll(_client, _strings, msg, data[0], data.Skip(1), isPublic: isPublic); + var poll = new Poll(_client, _strings, msg, data[0], data.Skip(1)); if (ActivePolls.TryAdd(channel.Guild.Id, poll)) { poll.OnEnded += (gid) => @@ -48,17 +48,7 @@ namespace NadekoBot.Services.Games public async Task TryExecuteEarly(DiscordSocketClient client, IGuild guild, IUserMessage msg) { if (guild == null) - { - foreach (var kvp in ActivePolls) - { - if (!kvp.Value.IsPublic) - { - if (await kvp.Value.TryVote(msg).ConfigureAwait(false)) - return true; - } - } return false; - } if (!ActivePolls.TryGetValue(guild.Id, out var poll)) return false; diff --git a/src/NadekoBot/Services/Utility/ConverterService.cs b/src/NadekoBot/Services/Utility/ConverterService.cs index 1edb470a..40f7fafa 100644 --- a/src/NadekoBot/Services/Utility/ConverterService.cs +++ b/src/NadekoBot/Services/Utility/ConverterService.cs @@ -18,7 +18,6 @@ namespace NadekoBot.Services.Utility public List Units { get; } = new List(); private readonly Logger _log; private readonly Timer _currencyUpdater; - private readonly Timer _currencyLoader; private readonly TimeSpan _updateInterval = new TimeSpan(12, 0, 0); private readonly DbService _db; diff --git a/src/NadekoBot/Services/Utility/PatreonRewardsService.cs b/src/NadekoBot/Services/Utility/PatreonRewardsService.cs index 8b463f7e..3eef3d72 100644 --- a/src/NadekoBot/Services/Utility/PatreonRewardsService.cs +++ b/src/NadekoBot/Services/Utility/PatreonRewardsService.cs @@ -15,7 +15,6 @@ using System.Threading.Tasks; namespace NadekoBot.Services.Utility { - //todo periodically load from the database, update only on shard 0 public class PatreonRewardsService { private readonly SemaphoreSlim getPledgesLocker = new SemaphoreSlim(1, 1); @@ -96,15 +95,12 @@ namespace NadekoBot.Services.Utility { getPledgesLocker.Release(); } - Console.WriteLine("Pledges loaded from the website"); } else { if(File.Exists(cacheFileName)) Pledges = JsonConvert.DeserializeObject(File.ReadAllText("./patreon_rewards.json")) .ToImmutableArray(); - - Console.WriteLine("Pledges loaded from the file"); } } diff --git a/src/NadekoBot/ShardsCoordinator.cs b/src/NadekoBot/ShardsCoordinator.cs index 3fe6da58..fcf80ebe 100644 --- a/src/NadekoBot/ShardsCoordinator.cs +++ b/src/NadekoBot/ShardsCoordinator.cs @@ -53,7 +53,7 @@ namespace NadekoBot FileName = Credentials.ShardRunCommand, Arguments = string.Format(Credentials.ShardRunArguments, i, curProcessId) }); - await Task.Delay(6500); + await Task.Delay(5000); } }