public async Task <bool> Ask <T>(string question, T defaultValue, Action <T> apply, TryGetValue <T> getValue)
        {
            if (defaultValue != null)
            {
                question += $" (Default {defaultValue})";
            }

            await ReplyAsync(question);

            SocketMessage response;
            T             result;

            do
            {
                response = await Interactive.NextMessageAsync(Context, true, true);

                if (defaultValue != null && response.Content.Length == 1 && response.Content[0] == SkipCharacter)
                {
                    return(false);
                }
            }while (!getValue(response, out result));

            apply(result);

            return(true);
        }
Пример #2
0
        public async Task SendResponseMessage(string id, [Remainder] string messageContents)
        {
            var supportMessage = await DatabaseSupport.GetSupportMessage(id);

            if (supportMessage == null)
            {
                await ReplyAsync("That message ID doesn't exist.");

                return;
            }

            var supportMsgChannel = DiscordSocketClient.GetChannel(ulong.Parse(supportMessage.ChannelID)) as ISocketMessageChannel;
            var supportMsgAuthor  = DiscordSocketClient.GetUser(ulong.Parse(supportMessage.AuthorID));

            await supportMsgChannel.SendMessageAsync($"{supportMsgAuthor.Mention} {messageContents}");

            await ReplyAsync("Do you want to keep the support message for later?");

            var response = Interactive.NextMessageAsync(Context, true, true, TimeSpan.FromSeconds(10)).Result.Content;

            if (response.Contains("yes") || response.Contains("y") || response.Contains("yea") ||
                response.Contains("yeah"))
            {
                return;
            }

            await DatabaseSupport.RemoveSupportMessage(id);

            await ReplyAsync("Support message cleared.");
        }
Пример #3
0
            public async Task <string> AsyncAsk(string question)
            {
                await ReplyMentionAsync(question);

                SocketMessage message = await Interactive.NextMessageAsync(Context, true, true, TimeSpan.FromMinutes(1));

                return(message.Content);
            }
