Skip to content

Commit 0a88ba9

Browse files
committed
Hans Tag Importer :0
1 parent 5e3fcc5 commit 0a88ba9

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

ModCore/HansTagImport/HansTag.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
4+
namespace ModCore.HansTagImport
5+
{
6+
public struct HansTag
7+
{
8+
[JsonProperty("name")]
9+
public string Name { get; set; }
10+
11+
[JsonProperty("guild")]
12+
public ulong Guild { get; set; }
13+
14+
[JsonProperty("channel")]
15+
public ulong? Channel { get; set; }
16+
17+
// unknown
18+
[JsonProperty("kind")]
19+
public int Kind { get; set; }
20+
21+
[JsonProperty("owner")]
22+
public ulong Owner { get; set; }
23+
24+
[JsonProperty("hidden")]
25+
public bool Hidden { get; set; }
26+
27+
[JsonProperty("latestRevision")]
28+
public DateTimeOffset LatestRevision { get; set; }
29+
30+
[JsonProperty("aliases")]
31+
public string[] Aliases { get; set; }
32+
33+
[JsonProperty("revisions")]
34+
public HansTagRevision[] Revisions { get; set; }
35+
}
36+
37+
public struct HansTagRevision
38+
{
39+
[JsonProperty("contents")]
40+
public string Contents { get; set; }
41+
42+
[JsonProperty("created")]
43+
public DateTimeOffset Created { get; set; }
44+
45+
[JsonProperty("user")]
46+
public ulong User { get; set; }
47+
}
48+
}

ModCore/HansTagImport/Importer.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using ModCore.Database;
2+
using ModCore.Database.DatabaseEntities;
3+
using Newtonsoft.Json;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace ModCore.HansTagImport
11+
{
12+
public class Importer
13+
{
14+
private List<HansTag> _loadedTags = new List<HansTag>();
15+
16+
public void LoadTags(Stream tagStream)
17+
{
18+
var serializer = new JsonSerializer();
19+
using var streamReader = new StreamReader(tagStream);
20+
using var jsonReader = new JsonTextReader(streamReader);
21+
_loadedTags = serializer.Deserialize<List<HansTag>>(jsonReader);
22+
}
23+
24+
public async Task DumpToDatabase(DatabaseContext database)
25+
{
26+
if(_loadedTags == default(List<HansTag>))
27+
{
28+
throw new InvalidOperationException("No tags were loaded!");
29+
}
30+
31+
foreach(var tag in _loadedTags)
32+
{
33+
var latestRevision = tag.Revisions.FirstOrDefault(x => x.Created == tag.LatestRevision);
34+
35+
// check if tag exists
36+
var existsTag = database.Tags.Any(x => x.Name == tag.Name && x.GuildId == (long)tag.Guild && x.ChannelId == (long)(tag.Channel ?? 0));
37+
38+
var modcoreTag = existsTag? database.Tags.FirstOrDefault(x => x.Name == tag.Name && x.GuildId == (long)tag.Guild && x.ChannelId == (long)(tag.Channel ?? 0))
39+
: new DatabaseTag();
40+
41+
modcoreTag.Name = tag.Name;
42+
modcoreTag.ChannelId = (long)(tag.Channel ?? 0);
43+
modcoreTag.Contents = latestRevision.Contents;
44+
modcoreTag.CreatedAt = latestRevision.Created.DateTime;
45+
modcoreTag.GuildId = (long)tag.Guild;
46+
modcoreTag.OwnerId = (long)tag.Owner;
47+
48+
if(existsTag)
49+
database.Tags.Update(modcoreTag);
50+
else
51+
database.Tags.Add(modcoreTag);
52+
}
53+
54+
await database.SaveChangesAsync();
55+
}
56+
}
57+
}

ModCore/LegacyCommands/Owner.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Threading.Tasks;
56
using DSharpPlus;
67
using DSharpPlus.CommandsNext;
@@ -11,6 +12,7 @@
1112
using Microsoft.Extensions.DependencyInjection;
1213
using ModCore.Database;
1314
using ModCore.Entities;
15+
using ModCore.HansTagImport;
1416
using ModCore.Utils;
1517
using ModCore.Utils.Extensions;
1618

@@ -28,6 +30,26 @@ public Owner(SharedData shared, DatabaseContextBuilder db)
2830
this.Database = db;
2931
}
3032

33+
[Command("import"), Hidden, RequireOwner]
34+
public async Task ImportTagsAsync(CommandContext context)
35+
{
36+
if(!context.Message.Attachments.Any() || !context.Message.Attachments[0].FileName.EndsWith(".json"))
37+
{
38+
await context.RespondAsync("u dum lmao big L cope");
39+
return;
40+
}
41+
42+
await context.RespondAsync("Importing tags...");
43+
44+
var importer = new Importer();
45+
using HttpClient http = new HttpClient();
46+
var downloadStream = await http.GetStreamAsync(context.Message.Attachments[0].Url);
47+
importer.LoadTags(downloadStream);
48+
await importer.DumpToDatabase(Database.CreateContext());
49+
50+
await context.RespondAsync("Done importing tags!");
51+
}
52+
3153
[Command("clear"), Hidden, RequireOwner]
3254
public async Task ClearCommandsAsync(CommandContext context)
3355
{

0 commit comments

Comments
 (0)