Skip to content

Commit

Permalink
Merge pull request #56 from MJHeijster/develop
Browse files Browse the repository at this point in the history
Bugfixes for the still unreleased 1.6.0.
  • Loading branch information
MJHeijster committed Jun 5, 2022
2 parents 0e60c48 + a85c2c2 commit ea7e5c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
11 changes: 9 additions & 2 deletions StatBot/Database/DatabaseHandlers/UserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ public static void InsertOrUpdateUser(User user)
{
if (userCache == null)
userCache = GetUsers();
if (!userCache.Contains(user))
var userFromCache = userCache.FirstOrDefault(c => c.Id == user.Id) ?? new User();
user.OverrideName = userFromCache.OverrideName;
if (!userCache.Any(c => c.Id == user.Id && c.Username == user.Username && c.Discrim == user.Discrim && c.AvatarUri == user.AvatarUri && c.IsBot == user.IsBot && c.OverrideName == user.OverrideName))
{
string command = $"REPLACE INTO Users(Id, Username, Discrim, AvatarUri, IsBot) VALUES({user.Id},'{user.Username}','{user.Discrim}','{user.AvatarUri}',{user.IsBot})";

string command = $"REPLACE INTO Users(Id, Username, Discrim, AvatarUri, IsBot, OverrideName) VALUES({user.Id},'{user.Username}','{user.Discrim}','{user.AvatarUri}',{user.IsBot}, '{user.OverrideName}')";
using (var connection = new SqliteConnection("Data Source=Database\\Statbot.db;"))
{
connection.Open();
Expand Down Expand Up @@ -137,6 +140,10 @@ public static List<User> GetUsers(bool includingOld = false)
{
oldUsers = GetOldUsers(id);
}
if (!oldUsers.Any())
{
oldUsers = null;
}
users.Add(new User(id, rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], oldUsers));

}
Expand Down
28 changes: 23 additions & 5 deletions StatBot/Database/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public User(SocketUser author)
Discrim = author.Discriminator;
AvatarUri = author.GetAvatarUrl();
}
/// <summary>
/// Initializes a new instance of the <see cref="User"/> class.
/// </summary>
public User()
{

}

/// <summary>
/// Initializes a new instance of the <see cref="User" /> class.
Expand Down Expand Up @@ -105,14 +112,25 @@ public User(string id, string username, string discrim, string avatarUri, bool i
/// <param name="oldUsers">The old users.</param>
public User(object id, object username, object discrim, object avatarUri, object isBot, object isExcludedFromStats, object overrideName, List<OldUser> oldUsers = null)
{
Id = Convert.ToUInt64(id);
Username = (string)username;
Discrim = (string)discrim;
AvatarUri = (string)avatarUri;
Id = ConvertFromDBVal<ulong>(id);
Username = ConvertFromDBVal<string>(username);
Discrim = ConvertFromDBVal<string>(discrim);
AvatarUri = ConvertFromDBVal<string>(avatarUri);
IsBot = (Int64)isBot == 1;
IsExcludedFromStats = (Int64)isExcludedFromStats == 1;
OverrideName = overrideName == null ? (string)OverrideName : null;
OverrideName = ConvertFromDBVal<string>(overrideName);
OldUsers = oldUsers;
}
public static T ConvertFromDBVal<T>(object obj)
{
if (obj == null || obj == DBNull.Value)
{
return default(T); // returns the default value for the type
}
else
{
return (T)obj;
}
}
}
}

0 comments on commit ea7e5c4

Please sign in to comment.