コード例 #1
0
        public async Task <MALAnimeEntry> FirstAnimeAsync(string search)
        {
            var results = await SearchAnimeAsync(search);

            if (results == null)
            {
                return(null);
            }

            MALAnimeEntry entry = results.FirstOrDefault(x =>
                                                         String.Equals(search, x.Title, StringComparison.OrdinalIgnoreCase) ||
                                                         String.Equals(search, x.EnglishTitle, StringComparison.OrdinalIgnoreCase));

            if (entry == null)
            {
                entry = results.OrderBy(x => x.Title.Length)
                        .FirstOrDefault(x => x.Title.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            if (entry == null)
            {
                entry = results.OrderBy(x => x.EnglishTitle.Length)
                        .FirstOrDefault(x => x.EnglishTitle.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            if (entry == null)
            {
                entry = results.First();
            }

            return(entry);
        }
コード例 #2
0
        public override async Task InvokeAsync(CommandContext ctx, string[] args)
        {
            if (args.Length < 1)
            {
                await ctx.ReplyAsync(Util.GenerateInvalidUsage(ctx.Bot, this));

                return;
            }

            string animeName = String.Join(' ', args);

            if (animeName.Length < 4)
            {
                await ctx.ReplyAsync("Please have your search query be more than four characters long.");

                return;
            }

            MalClient m = new MalClient(ctx.Bot.Configuration.MALUsername, ctx.Bot.Configuration.MALPassword);

            MALAnimeEntry anime = await m.FirstAnimeAsync(animeName);

            if (anime == null)
            {
                await ctx.ReplyAsync("We couldn't find that anime in the MAL database. Try an alternate name.");

                return;
            }

            var client = new TwistClient(ctx.DbContext);

            TwistEntry entry = await client.GetEntryAsync(anime.Id) ?? await client.GetEntryAsync(anime.Title);

            EmbedBuilder embed;

            if (entry == null)
            {
                embed = new EmbedBuilder
                {
                    Title        = "Anime Not Found",
                    Color        = Util.CyanColor,
                    ThumbnailUrl = anime.Image,
                    Description  = $"*{anime.Title}* wasn't found on twist.moe. Some possible alternate sources are listed below for your convenience."
                };

                embed.AddField("Alternate Sources", $"[Nyaa](https://nyaa.pantsu.cat/search?c=_&userID=0&q={WebUtility.UrlEncode(anime.Title)})\n[KissAnime](http://kissanime.ru/Search/Anime?keyword={WebUtility.UrlEncode(anime.Title)})");

                await ctx.ReplyAsync(embed.Build());

                return;
            }

            string desc = Util.BBCodeToMarkdown(HttpUtility.HtmlDecode(Regex.Replace(anime.Synopsis, @"<(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>", "")));

            embed = new EmbedBuilder
            {
                Title        = entry.Url,
                Url          = entry.Url,
                ThumbnailUrl = anime.Image,
                Color        = Util.CyanColor,
                Description  = $"A stream was located for *{anime.Title}* on twist.moe. Click the link above to continue.\n\n[MAL Entry](https://myanimelist.net/anime/{anime.Id}/)"
            };
            embed.AddField("Description", desc.Length > 1024 ? desc.Substring(0, 1021) + "..." : desc);
            embed.WithFooter("https://twist.moe", "https://twist.moe/public/icons/fav_x16.png");

            await ctx.ReplyAsync(embed.Build());
        }