예제 #1
0
        private async Task SearchBooruAsync(ABooru booru, string[] tags, BooruType booruId)
        {
            // GetRandomImageAsync crash if we send it something null
            tags ??= new string[0];

            BooruSharp.Search.Post.SearchResult post;
            List <string> newTags = null;

            try
            {
                post = await booru.GetRandomPostAsync(tags);
            }
            catch (InvalidTags)
            {
                // On invalid tags we try to get guess which one the user wanted to use
                newTags = new List <string>();
                foreach (string s in tags)
                {
                    var related = await new Konachan().GetTagsAsync(s); // Konachan have a feature where it can "autocomplete" a tag so we use it to guess what the user meant
                    if (related.Length == 0)
                    {
                        throw new CommandFailed("There is no image with those tags.");
                    }
                    newTags.Add(related.OrderBy(x => GetStringDistance(x.name, s)).First().name);
                }
                try
                {
                    // Once we got our new tags, we try doing a new search with them
                    post = await booru.GetRandomPostAsync(newTags.ToArray());
                }
                catch (InvalidTags)
                {
                    // Might happens if the Konachan tags don't exist in the current booru
                    throw new CommandFailed("There is no image with those tags.");
                }
            }

            int id = int.Parse("" + (int)booruId + post.id);

            StaticObjects.Tags.AddTag(id, booru, post);

            if (post.fileUrl == null)
            {
                throw new CommandFailed("A post was found but no image was available.");
            }
            await ReplyAsync(embed : new EmbedBuilder
            {
                Color    = RatingToColor(post.rating),
                ImageUrl = post.fileUrl.AbsoluteUri,
                Url      = post.postUrl.AbsoluteUri,
                Title    = "From " + Utils.ToWordCase(booru.ToString().Split('.').Last()),
                Footer   = new EmbedFooterBuilder
                {
                    Text = (newTags == null ? "" : "Some of your tags were invalid, the current search was done with: " + string.Join(", ", newTags) + "\n") +
                           "Do the 'Tags' command with then id '" + id + "' to have more information about this image."
                }
            }.Build());
        }
예제 #2
0
        private static async Task PostImage(ABooru booru, IMessageChannel chan, string[] tags, IGuild guild, ulong userId)
        {
            var result = await Features.NSFW.Booru.SearchBooru(chan is ITextChannel? !((ITextChannel)chan).IsNsfw : false, tags, booru, Program.p.rand);

            switch (result.error)
            {
            case Features.NSFW.Error.Booru.ChanNotNSFW:
                await chan.SendMessageAsync(Base.Sentences.ChanIsNotNsfw(guild));

                break;

            case Features.NSFW.Error.Booru.NotFound:
                await chan.SendMessageAsync(Base.Sentences.TagsNotFound(guild, tags));

                break;

            case Features.NSFW.Error.Booru.None:
                IUserMessage msg;
                if (!Utilities.IsImage(result.answer.url))
                {
                    msg = await chan.SendMessageAsync(result.answer.url + Environment.NewLine + "*" + Sentences.ImageInfo(guild, result.answer.saveId) + "*");
                }
                else
                {
                    msg = await chan.SendMessageAsync("", false, new EmbedBuilder()
                    {
                        Color    = result.answer.colorRating,
                        ImageUrl = result.answer.url,
                        Footer   = new EmbedFooterBuilder()
                        {
                            Text = (result.answer.newTags != null ? "Some of your tags were invalid. The current search tags are: " + string.Join(", ", result.answer.newTags) + "\n\n" :
                                    "") + (result.answer.saveId == null ? "" : Sentences.ImageInfo(guild, result.answer.saveId))
                        }
                    }.Build());
                }
                foreach (string t in tags)
                {
                    await Program.p.cm.ProgressAchievementAsync(AchievementID.DoDifferentsBoorus, 1, t.GetHashCode().ToString(), msg, userId);
                }
                if (Program.p.sendStats)
                {
                    await Program.p.UpdateElement(new Tuple <string, string>[] { new Tuple <string, string>("booru", booru.ToString().Split('.').Last().ToLower()) });
                }
                break;

            default:
                throw new NotImplementedException();
            }
        }