Fixed backup file names, improved handling of renaming things

This commit is contained in:
Deukhoofd 2018-04-22 18:45:04 +02:00
parent 42d169f157
commit dd01dba88d
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 25 additions and 5 deletions

View File

@ -55,18 +55,20 @@ namespace DeukBot4.MessageHandlers
{ {
using (var client = new WebClient()) using (var client = new WebClient())
{ {
var attachmentSplit = attachment.Filename.Split('.');
var filename = $"{attachmentSplit[0]}-{DateTime.UtcNow:s}.{attachmentSplit[1]}";
var content = client.DownloadData(attachment.Url); var content = client.DownloadData(attachment.Url);
var uri = new Uri($"{Settings.URL}/{attachment.Filename}-{DateTime.UtcNow:s}"); var uri = new Uri($"{Settings.URL}/{filename}");
client.Credentials = new NetworkCredential(Settings.Username, Settings.Password); client.Credentials = new NetworkCredential(Settings.Username, Settings.Password);
try try
{ {
client.UploadData(uri, "PUT", content); client.UploadData(uri, "PUT", content);
var attachmentSplit = attachment.Filename.Split('.');
var eb = new EmbedBuilder() var eb = new EmbedBuilder()
{ {
Title = "Image Backed Up", Title = "Image Backed Up",
Description = $"Image with the name ``{attachment.Filename}`` was backed up as " + Description = $"Image with the name ``{attachment.Filename}`` was backed up as " +
$"``{attachmentSplit[0]}-{DateTime.UtcNow:s}.{attachmentSplit[1]}``", $"``{filename}``",
ThumbnailUrl = attachment.Url, ThumbnailUrl = attachment.Url,
Timestamp = DateTime.UtcNow Timestamp = DateTime.UtcNow
}; };

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using DeukBot4.Utilities;
using Discord.WebSocket; using Discord.WebSocket;
namespace DeukBot4.MessageHandlers namespace DeukBot4.MessageHandlers
@ -37,10 +38,10 @@ namespace DeukBot4.MessageHandlers
private static async Task DeltaHandler(SocketMessage message) private static async Task DeltaHandler(SocketMessage message)
{ {
var lower = message.Content.ToLowerInvariant().Split(' '); var lower = message.Content.ToLowerInvariant().RemoveSpecialCharacters().Split(' ');
foreach (var s in lower) foreach (var s in lower)
{ {
var diff = Utilities.Lehvenstein.LevenshteinDistance(s, "delta"); var diff = Lehvenstein.LevenshteinDistance(s, "delta");
if (diff <= 1) if (diff <= 1)
{ {
await message.Channel.SendMessageAsync("uhh excuse me it's called Origin and it's an art"); await message.Channel.SendMessageAsync("uhh excuse me it's called Origin and it's an art");

View File

@ -0,0 +1,17 @@
using System.Text;
namespace DeukBot4.Utilities
{
public static class StringExtensions
{
public static string RemoveSpecialCharacters(this string str) {
StringBuilder sb = new StringBuilder();
foreach (char c in str) {
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
sb.Append(c);
}
}
return sb.ToString();
}
}
}