Change exception handling to null checking
This commit is contained in:
parent
2f823a3730
commit
a16354e425
1 changed files with 4 additions and 8 deletions
|
@ -43,10 +43,8 @@ class UserCachingSubservice {
|
||||||
|
|
||||||
// IMPORTANT: Do NOT forget to save changes in database after calling this!
|
// IMPORTANT: Do NOT forget to save changes in database after calling this!
|
||||||
private static void UpdateUser(SocketUser user, BotDatabaseContext db) {
|
private static void UpdateUser(SocketUser user, BotDatabaseContext db) {
|
||||||
CachedUser uinfo;
|
var uinfo = db.UserCache.Where(c => c.UserId == (long)user.Id).SingleOrDefault();
|
||||||
try {
|
if (uinfo == null) {
|
||||||
uinfo = db.UserCache.Where(c => c.UserId == (long)user.Id).First();
|
|
||||||
} catch (InvalidOperationException) {
|
|
||||||
uinfo = new() { UserId = (long)user.Id };
|
uinfo = new() { UserId = (long)user.Id };
|
||||||
db.UserCache.Add(uinfo);
|
db.UserCache.Add(uinfo);
|
||||||
}
|
}
|
||||||
|
@ -58,10 +56,8 @@ class UserCachingSubservice {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void UpdateGuildUser(SocketGuildUser user, BotDatabaseContext db) {
|
private static void UpdateGuildUser(SocketGuildUser user, BotDatabaseContext db) {
|
||||||
CachedGuildUser guinfo;
|
var guinfo = db.GuildUserCache.Where(c => c.GuildId == (long)user.Guild.Id && c.UserId == (long)user.Id).SingleOrDefault();
|
||||||
try {
|
if (guinfo == null) {
|
||||||
guinfo = db.GuildUserCache.Where(c => c.GuildId == (long)user.Guild.Id && c.UserId == (long)user.Id).First();
|
|
||||||
} catch (InvalidOperationException) {
|
|
||||||
guinfo = new() { GuildId = (long)user.Guild.Id, UserId = (long)user.Id };
|
guinfo = new() { GuildId = (long)user.Guild.Id, UserId = (long)user.Id };
|
||||||
db.GuildUserCache.Add(guinfo);
|
db.GuildUserCache.Add(guinfo);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue