diff --git a/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs b/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs index 200b7ba5..3b182f23 100644 --- a/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/PlantAndPickCommands.cs @@ -35,6 +35,8 @@ namespace NadekoBot.Modules.Games //channelId/last generation private static ConcurrentDictionary lastGenerations { get; } = new ConcurrentDictionary(); + private static ConcurrentHashSet usersRecentlyPicked { get; } = new ConcurrentHashSet(); + private static float chance { get; } private static int cooldown { get; } private static Logger _log { get; } @@ -101,23 +103,29 @@ namespace NadekoBot.Modules.Games { var channel = (ITextChannel)imsg.Channel; - if (!channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages) + if (!channel.Guild.GetCurrentUser().GetPermissions(channel).ManageMessages || !usersRecentlyPicked.Add(imsg.Author.Id)) + return; + + try { - await channel.SendErrorAsync("I need manage channel permissions in order to process this command.").ConfigureAwait(false); - return; + + List msgs; + + try { await imsg.DeleteAsync().ConfigureAwait(false); } catch { } + if (!plantedFlowers.TryRemove(channel.Id, out msgs)) + return; + + await Task.WhenAll(msgs.Select(toDelete => toDelete.DeleteAsync())).ConfigureAwait(false); + + await CurrencyHandler.AddCurrencyAsync((IGuildUser)imsg.Author, "Picked flower(s).", msgs.Count, false).ConfigureAwait(false); + var msg = await channel.SendConfirmAsync($"**{imsg.Author}** picked {msgs.Count}{Gambling.Gambling.CurrencySign}!").ConfigureAwait(false); + msg.DeleteAfter(10); + } + finally + { + await Task.Delay(60000); + usersRecentlyPicked.TryRemove(imsg.Author.Id); } - - List msgs; - - try { await imsg.DeleteAsync().ConfigureAwait(false); } catch { } - if (!plantedFlowers.TryRemove(channel.Id, out msgs)) - return; - - await Task.WhenAll(msgs.Select(toDelete => toDelete.DeleteAsync())).ConfigureAwait(false); - - await CurrencyHandler.AddCurrencyAsync((IGuildUser)imsg.Author, "Picked flower(s).", msgs.Count, false).ConfigureAwait(false); - var msg = await channel.SendConfirmAsync($"**{imsg.Author}** picked {msgs.Count}{Gambling.Gambling.CurrencySign}!").ConfigureAwait(false); - msg.DeleteAfter(10); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index d4e2eeda..df31964a 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -4767,7 +4767,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Picks the currency planted in this channel.. + /// Looks up a localized string similar to Picks the currency planted in this channel. 60 seconds cooldown.. /// public static string pick_desc { get { diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index 0b6c7881..1b0f35cd 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1363,7 +1363,7 @@ pick - Picks the currency planted in this channel. + Picks the currency planted in this channel. 60 seconds cooldown. `{0}pick` diff --git a/src/NadekoBot/Services/CommandHandler.cs b/src/NadekoBot/Services/CommandHandler.cs index 031a2dfe..49d3d71d 100644 --- a/src/NadekoBot/Services/CommandHandler.cs +++ b/src/NadekoBot/Services/CommandHandler.cs @@ -64,80 +64,14 @@ namespace NadekoBot.Services _client.MessageReceived += MessageReceivedHandler; } - private async Task MessageReceivedHandler(IMessage msg) + private Task MessageReceivedHandler(IMessage msg) { var usrMsg = msg as IUserMessage; if (usrMsg == null) - return; + return Task.CompletedTask; if (usrMsg.Author.IsBot || !NadekoBot.Ready) //no bots - return; - - var guild = (msg.Channel as ITextChannel)?.Guild; - - if (guild != null && guild.OwnerId != usrMsg.Author.Id) - { - if (Permissions.FilterCommands.InviteFilteringChannels.Contains(usrMsg.Channel.Id) || - Permissions.FilterCommands.InviteFilteringServers.Contains(guild.Id)) - { - if (usrMsg.Content.IsDiscordInvite()) - { - try - { - await usrMsg.DeleteAsync().ConfigureAwait(false); - return; - } - catch (HttpException ex) - { - _log.Warn("I do not have permission to filter invites in channel with id " + usrMsg.Channel.Id, ex); - } - } - } - - var filteredWords = Permissions.FilterCommands.FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id).Concat(Permissions.FilterCommands.FilteredWordsForServer(guild.Id)); - var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' '); - if (filteredWords.Any(w => wordsInMessage.Contains(w))) - { - try - { - await usrMsg.DeleteAsync().ConfigureAwait(false); - return; - } - catch (HttpException ex) - { - _log.Warn("I do not have permission to filter words in channel with id " + usrMsg.Channel.Id, ex); - } - } - } - - BlacklistItem blacklistedItem; - if ((blacklistedItem = Permissions.BlacklistCommands.BlacklistedItems.FirstOrDefault(bi => - (bi.Type == BlacklistItem.BlacklistType.Server && bi.ItemId == guild?.Id) || - (bi.Type == BlacklistItem.BlacklistType.Channel && bi.ItemId == msg.Channel.Id) || - (bi.Type == BlacklistItem.BlacklistType.User && bi.ItemId == usrMsg.Author.Id))) != null) - { - return; - } - - try - { - var cleverbotExecuted = await Games.CleverBotCommands.TryAsk(usrMsg); - - if (cleverbotExecuted) - return; - } - catch (Exception ex) { _log.Warn(ex, "Error in cleverbot"); } - - try - { - // maybe this message is a custom reaction - var crExecuted = await CustomReactions.TryExecuteCustomReaction(usrMsg).ConfigureAwait(false); - - //if it was, don't execute the command - if (crExecuted) - return; - } - catch { } + return Task.CompletedTask; var throwaway = Task.Run(async () => { @@ -146,6 +80,72 @@ namespace NadekoBot.Services try { + var guild = (msg.Channel as ITextChannel)?.Guild; + + if (guild != null && guild.OwnerId != usrMsg.Author.Id) + { + if (Permissions.FilterCommands.InviteFilteringChannels.Contains(usrMsg.Channel.Id) || + Permissions.FilterCommands.InviteFilteringServers.Contains(guild.Id)) + { + if (usrMsg.Content.IsDiscordInvite()) + { + try + { + await usrMsg.DeleteAsync().ConfigureAwait(false); + return; + } + catch (HttpException ex) + { + _log.Warn("I do not have permission to filter invites in channel with id " + usrMsg.Channel.Id, ex); + } + } + } + + var filteredWords = Permissions.FilterCommands.FilteredWordsForChannel(usrMsg.Channel.Id, guild.Id).Concat(Permissions.FilterCommands.FilteredWordsForServer(guild.Id)); + var wordsInMessage = usrMsg.Content.ToLowerInvariant().Split(' '); + if (filteredWords.Any(w => wordsInMessage.Contains(w))) + { + try + { + await usrMsg.DeleteAsync().ConfigureAwait(false); + return; + } + catch (HttpException ex) + { + _log.Warn("I do not have permission to filter words in channel with id " + usrMsg.Channel.Id, ex); + } + } + } + + BlacklistItem blacklistedItem; + if ((blacklistedItem = Permissions.BlacklistCommands.BlacklistedItems.FirstOrDefault(bi => + (bi.Type == BlacklistItem.BlacklistType.Server && bi.ItemId == guild?.Id) || + (bi.Type == BlacklistItem.BlacklistType.Channel && bi.ItemId == msg.Channel.Id) || + (bi.Type == BlacklistItem.BlacklistType.User && bi.ItemId == usrMsg.Author.Id))) != null) + { + return; + } + + try + { + var cleverbotExecuted = await Games.CleverBotCommands.TryAsk(usrMsg); + + if (cleverbotExecuted) + return; + } + catch (Exception ex) { _log.Warn(ex, "Error in cleverbot"); } + + try + { + // maybe this message is a custom reaction + var crExecuted = await CustomReactions.TryExecuteCustomReaction(usrMsg).ConfigureAwait(false); + + //if it was, don't execute the command + if (crExecuted) + return; + } + catch { } + var t = await ExecuteCommand(usrMsg, usrMsg.Content, guild, usrMsg.Author, MultiMatchHandling.Best); var command = t.Item1; var permCache = t.Item2; @@ -209,7 +209,8 @@ namespace NadekoBot.Services _log.Warn(ex.InnerException, "Inner Exception of the error in CommandHandler"); } }); - return; + + return Task.CompletedTask; } public async Task> ExecuteCommand(IUserMessage message, string input, IGuild guild, IUser user, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Best) {