Exemplo n.º 1
0
        private async Task SlashCommand(SocketSlashCommand command)
        {
            if (command.ChannelId.HasValue &&
                command.Data.Options.Count > 0 &&
                PixivChannels.TryGetValue(command.ChannelId.Value, out var channelPixivSettings))
            {
                await command.DeferAsync();


                try {
                    var param = command.Data.Options.FirstOrDefault();
                    if (param != null && param.Value != null)
                    {
                        switch (param.Name)
                        {
                        case "id":

                            var illust = await PixivDownloader.GetIllustration((long)param.Value);

                            bool isFirstSent     = true;
                            var  pagesToDownload = Enumerable.Range(0, illust.PageCount);
                            //var attachments = new List<FileAttachment>();
                            if (illust != null && illust.PageCount > 0 && !CheckRestricted(channelPixivSettings, illust))
                            {
                                await command.DeleteOriginalResponseAsync();

                                foreach (var imageTask in PixivDownloader.DownloadIllistrationAsync(illust, maxPages: channelPixivSettings.MaxPages, maxBytes: MaxUploadBytes))
                                {
                                    using (var image = await imageTask) {
                                        string text = isFirstSent ? illust.ToString() : string.Empty;
                                        if (isFirstSent && channelPixivSettings.MaxPages.HasValue && pagesToDownload.Count() > channelPixivSettings.MaxPages)
                                        {
                                            text += $" [Showing {channelPixivSettings.MaxPages} images out of {pagesToDownload.Count()}]";
                                        }
                                        if (!image.IsOriginal)
                                        {
                                            text += " (preview version)";
                                        }
                                        text        = text.Trim();
                                        isFirstSent = false;
                                        Console.WriteLine($"Sending page {image.PageNumber}");
                                        await Uploader.SendImage(command.Channel, image, string.IsNullOrEmpty(text)?null : text);

                                        //command.Channel.SendFileAsync(image.ImageData, )
                                        //var ms = new MemoryStream();
                                        //image.ImageData.CopyTo(ms);
                                        //ms.Position = 0;
                                        //attachments.Add(new FileAttachment(ms, image.Filename));
                                    }
                                }
                            }
                            else
                            {
                                await command.ModifyOriginalResponseAsync(mp => mp.Content = "Error");
                            }

                            //if (attachments.Any()) {
                            //  ////await command.ModifyOriginalResponseAsync(mp => mp.Content = illust.ToString());
                            //  //await command.DeleteOriginalResponseAsync();
                            //  ////await command.FollowupWithFilesAsync(attachments);
                            //  ////await command.RespondWithFilesAsync(attachments, text: illust.ToString());
                            //}
                            //else


                            break;

                        case "artist":

                            break;

                        case "search":

                            break;

                        default:
                            await command.ModifyOriginalResponseAsync(mp => mp.Content = "Error: Invalid Command");

                            break;
                        }
                    }
                }
                catch (Exception ex) {
                    Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                    await command.ModifyOriginalResponseAsync(mp => mp.Content = "Error");
                }
            }
            else
            {
                await command.RespondAsync("Invalid Comment", ephemeral : true);
            };

            return;
        }