images work

This commit is contained in:
Ben Harris 2017-12-09 11:13:11 -05:00
parent ed808f938b
commit 32704407f0
5 changed files with 59 additions and 10 deletions

2
.gitignore vendored
View File

@ -1,7 +1,7 @@
# dotbot specific
dotbot.db
_config.json
UploadedImages/
# .net specific

View File

@ -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")]

View File

@ -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<SocketCommandContext>
{
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 <name>");
}
[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");
}
}
}

View File

@ -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());
}
}

View File

@ -24,5 +24,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Fonts\" />
<Folder Include="UploadedImages\" />
</ItemGroup>
</Project>