Пример #4
0
        public async Task DefaultChat()
        {
            var commandPrefix = await _command.GetPrefix(Context.Guild.Id);

            ChatSession session;

            try
            {
                session = await _chat.CreateSession(Context.User as SocketGuildUser, Context.Channel);
            }
            catch (SessionExistsException e)
            {
                await ReplyAsync(_localization.GetMessage("Chat error exists", commandPrefix));

                await _logs.Write("Chat", e, Context.Guild);

                return;
            }

            await ReplyAsync(_localization.GetMessage("Chat start"));

            while (!session.HasEnded)
            {
                var userMessage = await Interactive.NextMessageAsync(Context, timeout : TimeSpan.FromMinutes(1));

                if (userMessage == null)
                {
                    if (session.HasEnded)
                    {
                        break;
                    }
                    await ReplyAsync(_localization.GetMessage("Chat end timeout"));

                    return;
                }

                if (userMessage.Content.StartsWith(commandPrefix, StringComparison.OrdinalIgnoreCase) || userMessage.Content.StartsWith(Context.Client.CurrentUser.Mention, StringComparison.OrdinalIgnoreCase))
                {
                    if (userMessage.Content.EndsWith("chat stop", StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                    continue;
                }

                var response = await session.GetResponse(userMessage.Content);
                await ReplyAsync(response);
            }

            await ReplyAsync(_localization.GetMessage("Chat end"));
        }
Пример #5
0
        public async Task <bool> HandleCallbackAsync(SocketReaction reaction)
        {
            var emote = reaction.Emote;

            if (emote.Equals(Options.First))
            {
                _page = 1;
            }
            else if (emote.Equals(Options.Next))
            {
                if (_page >= _pages)
                {
                    return(false);
                }
                ++_page;
            }
            else if (emote.Equals(Options.Back))
            {
                if (_page <= 1)
                {
                    return(false);
                }
                --_page;
            }
            else if (emote.Equals(Options.Last))
            {
                _page = _pages;
            }
            else if (emote.Equals(Options.Stop))
            {
                await Message.DeleteAsync().ConfigureAwait(false);

                return(true);
            }
            else if (emote.Equals(Options.Jump))
            {
                _ = Task.Run(async() =>
                {
                    var criteria = new Criteria <SocketMessage>()
                                   .AddCriterion(new EnsureSourceChannelCriterion())
                                   .AddCriterion(new EnsureFromUserCriterion(reaction.UserId))
                                   .AddCriterion(new EnsureIsIntegerCriterion());
                    var response = await Interactive.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(15));
                    var request  = int.Parse(response.Content);
                    if (request < 1 || request > _pages)
                    {
                        _ = response.DeleteAsync().ConfigureAwait(false);
                        await Interactive.ReplyAndDeleteAsync(Context, Options.Stop.Name);
                        return;
                    }
                    _page = request;
                    _     = response.DeleteAsync().ConfigureAwait(false);
                    await RenderAsync().ConfigureAwait(false);
                });
            }
            else if (emote.Equals(Options.Info))
            {
                await Interactive.ReplyAndDeleteAsync(Context, Options.InformationText, timeout : Options.InfoTimeout);

                return(false);
            }
            _ = Message.RemoveReactionAsync(reaction.Emote, reaction.User.Value);
            await RenderAsync().ConfigureAwait(false);

            return(false);
        }
Пример #6
0
        public async Task <bool> HandleCallbackAsync(SocketInteraction interaction)
        {
            var msgComponent = interaction as SocketMessageComponent;

            switch (msgComponent.Data.CustomId)
            {
            case FirstId:
                _page = 1;
                break;

            case NextId:
            {
                if (_page >= _pages)
                {
                    return(false);
                }

                ++_page;
                break;
            }

            case PreviousId:
            {
                if (_page <= 1)
                {
                    return(false);
                }

                --_page;
                break;
            }

            case LastId:
                _page = _pages;
                break;

            case StopId:
                await Message.DeleteAsync().ConfigureAwait(false);

                return(true);

            case JumpId:
            {
                _ = Task.Run(async() =>
                    {
                        var criteria = new Criteria <SocketMessage>()
                                       .AddCriterion(new EnsureSourceChannelCriterion())
                                       .AddCriterion(new EnsureFromUserCriterion(interaction.User.Id))
                                       .AddCriterion(new EnsureIsIntegerCriterion());

                        var response = await Interactive.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(15));
                        var request  = int.Parse(response.Content);
                        if (request < 1 || request > _pages)
                        {
                            _ = response.DeleteAsync().ConfigureAwait(false);
                            await Interactive.ReplyAndDeleteAsync(Context, Options.Stop.Name);
                            return;
                        }
                        _page = request;
                        _     = response.DeleteAsync().ConfigureAwait(false);
                        await RenderAsync(msgComponent).ConfigureAwait(false);
                    });

                break;
            }

            case InfoId:
                await Interactive.ReplyAndDeleteAsync(Context, Options.InformationText, timeout : Options.InfoTimeout);

                return(false);
            }

            await RenderAsync(msgComponent).ConfigureAwait(false);

            return(false);
        }
        /// <summary>
        /// Handles the reaction callback.
        /// </summary>
        /// <param name="reaction">
        /// The reaction.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <bool> HandleCallbackAsync(SocketReaction reaction)
        {
            //bool render = true;
            var emote = reaction.Emote;

            if (emote.Equals(Options.First))
            {
                if (page == 1)
                {
                    return(false);
                }
                page = 1;
            }
            else if (emote.Equals(Options.Next))
            {
                if (page >= pages)
                {
                    return(false);
                }
                ++page;
            }
            else if (emote.Equals(Options.Back))
            {
                if (page <= 1)
                {
                    return(false);
                }
                --page;
            }
            else if (emote.Equals(Options.Last))
            {
                if (page == pages)
                {
                    return(false);
                }
                page = pages;
            }
            else if (emote.Equals(Options.Stop))
            {
                _ = OnStopAsync(Message, Options.ActionOnStop);
                return(false);
            }
            else if (emote.Equals(Options.Jump))
            {
                //render = false;
                _ = Task.Run(async() =>
                {
                    var criteria = new Criteria <SocketMessage>()
                                   .AddCriterion(new EnsureSourceChannelCriterion())
                                   .AddCriterion(new EnsureFromUserCriterion(reaction.UserId))
                                   .AddCriterion(new EnsureIsIntegerCriterion());
                    var response = await Interactive.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(15));

                    if (response == null || !int.TryParse(response.Content, out int requestedPage))
                    {
                        return;
                    }

                    _ = response.DeleteAsync().ConfigureAwait(false);
                    if (requestedPage < 1 || requestedPage == page || requestedPage > pages)
                    {
                        //_ = response.DeleteAsync().ConfigureAwait(false);
                        //await Interactive.ReplyAndDeleteAsync(Context, Options.Stop.Name);
                        return;
                    }

                    page = requestedPage;

                    _ = Message.RemoveReactionAsync(reaction.Emote, reaction.User.Value);

                    await RenderAsync().ConfigureAwait(false);
                });
                return(false);
            }
            else if (emote.Equals(Options.Info))
            {
                if (ShowingInfo.Contains(Message.Id))
                {
                    return(false);
                }
                var msg = await Context.Channel.SendMessageAsync(Options.InformationText);

                ShowingInfo.Add(Message.Id);
                _ = Task.Delay(Options.InfoTimeout)
                    .ContinueWith(_ =>
                {
                    _ = msg.DeleteAsync();
                    ShowingInfo.Remove(Message.Id);
                }).ConfigureAwait(false);
                //await Interactive.ReplyAndDeleteAsync(Context, Options.InformationText, timeout: Options.InfoTimeout);
                return(false);
            }
            else
            {
                return(false);
            }

            _ = Message.RemoveReactionAsync(reaction.Emote, reaction.User.Value);

            await RenderAsync().ConfigureAwait(false);

            return(false);
        }
        public async Task <bool> HandleCallbackAsync(ReactionAddedEventArgs e)
        {
            var emote = e.Emoji;

            if (emote.Equals(_options.First))
            {
                page = 1;
            }
            else if (emote.Equals(_options.Next))
            {
                if (page >= pages)
                {
                    return(false);
                }
                ++page;
            }
            else if (emote.Equals(_options.Back))
            {
                if (page <= 1)
                {
                    return(false);
                }
                --page;
            }
            else if (emote.Equals(_options.Last))
            {
                page = pages;
            }
            else if (emote.Equals(_options.Stop))
            {
                await Message.DeleteAsync().ConfigureAwait(false);

                return(true);
            }
            else if (emote.Equals(_options.Jump))
            {
                _ = Task.Run(async() =>
                {
                    var criteria = new Criteria <CachedUserMessage>()
                                   .AddCriterion(new EnsureSourceChannelCriterion())
                                   .AddCriterion(new EnsureFromUserCriterion(e.User.Id))
                                   .AddCriterion(new EnsureIsIntegerCriterion());
                    var response = await Interactive.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(15));
                    var request  = int.Parse(response.Content);
                    if (request < 1 || request > pages)
                    {
                        _ = response.DeleteAsync().ConfigureAwait(false);
                        await Interactive.ReplyAndDeleteAsync(Context, _options.Stop.Name);
                        return;
                    }
                    page = request;
                    _    = response.DeleteAsync().ConfigureAwait(false);
                    await RenderAsync().ConfigureAwait(false);
                });
            }
            else if (emote.Equals(_options.Info))
            {
                await Interactive.ReplyAndDeleteAsync(Context, _options.InformationText, timeout : _options.InfoTimeout);

                return(false);
            }
            await Message.RemoveMemberReactionAsync(e.User.Id, emote);

            await RenderAsync().ConfigureAwait(false);

            return(false);
        }
        /// <summary>
        /// Handles the reaction callback.
        /// </summary>
        /// <param name="reaction">
        /// The reaction.
        /// </param>
        /// <returns>
        /// A task representing the asynchronous operation.
        /// </returns>
        public async Task <bool> HandleCallbackAsync(SocketReaction reaction)
        {
            var emote = reaction.Emote;

            if (emote.Equals(_pager.Options.First))
            {
                if (_page == 1)
                {
                    return(false);
                }
                _page = 1;
            }
            else if (emote.Equals(_pager.Options.Next))
            {
                if (_page >= _pages)
                {
                    return(false);
                }
                ++_page;
            }
            else if (emote.Equals(_pager.Options.Back))
            {
                if (_page <= 1)
                {
                    return(false);
                }
                --_page;
            }
            else if (emote.Equals(_pager.Options.Last))
            {
                if (_page == _pages)
                {
                    return(false);
                }
                _page = _pages;
            }
            else if (emote.Equals(_pager.Options.Stop))
            {
                _ = OnStopAsync(Message, _pager.Options.ActionOnStop).ConfigureAwait(false);
                return(false);
            }
            else if (emote.Equals(_pager.Options.Jump))
            {
                _ = Task.Run(async() =>
                {
                    var criteria = new Criteria <SocketMessage>()
                                   .AddCriterion(new EnsureSourceChannelCriterion())
                                   .AddCriterion(new EnsureFromUserCriterion(reaction.UserId))
                                   .AddCriterion(new EnsureIsIntegerCriterion());

                    var response = await Interactive.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(15)).ConfigureAwait(false);

                    if (response == null || !int.TryParse(response.Content, out int requestedPage))
                    {
                        return;
                    }

                    _ = response.DeleteAsync().ConfigureAwait(false);

                    if (requestedPage < 1 || requestedPage == _page || requestedPage > _pages)
                    {
                        return;
                    }

                    _page = requestedPage;

                    _ = Message.RemoveReactionAsync(reaction.Emote, reaction.UserId).ConfigureAwait(false);

                    await RenderAsync().ConfigureAwait(false);
                });
                return(false);
            }
            else if (emote.Equals(_pager.Options.Info))
            {
                lock (_showingInfo)
                {
                    if (_showingInfo.Contains(Message.Id))
                    {
                        return(false);
                    }
                }

                _ = Task.Run(async() =>
                {
                    var msg = await Context.Channel.SendMessageAsync(_pager.Options.InformationText).ConfigureAwait(false);
                    lock (_showingInfo)
                    {
                        _showingInfo.Add(Message.Id);
                    }
                    await Task.Delay(_pager.Options.InfoTimeout).ConfigureAwait(false);
                    await msg.DeleteAsync().ConfigureAwait(false);
                    lock (_showingInfo)
                    {
                        _showingInfo.Remove(Message.Id);
                    }
                });

                return(false);
            }
            else
            {
                return(false);
            }

            _ = Message.RemoveReactionAsync(reaction.Emote, reaction.UserId).ConfigureAwait(false);

            await RenderAsync().ConfigureAwait(false);

            return(false);
        }
