Пример #1
0
            public override async Task <bool> RunAsync(CancellationToken cancellationToken = default)
            {
                if (!await base.RunAsync(cancellationToken) ||
                    !DoujinMessage.TryParseDoujinIdFromMessage(Context.Message, out var id, out var isFeed))
                {
                    return(false);
                }

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

                if (doujin == null)
                {
                    return(false);
                }

                var context = Context as IDiscordContext;

                if (isFeed || Interactive?.Source?.Id != Context.User.Id)
                {
                    context = new DiscordContextWrapper(context)
                    {
                        Channel = await Context.User.GetOrCreateDMChannelAsync()
                    }
                }
                ;

                // send download interactive
                await _interactive.SendInteractiveAsync(new DownloadMessage(doujin), context, cancellationToken);

                return(true);
            }
        }
Пример #2
0
            public override async Task <bool> RunAsync(CancellationToken cancellationToken = default)
            {
                if (!await base.RunAsync(cancellationToken))
                {
                    return(false);
                }

                if (!DoujinMessage.TryParseDoujinIdFromMessage(Context.Message, out var id, out var isFeed))
                {
                    return(false);
                }

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

                if (doujin == null)
                {
                    return(false);
                }

                bool added;

                Collection collection;

                do
                {
                    collection = await _database.GetCollectionAsync(
                        Context.User.Id,
                        _favoritesCollection,
                        cancellationToken);

                    if (collection == null)
                    {
                        // create new collection for favorites
                        collection = new Collection
                        {
                            Name    = _favoritesCollection,
                            OwnerId = Context.User.Id,
                            Doujins = new List <CollectionRef>()
                        };

                        _database.Add(collection);
                    }

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

                    if (existingRef == null)
                    {
                        // add to favorites collection
                        collection.Doujins.Add(new CollectionRef
                        {
                            DoujinId = doujin.Id
                        });

                        added = true;
                    }
                    else
                    {
                        // remove from favorites collection
                        collection.Doujins.Remove(existingRef);

                        added = false;
                    }
                }while (!await _database.SaveAsync(cancellationToken));

                var context = Context as IDiscordContext;

                if (isFeed || Interactive?.Source?.Id != Context.User.Id)
                {
                    context = new DiscordContextWrapper(Context)
                    {
                        Channel = await Context.User.GetOrCreateDMChannelAsync()
                    }
                }
                ;

                if (added)
                {
                    await context.ReplyAsync("addedToCollection",
                                             new { doujin, collection },
                                             TimeSpan.FromSeconds(5));
                }
                else
                {
                    await context.ReplyAsync("removedFromCollection",
                                             new { doujin, collection },
                                             TimeSpan.FromSeconds(5));
                }

                return(true);
            }
        }