diff --git a/DeukBot4.sln.DotSettings b/DeukBot4.sln.DotSettings index ca11ccf..e5850a1 100644 --- a/DeukBot4.sln.DotSettings +++ b/DeukBot4.sln.DotSettings @@ -1,2 +1,3 @@  - TTS \ No newline at end of file + TTS + URL \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs b/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs index 9ca3e59..b6966e3 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs @@ -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) { diff --git a/DeukBot4/MessageHandlers/ImageBackupHandler.cs b/DeukBot4/MessageHandlers/ImageBackupHandler.cs new file mode 100644 index 0000000..041a537 --- /dev/null +++ b/DeukBot4/MessageHandlers/ImageBackupHandler.cs @@ -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 _backupChannels = new List(); + 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 ls) + { + _backupChannels = ls; + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/MainHandler.cs b/DeukBot4/MessageHandlers/MainHandler.cs index ff31f09..7e7dba2 100644 --- a/DeukBot4/MessageHandlers/MainHandler.cs +++ b/DeukBot4/MessageHandlers/MainHandler.cs @@ -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) diff --git a/DeukBot4/Program.cs b/DeukBot4/Program.cs index 80402c1..19ec46a 100644 --- a/DeukBot4/Program.cs +++ b/DeukBot4/Program.cs @@ -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; diff --git a/DeukBot4/Settings.cs b/DeukBot4/Settings.cs index 65e9b77..544b0e6 100644 --- a/DeukBot4/Settings.cs +++ b/DeukBot4/Settings.cs @@ -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 BackupChannels { get; private set; } + [JsonProperty] + public ImageBackupHandler.NextCloudSettings NextcloudSettings { get; private set; } public static Settings FromJsonFile(string filepath) diff --git a/DeukBot4/Utilities/Lehvenstein.cs b/DeukBot4/Utilities/Lehvenstein.cs index f8c6bbd..55eeb5a 100644 --- a/DeukBot4/Utilities/Lehvenstein.cs +++ b/DeukBot4/Utilities/Lehvenstein.cs @@ -2,7 +2,7 @@ namespace DeukBot4.Utilities { - public class Lehvenstein + public static class Lehvenstein { /// /// 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,