Пример #10
0
        public async Task SetupMessage()
        {
            PostedMessages = new Collection <IMessage>();
            Interactive.ClearReactionCallbacks();
            if (this.beforeTextPosted != null)
            {
                this.beforeTextPosted(this);
            }
            if (additionalPosts.Count > 0)
            {
                var additionalPostsString = new StringBuilder();
                foreach (var text in additionalPosts)
                {
                    additionalPostsString.Append(text + Environment.NewLine);
                }
                var value = additionalPostsString.ToString();
                if (value != string.Empty)
                {
                    var message = await Context.Channel.SendMessageAsync(value);

                    PostedMessages.Add(message);
                }
            }

            this.Message = await Context.Channel.SendMessageAsync(description);

            PostedMessages.Add(this.Message);
            if (type == StepType.Reaction)
            {
                var emotes = new Collection <IEmote>();
                foreach (var action in Actions)
                {
                    emotes.Add(action.Emote);
                }
                await Message.AddReactionsAsync(emotes.ToArray());

                Interactive.AddReactionCallback(this.Message, this);
            }
            else
            {
                var response = await Interactive.NextMessageAsync(Context, true, true, TimeSpan.FromMinutes(2));

                Result = response.Content;
                await response.DeleteAsync();
                await RemoveMessagesOnNextProgression();

                reactBasedOnResult();
            }

            if (Timeout.HasValue && Timeout.Value != null)
            {
                _ = Task.Delay(Timeout.Value).ContinueWith(async _ =>
                {
                    if (Message != null)
                    {
                        Interactive.RemoveReactionCallback(Message);
                    }

                    foreach (var message in PostedMessages)
                    {
                        await message.DeleteAsync();
                    }
                });
            }
        }
Пример #11
0
        public async ValueTask <bool> HandleAsync(SocketReaction reaction)
        {
            var emote = reaction.Emote;

            if (emote.Equals(_pager.Options.First))
            {
                _currentPageIndex = 1;
            }
            else if (emote.Equals(_pager.Options.Next))
            {
                if (_currentPageIndex >= _pageCount)
                {
                    return(false);
                }
                _currentPageIndex++;
            }
            else if (emote.Equals(_pager.Options.Back))
            {
                if (_currentPageIndex <= 1)
                {
                    return(false);
                }
                _currentPageIndex--;
            }
            else if (emote.Equals(_pager.Options.Last))
            {
                _currentPageIndex = _pageCount;
            }
            else if (emote.Equals(_pager.Options.Stop))
            {
                return(await Context.Message.TryDeleteAsync() && await Message.TryDeleteAsync());
            }
            else if (emote.Equals(_pager.Options.Jump))
            {
                _ = Executor.ExecuteAsync(async() =>
                {
                    var response = await Interactive.NextMessageAsync(Context, new Criteria <SocketUserMessage>()
                                                                      .AddCriterion(new EnsureSourceChannelCriterion())
                                                                      .AddCriterion(new EnsureFromUserCriterion(reaction.UserId))
                                                                      .AddCriterion((__, msg) => new ValueTask <bool>(int.TryParse(msg.Content, out _))), 15.Seconds());
                    var req = int.Parse(response.Content);

                    if (req < 1 || req > _pageCount)
                    {
                        _ = response.TryDeleteAsync();
                        await Interactive.ReplyAndDeleteAsync(Context, _pager.Options.Stop.Name, timeout: 3.Seconds());
                        return;
                    }

                    _currentPageIndex = req;
                    _ = response.TryDeleteAsync();
                    await RenderAsync();
                });
            }
            else if (emote.Equals(_pager.Options.Info))
            {
                await Interactive.ReplyAndDeleteAsync(Context, _pager.Options.InformationText, timeout : _pager.Options.InfoTimeout);

                return(false);
            }
            else if (emote.Name.Equals(DiscordHelper.OctagonalSign))
            {
                await DisposeAsync();

                return(true);
            }

            await Message.RemoveReactionAsync(reaction.Emote, reaction.User.Value);

            await RenderAsync();

            return(false);
        }
