Exemple #1
0
        public Task GetAsync(string url,
                             CancellationToken cancellationToken = default)
        {
            var(source, id) = GalleryUtility.Parse(url);

            return(GetAsync(source, id, cancellationToken));
        }
Exemple #2
0
        public Task RemoveAsync(string name,
                                string url,
                                CancellationToken cancellationToken = default)
        {
            var(source, id) = GalleryUtility.Parse(url);

            return(RemoveAsync(name, source, id, cancellationToken));
        }
Exemple #3
0
        public async Task AddAsync(string name,
                                   string source,
                                   string id,
                                   CancellationToken cancellationToken = default)
        {
            name = FixCollectionName(name);

            Doujin     doujin;
            Collection collection;

            do
            {
                collection = await _database.GetCollectionAsync(_context.User.Id, name, cancellationToken);

                if (collection == null)
                {
                    collection = new Collection
                    {
                        Name    = name,
                        OwnerId = _context.User.Id,
                        Doujins = new List <CollectionRef>()
                    };

                    _database.Add(collection);
                }

                doujin = await _database.GetDoujinAsync(GalleryUtility.ExpandContraction(source),
                                                        id,
                                                        cancellationToken);

                if (doujin == null)
                {
                    await _context.ReplyAsync("doujinNotFound");

                    return;
                }

                if (collection.Doujins.Any(x => x.DoujinId == doujin.Id))
                {
                    await _context.ReplyAsync("alreadyInCollection", new { doujin, collection });

                    return;
                }

                collection.Doujins.Add(new CollectionRef
                {
                    DoujinId = doujin.Id
                });
            }while (!await _database.SaveAsync(cancellationToken));

            await _context.ReplyAsync("addedToCollection", new { doujin, collection });
        }
Exemple #4
0
        public async Task RemoveAsync(string name,
                                      string source,
                                      string id,
                                      CancellationToken cancellationToken = default)
        {
            name = FixCollectionName(name);

            Doujin     doujin;
            Collection collection;

            do
            {
                collection = await _database.GetCollectionAsync(_context.User.Id, name, cancellationToken);

                if (collection == null)
                {
                    await _context.ReplyAsync("collectionNotFound", new { name });

                    return;
                }

                doujin = await _database.GetDoujinAsync(GalleryUtility.ExpandContraction(source),
                                                        id,
                                                        cancellationToken);

                if (doujin == null)
                {
                    await _context.ReplyAsync("doujinNotFound");

                    return;
                }

                var item = collection.Doujins.FirstOrDefault(x => x.DoujinId == doujin.Id);

                if (item == null)
                {
                    await _context.ReplyAsync("notInCollection", new { doujin, collection });

                    return;
                }

                collection.Doujins.Remove(item);
            }while (!await _database.SaveAsync(cancellationToken));

            await _context.ReplyAsync("removedFromCollection", new { doujin, collection });
        }
Exemple #5
0
        public async Task GetAsync(string source,
                                   string id,
                                   CancellationToken cancellationToken = default)
        {
            var doujin = await _database.GetDoujinAsync(GalleryUtility.ExpandContraction(source),
                                                        id,
                                                        cancellationToken);

            if (doujin == null)
            {
                await _context.ReplyAsync("doujinNotFound");

                return;
            }

            await _interactive.SendInteractiveAsync(new DoujinMessage(doujin), _context, cancellationToken);
        }
Exemple #6
0
        public async Task SearchAsync(string query,
                                      string source = null,
                                      CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrEmpty(query))
            {
                await _context.ReplyAsync("invalidQuery", new { query });

                return;
            }

            await _interactive.SendInteractiveAsync(
                new DoujinListFromQueryMessage(new DoujinSearchArgs
            {
                Query = query,
                QualityFilter = false,
                Source = GalleryUtility.ExpandContraction(source)
            }),
                _context,
                cancellationToken);
        }
Exemple #7
0
        public async Task <bool> TryHandleAsync(IMessageContext context,
                                                CancellationToken cancellationToken = default)
        {
            switch (context.Event)
            {
            case MessageEvent.Create: break;

            default: return(false);
            }

            var content = context.Message.Content;

            // ignore urls in commands
            if (content.StartsWith(_settings.Discord.Prefix))
            {
                return(false);
            }

            // match gallery urls
            var ids = GalleryUtility.ParseMany(content);

            if (ids.Length == 0)
            {
                return(false);
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"Matched galleries: {string.Join(", ", ids.Select((s, i) => $"{s}/{i}"))}");
            }

            // send interactive
            using (context.BeginTyping())
            {
                // send one interactive if only one id detected
                if (ids.Length == 1)
                {
                    var(source, id) = ids[0];

                    Doujin doujin;

                    using (var scope = _services.CreateScope())
                    {
                        doujin = await scope.ServiceProvider
                                 .GetRequiredService <IDatabase>()
                                 .GetDoujinAsync(source, id, cancellationToken);
                    }

                    if (doujin == null)
                    {
                        await context.ReplyAsync("doujinNotFound");
                    }
                    else
                    {
                        await _interactive.SendInteractiveAsync(
                            new DoujinMessage(doujin),
                            context,
                            cancellationToken);
                    }
                }

                // send as a list
                else
                {
                    await _interactive.SendInteractiveAsync(
                        new GalleryUrlDetectedMessage(ids),
                        context,
                        cancellationToken);
                }
            }

            return(true);
        }