diff --git a/ModCore/HansTagImport/HansTag.cs b/ModCore/HansTagImport/HansTag.cs new file mode 100644 index 00000000..c4428da6 --- /dev/null +++ b/ModCore/HansTagImport/HansTag.cs @@ -0,0 +1,48 @@ +using Newtonsoft.Json; +using System; + +namespace ModCore.HansTagImport +{ + public struct HansTag + { + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("guild")] + public ulong Guild { get; set; } + + [JsonProperty("channel")] + public ulong? Channel { get; set; } + + // unknown + [JsonProperty("kind")] + public int Kind { get; set; } + + [JsonProperty("owner")] + public ulong Owner { get; set; } + + [JsonProperty("hidden")] + public bool Hidden { get; set; } + + [JsonProperty("latestRevision")] + public DateTimeOffset LatestRevision { get; set; } + + [JsonProperty("aliases")] + public string[] Aliases { get; set; } + + [JsonProperty("revisions")] + public HansTagRevision[] Revisions { get; set; } + } + + public struct HansTagRevision + { + [JsonProperty("contents")] + public string Contents { get; set; } + + [JsonProperty("created")] + public DateTimeOffset Created { get; set; } + + [JsonProperty("user")] + public ulong User { get; set; } + } +} diff --git a/ModCore/HansTagImport/Importer.cs b/ModCore/HansTagImport/Importer.cs new file mode 100644 index 00000000..d7082ec0 --- /dev/null +++ b/ModCore/HansTagImport/Importer.cs @@ -0,0 +1,57 @@ +using ModCore.Database; +using ModCore.Database.DatabaseEntities; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace ModCore.HansTagImport +{ + public class Importer + { + private List _loadedTags = new List(); + + public void LoadTags(Stream tagStream) + { + var serializer = new JsonSerializer(); + using var streamReader = new StreamReader(tagStream); + using var jsonReader = new JsonTextReader(streamReader); + _loadedTags = serializer.Deserialize>(jsonReader); + } + + public async Task DumpToDatabase(DatabaseContext database) + { + if(_loadedTags == default(List)) + { + throw new InvalidOperationException("No tags were loaded!"); + } + + foreach(var tag in _loadedTags) + { + var latestRevision = tag.Revisions.FirstOrDefault(x => x.Created == tag.LatestRevision); + + // check if tag exists + var existsTag = database.Tags.Any(x => x.Name == tag.Name && x.GuildId == (long)tag.Guild && x.ChannelId == (long)(tag.Channel ?? 0)); + + var modcoreTag = existsTag? database.Tags.FirstOrDefault(x => x.Name == tag.Name && x.GuildId == (long)tag.Guild && x.ChannelId == (long)(tag.Channel ?? 0)) + : new DatabaseTag(); + + modcoreTag.Name = tag.Name; + modcoreTag.ChannelId = (long)(tag.Channel ?? 0); + modcoreTag.Contents = latestRevision.Contents; + modcoreTag.CreatedAt = latestRevision.Created.DateTime; + modcoreTag.GuildId = (long)tag.Guild; + modcoreTag.OwnerId = (long)tag.Owner; + + if(existsTag) + database.Tags.Update(modcoreTag); + else + database.Tags.Add(modcoreTag); + } + + await database.SaveChangesAsync(); + } + } +} diff --git a/ModCore/LegacyCommands/Owner.cs b/ModCore/LegacyCommands/Owner.cs index 94a20cb7..629ae657 100644 --- a/ModCore/LegacyCommands/Owner.cs +++ b/ModCore/LegacyCommands/Owner.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.CommandsNext; @@ -11,6 +12,7 @@ using Microsoft.Extensions.DependencyInjection; using ModCore.Database; using ModCore.Entities; +using ModCore.HansTagImport; using ModCore.Utils; using ModCore.Utils.Extensions; @@ -28,6 +30,26 @@ public Owner(SharedData shared, DatabaseContextBuilder db) this.Database = db; } + [Command("import"), Hidden, RequireOwner] + public async Task ImportTagsAsync(CommandContext context) + { + if(!context.Message.Attachments.Any() || !context.Message.Attachments[0].FileName.EndsWith(".json")) + { + await context.RespondAsync("u dum lmao big L cope"); + return; + } + + await context.RespondAsync("Importing tags..."); + + var importer = new Importer(); + using HttpClient http = new HttpClient(); + var downloadStream = await http.GetStreamAsync(context.Message.Attachments[0].Url); + importer.LoadTags(downloadStream); + await importer.DumpToDatabase(Database.CreateContext()); + + await context.RespondAsync("Done importing tags!"); + } + [Command("clear"), Hidden, RequireOwner] public async Task ClearCommandsAsync(CommandContext context) {