Пример #12
0
        public async Task <bool> HandleCallbackAsync(SocketReaction reaction)
        {
            var emote = reaction.Emote;

            if (emote.Equals(options.First))
            {
                page = 1;
            }
            else if (emote.Equals(options.Next))
            {
                if (page >= pages)
                {
                    return(false);
                }
                ++page;
            }
            else if (emote.Equals(options.Back))
            {
                if (page <= 1)
                {
                    return(false);
                }
                --page;
            }
            else if (emote.Equals(options.Last))
            {
                page = pages;
            }
            else if (emote.Equals(options.Stop))
            {
                await Message.DeleteAsync().ConfigureAwait(false);

                return(true);
            }
            else if (emote.Equals(options.Jump))
            {
                _ = Task.Run(async() =>
                {
                    var msg      = await Context.Channel.SendMessageAsync("Which page do you wish to jump to? <:KannaHype:571690048001671238>");
                    var criteria = new Criteria <SocketMessage>()
                                   .AddCriterion(new EnsureSourceChannelCriterion())
                                   .AddCriterion(new EnsureFromUserCriterion(reaction.UserId))
                                   .AddCriterion(new EnsureIsIntegerCriterion())
                                   .AddCriterion(new EnsureRangeCriterion(pages));
                    var response = await Interactive.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(60));
                    var request  = int.Parse(response.Content);
                    page         = request;
                    await RenderAsync().ConfigureAwait(false);
                    _ = response.DeleteAsync().ConfigureAwait(false);
                    await msg.DeleteAsync();
                });
                return(false);
            }
            else if (emote.Equals(options.Info))
            {
                await Interactive.ReplyAndDeleteAsync(Context, options.InformationText, timeout : options.InfoTimeout);

                return(false);
            }
            _ = Message.RemoveReactionAsync(reaction.Emote, reaction.User.Value);
            await RenderAsync().ConfigureAwait(false);

            return(false);
        }