Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Command
- public async Task UserCrawl()
- {
- List<SocketGuildUser> userList = new List<SocketGuildUser>();
- foreach (SocketGuildUser User in Context.Guild.Users)
- {
- Console.WriteLine($"Found user: {User.Username}#{User.Discriminator}");
- userList.Add(User);
- }
- foreach (SocketGuildUser user in userList)
- {
- Console.WriteLine($"Saving user: {user.Username}#{user.Discriminator}");
- Program.DatabaseHandler.AddNewProfile(user, false);
- }
- }
- //Create UserProfiles from users detected in the server
- public void AddNewProfile(SocketGuildUser user, bool isBanned = false)
- {
- UserProfile profile = new UserProfile(user, false);
- if (!UserProfiles.ContainsKey(user.Id)) UserProfiles.Add(user.Id, new UserProfile(user, false));
- SaveProfile(profile.GetSerializable());
- }
- //Serialize UserObject to JSON and write to file
- private void SaveProfile(SerialUserProfile profile)
- {
- try
- {
- JsonSerializerSettings settings = new JsonSerializerSettings();
- settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
- settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
- if (!File.Exists($@"{userProfilePath}\{profile.UserSocket.Id.ToString()}.json"))
- {
- try
- {
- Console.WriteLine($"Serializing user: {profile.UserSocket.Username}#{profile.UserSocket.Discriminator}");
- File.WriteAllText($@"{userProfilePath}\{profile.UserSocket.Id.ToString()}.json", JsonConvert.SerializeObject(profile, settings)); //Error thrown here
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- throw;
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- //UserProfile Object
- public class UserProfile
- {
- private List<UserWarning> UserWarnings { get; set; }
- private List<UserBan> UserBans { get; set; }
- private SocketGuildUser UserSocket { get; set; }
- private bool IsBanned { get; set; }
- public UserProfile(SocketGuildUser user, bool isBanned = false)
- {
- this.UserSocket = user;
- this.IsBanned = isBanned;
- this.UserWarnings = new List<UserWarning>();
- this.UserBans = new List<UserBan>();
- InitAsync();
- }
- private async Task InitAsync()
- {
- foreach (UserWarning warn in UserWarnings)
- {
- }
- }
- public List<UserWarning> GetUserWarnings()
- {
- return this.UserWarnings;
- }
- public List<UserBan> GetUserBans()
- {
- return this.UserBans;
- }
- public SocketGuildUser GetUserSocket()
- {
- return this.UserSocket;
- }
- public bool GetBanState()
- {
- return this.IsBanned;
- }
- public void AdduserBan(bool isHolderBan, Discord.Rest.RestUserMessage msg, SocketGuildUser user)
- {
- UserBan ban = new UserBan(DateTime.Now, UserSocket, new TimeSpan(42069), "Holding ban", isHolderBan, msg, user);
- }
- public SerialUserProfile GetSerializable()
- {
- SerialUserProfile profile = new SerialUserProfile();
- profile.IsBanned = GetBanState();
- profile.UserBans = GetUserBans();
- profile.UserSocket = GetUserSocket();
- profile.UserWarnings = GetUserWarnings();
- return profile;
- }
- }
- //version of UserProfile for serialization
- public class SerialUserProfile
- {
- public List<UserWarning> UserWarnings { get; set; }
- public List<UserBan> UserBans { get; set; }
- public SocketGuildUser UserSocket { get; set; }
- public bool IsBanned { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement