|
| 1 | +using Discord; |
| 2 | +using Discord.Commands; |
| 3 | +using Dogey.Services; |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Dogey.Commands |
| 12 | +{ |
| 13 | + public class GDPRModule : DogeyModuleBase |
| 14 | + { |
| 15 | + private readonly RootDatabase _db; |
| 16 | + private User _currentUser; |
| 17 | + |
| 18 | + public GDPRModule(LocaleService locale, RootDatabase db) : base(locale) |
| 19 | + { |
| 20 | + _db = db; |
| 21 | + } |
| 22 | + |
| 23 | + protected override void BeforeExecute(CommandInfo command) |
| 24 | + { |
| 25 | + _currentUser = _db.Users.SingleOrDefault(x => x.Id == Context.User.Id); |
| 26 | + } |
| 27 | + |
| 28 | + [Command("exportuser")] |
| 29 | + public async Task ExportUserDataAsync() |
| 30 | + { |
| 31 | + var user = await _db.Users.Include(x => x.Guilds).SingleOrDefaultAsync(x => x.Id == Context.User.Id); |
| 32 | + if (user == null) |
| 33 | + { |
| 34 | + await ReplyAsync(_locale.GetString("gdpr:user_data_null", Context.User, _currentUser.Locale)); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + var directory = Path.Combine(AppContext.BaseDirectory, $"common/temp"); |
| 39 | + if (!Directory.Exists(directory)) |
| 40 | + Directory.CreateDirectory(directory); |
| 41 | + |
| 42 | + var filePath = Path.Combine(directory, $"{Context.User.Id}-data.json"); |
| 43 | + using (var file = File.CreateText(filePath)) |
| 44 | + { |
| 45 | + var serializer = new JsonSerializer() |
| 46 | + { |
| 47 | + Formatting = Formatting.Indented, |
| 48 | + NullValueHandling = NullValueHandling.Ignore, |
| 49 | + ReferenceLoopHandling = ReferenceLoopHandling.Ignore |
| 50 | + }; |
| 51 | + serializer.Serialize(file, user); |
| 52 | + } |
| 53 | + |
| 54 | + await Context.User.SendFileAsync(filePath); |
| 55 | + await ReplyAsync(_locale.GetString("gdpr:user_data_get", Context.User, _currentUser.Locale)); |
| 56 | + File.Delete(filePath); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments