Example #1
0
        // if the user supplies a tagname that doesn't exist, search the database to see if there are
        // any tags containing the user-supplied text

        // if there are any results from the database, post a list of the results & add emoji reactions
        // corresponding to each item in the list

        // when the user selects a emoji, it will pass the corresponding tagName and any extra passed
        // parameters to the RetryCommandUsingFoundTag function

        // afterwards, save a pairing of the calling user object and the searchResults message into a
        // dictionary, for use by the RetryCommandUsingFoundTag function
        private async Task FindTagAndRetry(string functionToRetry, params string[] args)
        {
            var tagName = args[0];

            var searchResponse = await DatabaseTags.SearchTagsInDatabase(Context, tagName);

            // if single result, just use that
            if (searchResponse.Count == 1)
            {
                await RetryCommandUsingFoundTag(searchResponse[0], functionToRetry, args);

                return;
            }

            // if the single-result check didn't catch but there are results
            if (searchResponse.Any())
            {
                string[] numbers      = new[] { "0⃣", "1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣" };
                var      numberEmojis = new List <Emoji>();

                EmbedBuilder  embedBuilder  = new EmbedBuilder();
                StringBuilder stringBuilder = new StringBuilder();

                // add the number of emojis we need to the emojis list, and build our string-list of search results
                for (int i = 0; i < searchResponse.Count && i < numbers.Length; i++)
                {
                    numberEmojis.Add(new Emoji(numbers[i]));
                    stringBuilder.AppendLine($"{numbers[i]} - {searchResponse[i]}");
                }

                embedBuilder.WithDescription(stringBuilder.ToString());
                embedBuilder.WithColor(Color.Blue);

                // build a message and add reactions to it
                // reactions will be watched, and the one selected will fire the HandleFindTagReactionResult method, passing
                // that reaction's corresponding tagname and the function passed into this parameter
                var messageContents = new ReactionCallbackData("Did you mean... ", embedBuilder.Build());
                for (int i = 0; i < searchResponse.Count; i++)
                {
                    var counter = i;
                    messageContents.AddCallBack(numberEmojis[counter], (c, r) => RetryCommandUsingFoundTag(searchResponse[counter], functionToRetry, args));
                }

                var message = await InlineReactionReplyAsync(messageContents);

                // add calling user and searchResults embed to a dict as a pair
                // this way we can hold multiple users' reaction messages and operate on them separately
                _dictFindTagUserEmbedPairs.Add(Context.User, message);
            }
            else
            {
                await ReplyAsync("I can't find any tags like what you're looking for.");
            }
        }
Example #2
0
        public async Task TagSearchCommandAsync(string search)
        {
            var results = await DatabaseTags.SearchTagsInDatabase(Context, search);

            if (results != null)
            {
                EmbedBuilder  embedBuilder  = new EmbedBuilder();
                StringBuilder stringBuilder = new StringBuilder();

                foreach (var result in results)
                {
                    stringBuilder.AppendLine(result);
                }

                embedBuilder.AddField("Tags", stringBuilder.ToString(), true);

                await ReplyAsync(null, false, embedBuilder.Build());
            }
            else
            {
                await ReplyAsync("No results found.");
            }
        }