Added system for backing up images

This commit is contained in:
Deukhoofd 2018-04-03 14:40:24 +02:00
parent 1cb3f981d5
commit 9606d90752
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
7 changed files with 110 additions and 11 deletions

View File

@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TTS/@EntryIndexedValue">TTS</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TTS/@EntryIndexedValue">TTS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=URL/@EntryIndexedValue">URL</s:String></wpf:ResourceDictionary>

View File

@ -63,6 +63,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
[Command("whatdoyouthinkdeukbot", PermissionLevel.Everyone)]
[Command("whatdoyouthink", PermissionLevel.Everyone)]
[CommandHelp("Gives the bots opinion about something", "Gives the bots opinion about something\nusage:\n``whatdoyouthink {about something}``")]
[CommandParameters(ParameterMatcher.ParameterType.Remainder)]
public async Task BotOpinion(CommandRequest request)
{

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
namespace DeukBot4.MessageHandlers
{
// ReSharper disable once ClassNeverInstantiated.Global
public class ServerChannelIDs
{
public ulong ServerId { get; set; }
public ulong ChannelId { get; set; }
}
public static class ImageBackupHandler
{
// ReSharper disable once ClassNeverInstantiated.Global
public class NextCloudSettings
{
public string URL { get; set; }
public string Username { get; set; }
public string Password { internal get; set; }
}
private static List<ServerChannelIDs> _backupChannels = new List<ServerChannelIDs>();
public static NextCloudSettings Settings { private get; set; }
public static async Task Backup(SocketMessage message)
{
if (!(message.Channel is IGuildChannel serverChannel))
return;
var images = message.Attachments.Where(x => x.Height.HasValue).ToArray();
if (!images.Any())
{
return;
}
var found = false;
foreach (var backupChannel in _backupChannels)
{
if (serverChannel.GuildId == backupChannel.ServerId && serverChannel.Id == backupChannel.ChannelId)
{
found = true;
break;
}
}
if (!found)
return;
foreach (var attachment in images)
{
using (var client = new WebClient())
{
var content = client.DownloadData(attachment.Url);
var uri = new Uri($"{Settings.URL}/{attachment.Filename}");
client.Credentials = new NetworkCredential(Settings.Username, Settings.Password);
try
{
client.UploadData(uri, "PUT", content);
var eb = new EmbedBuilder()
{
Title = "Image Backed Up",
Description = $"Image with the name {attachment.Filename} was backed up",
ThumbnailUrl = attachment.Url,
Timestamp = DateTime.UtcNow
};
await message.Channel.SendMessageAsync("", embed: eb.Build());
}
catch (Exception e)
{
await Logger.LogError(e);
}
}
}
}
public static void SetBackupChannels(List<ServerChannelIDs> ls)
{
_backupChannels = ls;
}
}
}

View File

@ -17,6 +17,7 @@ namespace DeukBot4.MessageHandlers
#pragma warning disable 4014
CommandHandler.CommandHandler.HandleMessage(message);
HandlePrivateMessage(message);
ImageBackupHandler.Backup(message);
#pragma warning restore 4014
}
catch (Exception e)

View File

@ -29,6 +29,9 @@ namespace DeukBot4
DatabaseInitializer.Initialize();
ServerSettingHandler.OnBotStartUp();
CommandHandler.Build();
ImageBackupHandler.SetBackupChannels(Settings.BackupChannels);
ImageBackupHandler.Settings = Settings.NextcloudSettings;
Client = new DiscordSocketClient();
Client.Log += Logger.LogDiscord;

View File

@ -1,4 +1,7 @@
using System.IO;
using System;
using System.Collections.Generic;
using System.IO;
using DeukBot4.MessageHandlers;
using Newtonsoft.Json;
namespace DeukBot4
@ -13,6 +16,10 @@ namespace DeukBot4
public ulong OwnerId { get; private set; }
[JsonProperty]
public string DatabaseConnectionString { get; private set; }
[JsonProperty]
public List<ServerChannelIDs> BackupChannels { get; private set; }
[JsonProperty]
public ImageBackupHandler.NextCloudSettings NextcloudSettings { get; private set; }
public static Settings FromJsonFile(string filepath)

View File

@ -2,7 +2,7 @@
namespace DeukBot4.Utilities
{
public class Lehvenstein
public static class Lehvenstein
{
/// <summary>
/// Calculates the Levenshtein distance between two strings--the number of changes that need to be made for the first string to become the second.
@ -19,32 +19,32 @@ namespace DeukBot4.Utilities
{
throw new ArgumentNullException(nameof(first));
}
if (second == null)
{
throw new ArgumentNullException(nameof(second));
}
int n = first.Length;
int m = second.Length;
var n = first.Length;
var m = second.Length;
var d = new int[n + 1, m + 1]; // matrix
if (n == 0) return m;
if (m == 0) return n;
for (int i = 0; i <= n; d[i, 0] = i++)
for (var i = 0; i <= n; d[i, 0] = i++)
{
}
for (int j = 0; j <= m; d[0, j] = j++)
for (var j = 0; j <= m; d[0, j] = j++)
{
}
for (int i = 1; i <= n; i++)
for (var i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
for (var j = 1; j <= m; j++)
{
int cost = (second.Substring(j - 1, 1) == first.Substring(i - 1, 1) ? 0 : 1); // cost
var cost = (second.Substring(j - 1, 1) == first.Substring(i - 1, 1) ? 0 : 1); // cost
d[i, j] = Math.Min(
Math.Min(
d[i - 1, j] + 1,