Advertisement
Stillkill

Untitled

Jan 27th, 2019
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. //Command
  2. public async Task UserCrawl()
  3. {
  4.     List<SocketGuildUser> userList = new List<SocketGuildUser>();
  5.     foreach (SocketGuildUser User in Context.Guild.Users)
  6.     {
  7.         Console.WriteLine($"Found user: {User.Username}#{User.Discriminator}");
  8.         userList.Add(User);
  9.     }
  10.     foreach (SocketGuildUser user in userList)
  11.     {
  12.         Console.WriteLine($"Saving user: {user.Username}#{user.Discriminator}");
  13.         Program.DatabaseHandler.AddNewProfile(user, false);
  14.     }
  15. }
  16.  
  17. //Create UserProfiles from users detected in the server
  18. public void AddNewProfile(SocketGuildUser user, bool isBanned = false)
  19. {
  20.     UserProfile profile = new UserProfile(user, false);
  21.     if (!UserProfiles.ContainsKey(user.Id)) UserProfiles.Add(user.Id, new UserProfile(user, false));
  22.     SaveProfile(profile.GetSerializable());
  23. }
  24.  
  25. //Serialize UserObject to JSON and write to file
  26. private void SaveProfile(SerialUserProfile profile)
  27. {
  28.     try
  29.     {
  30.         JsonSerializerSettings settings = new JsonSerializerSettings();
  31.         settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
  32.         settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
  33.         if (!File.Exists($@"{userProfilePath}\{profile.UserSocket.Id.ToString()}.json"))
  34.         {
  35.             try
  36.             {
  37.                 Console.WriteLine($"Serializing user: {profile.UserSocket.Username}#{profile.UserSocket.Discriminator}");
  38.                 File.WriteAllText($@"{userProfilePath}\{profile.UserSocket.Id.ToString()}.json", JsonConvert.SerializeObject(profile, settings)); //Error thrown here
  39.             }
  40.             catch (Exception e)
  41.             {
  42.                 Console.WriteLine(e.Message);
  43.                 throw;
  44.             }
  45.         }
  46.     }
  47.     catch (Exception e)
  48.     {
  49.         Console.WriteLine(e.Message);
  50.     }
  51. }
  52.  
  53. //UserProfile Object
  54. public class UserProfile
  55. {
  56.        
  57.     private List<UserWarning> UserWarnings { get; set; }
  58.     private List<UserBan> UserBans { get; set; }
  59.     private SocketGuildUser UserSocket { get; set; }
  60.     private bool IsBanned { get; set; }
  61.  
  62.     public UserProfile(SocketGuildUser user, bool isBanned = false)
  63.     {
  64.         this.UserSocket = user;
  65.         this.IsBanned = isBanned;
  66.         this.UserWarnings = new List<UserWarning>();
  67.         this.UserBans = new List<UserBan>();
  68.         InitAsync();
  69.     }
  70.  
  71.     private async Task InitAsync()
  72.     {
  73.         foreach (UserWarning warn in UserWarnings)
  74.         {
  75.                
  76.         }
  77.     }
  78.  
  79.     public List<UserWarning> GetUserWarnings()
  80.     {
  81.         return this.UserWarnings;
  82.     }
  83.  
  84.     public List<UserBan> GetUserBans()
  85.     {
  86.         return this.UserBans;
  87.     }
  88.  
  89.     public SocketGuildUser GetUserSocket()
  90.     {
  91.         return this.UserSocket;
  92.     }
  93.  
  94.     public bool GetBanState()
  95.     {
  96.         return this.IsBanned;
  97.     }
  98.  
  99.     public void AdduserBan(bool isHolderBan, Discord.Rest.RestUserMessage msg, SocketGuildUser user)
  100.     {
  101.         UserBan ban = new UserBan(DateTime.Now, UserSocket, new TimeSpan(42069), "Holding ban", isHolderBan, msg, user);
  102.     }
  103.  
  104.     public SerialUserProfile GetSerializable()
  105.     {
  106.         SerialUserProfile profile = new SerialUserProfile();
  107.         profile.IsBanned = GetBanState();
  108.         profile.UserBans = GetUserBans();
  109.         profile.UserSocket = GetUserSocket();
  110.         profile.UserWarnings = GetUserWarnings();
  111.         return profile;
  112.     }
  113. }
  114.  
  115. //version of UserProfile for serialization
  116. public class SerialUserProfile
  117. {
  118.     public List<UserWarning> UserWarnings { get; set; }
  119.     public List<UserBan> UserBans { get; set; }
  120.     public SocketGuildUser UserSocket { get; set; }
  121.     public bool IsBanned { get; set; }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement