From 32704407f0ab4fe0a9479ea4c31b8c526480f0cb Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sat, 9 Dec 2017 11:13:11 -0500 Subject: [PATCH] images work --- .gitignore | 2 +- Commands/Definitions.cs | 2 +- Commands/Images.cs | 60 +++++++++++++++++++++++++++---- Services/CommandHandlerService.cs | 4 +-- dotbot.csproj | 1 + 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 7105e91..2113e39 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # dotbot specific dotbot.db _config.json - +UploadedImages/ # .net specific diff --git a/Commands/Definitions.cs b/Commands/Definitions.cs index 173d1f0..4a8e67e 100644 --- a/Commands/Definitions.cs +++ b/Commands/Definitions.cs @@ -25,7 +25,7 @@ namespace dotbot.Commands [Command("get")] [Summary("get some text that was saved")] - public async Task GetDefinition([Summary("key to look for")] string Key) => await base.ReplyAsync($"**{Key}**: {db.Defs.Find(Key)?.Def ?? "not set"}"); + public async Task GetDefinition([Summary("key to look for")] string Key) => await ReplyAsync($"**{Key}**: {db.Defs.Find(Key)?.Def ?? "not set"}"); [Command("unset")] diff --git a/Commands/Images.cs b/Commands/Images.cs index 3887fbc..5797cb6 100644 --- a/Commands/Images.cs +++ b/Commands/Images.cs @@ -1,7 +1,9 @@ using Discord.Commands; using dotbot.Core; using Microsoft.Extensions.Configuration; +using System.IO; using System.Linq; +using System.Net; using System.Threading.Tasks; namespace dotbot.Commands @@ -10,6 +12,13 @@ namespace dotbot.Commands public class Images : ModuleBase { public DotbotDb db { get; set; } + private readonly IConfigurationRoot _config; + + public Images(IConfigurationRoot config) + { + _config = config; + } + [Command] [Alias("image", "pic")] @@ -17,18 +26,22 @@ namespace dotbot.Commands [Summary("get a saved image")] public async Task GetImage([Summary("image name")] string name) { - + if (db.Images.Any(i => i.Id == name)) + { + await Context.Channel.TriggerTypingAsync(); + await Context.Message.DeleteAsync(); + var img = db.Images.Find(name); + await Context.Channel.SendFileAsync($"UploadedImages/{img.FilePath}", $"{img.Id} by {Context.User.Mention}"); + } + else await ReplyAsync($"no image `{name}` found. you can save one if you want by attaching an image with this command {_config["prefix"]}img save "); } - + [Command("list")] [Alias("ls")] [Priority(1)] [Summary("list all saved images")] - public async Task ListImages() - { - - } + public async Task ListImages() => await ReplyAsync($"here are the available images: \n```{string.Join(", ", db.Images.Select(i => i.Id))}```"); [Command("save")] @@ -36,10 +49,45 @@ namespace dotbot.Commands [Summary("save an image for later")] public async Task SaveImage([Summary("image name")] string name) { + await Context.Channel.TriggerTypingAsync(); + var attached = Context.Message.Attachments; + if (attached.Count != 1) + { + await ReplyAsync($"please attach 1 image"); + return; + } + var img = attached.First(); + (new WebClient { Proxy = null }).DownloadFile(img.Url, $"UploadedImages/{img.Filename}"); + + if (db.Images.Any(i => i.Id == name)) + db.Images.Find(name).FilePath = img.Filename; + else + db.Images.Add(new Image { Id = name, FilePath = img.Filename }); + db.SaveChanges(); + + await Context.Message.DeleteAsync(); + await ReplyAsync($"the image has been saved as {name}"); } + [Command("rm")] + [Alias("del", "remove", "delete")] + [Priority(1)] + [Summary("delete a saved image")] + public async Task DeleteImage([Summary("image name")] string name) + { + await Context.Channel.TriggerTypingAsync(); + if (db.Images.Any(i => i.Id == name)) + { + File.Delete($"UploadedImages/{name}"); + db.Images.Remove(db.Images.Find(name)); + db.SaveChanges(); + await ReplyAsync($"{name} has been deleted"); + } + else await ReplyAsync($"{name} didn't exist in the first place"); + } + } } diff --git a/Services/CommandHandlerService.cs b/Services/CommandHandlerService.cs index ce043f1..72ffc0a 100644 --- a/Services/CommandHandlerService.cs +++ b/Services/CommandHandlerService.cs @@ -60,12 +60,12 @@ namespace dotbot.Services await context.Channel.TriggerTypingAsync(); await context.Message.DeleteAsync(); var img = _db.Images.Find(key); - await context.Channel.SendFileAsync(img.FilePath, $"{img.Id} by {context.User.Mention}"); + await context.Channel.SendFileAsync($"UploadedImages/{img.FilePath}", $"{img.Id} by {context.User.Mention}"); return; } } - if (!result.IsSuccess) + if (!result.IsSuccess && result.ToString() != "UnknownCommand: Unknown command.") await context.Channel.SendMessageAsync(result.ToString()); } } diff --git a/dotbot.csproj b/dotbot.csproj index acd3659..dcc13d1 100644 --- a/dotbot.csproj +++ b/dotbot.csproj @@ -24,5 +24,6 @@ + \ No newline at end of file