public async Task AddLastFMFriendAsync(string discordUserID, string lastfmusername) { User user = db.Users.FirstOrDefault(f => f.DiscordUserID == discordUserID); if (user == null) { User newUser = new User { DiscordUserID = discordUserID, UserType = UserType.User }; db.Users.Add(newUser); user = newUser; } Friend friend = new Friend { User = user, LastFMUserName = lastfmusername, }; db.Friends.Add(friend); db.SaveChanges(); await Task.CompletedTask; }
// Set LastFM Name public void SetLastFM(IUser discordUser, User newUserSettings) { using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString); var user = db.Users.FirstOrDefault(f => f.DiscordUserId == discordUser.Id); this._cache.Remove($"user-settings-{discordUser.Id}"); this._cache.Remove($"user-isRegistered-{discordUser.Id}"); if (user == null) { var newUser = new User { DiscordUserId = discordUser.Id, UserType = UserType.User, UserNameLastFM = newUserSettings.UserNameLastFM, TitlesEnabled = true, ChartTimePeriod = ChartTimePeriod.Monthly, FmEmbedType = newUserSettings.FmEmbedType, FmCountType = newUserSettings.FmCountType, SessionKeyLastFm = newUserSettings.SessionKeyLastFm }; db.Users.Add(newUser); try { db.SaveChanges(); } catch (Exception e) { Log.Error(e, "Error in SetLastFM"); throw; } } else { user.UserNameLastFM = newUserSettings.UserNameLastFM; user.FmEmbedType = newUserSettings.FmEmbedType; user.FmCountType = newUserSettings.FmCountType; if (!string.Equals(user.UserNameLastFM, newUserSettings.UserNameLastFM, StringComparison.CurrentCultureIgnoreCase)) { user.SessionKeyLastFm = newUserSettings.SessionKeyLastFm; } db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } }
// Set LastFM Name public void SetLastFM(IUser discordUser, User userSettings) { using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString); var user = db.Users.FirstOrDefault(f => f.DiscordUserId == discordUser.Id); if (user == null) { var newUser = new User { DiscordUserId = discordUser.Id, UserType = UserType.User, UserNameLastFM = userSettings.UserNameLastFM, TitlesEnabled = true, ChartTimePeriod = ChartTimePeriod.Monthly, FmEmbedType = userSettings.FmEmbedType, FmCountType = userSettings.FmCountType }; db.Users.Add(newUser); try { db.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); throw; } } else { user.UserNameLastFM = userSettings.UserNameLastFM; user.FmEmbedType = userSettings.FmEmbedType; user.FmCountType = userSettings.FmCountType; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } }
// Random user public async Task <string> GetRandomLastFMUserAsync() { User featuredUser = await db.Users.FirstOrDefaultAsync(f => f.Featured == true).ConfigureAwait(false); if (featuredUser != null) { featuredUser.Featured = false; db.Entry(featuredUser).State = EntityState.Modified; } List <User> users = db.Users.Where(w => w.Blacklisted != true).ToList(); Random rand = new Random(); User user = users[rand.Next(users.Count)]; user.Featured = true; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return(user.UserNameLastFM); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("No arguments defined. Please input the folder path you wish to migrate."); Console.ReadKey(); return; } Console.WriteLine("Migrating..."); DirectoryInfo d = new DirectoryInfo(args[0]); //Assuming Test is your Folder FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files var users = db.Users.ToList(); foreach (FileInfo file in Files) { try { string line1 = File.ReadLines(file.FullName).First(); if (!users.Select(s => s.UserNameLastFM).Contains(line1)) { User User = new User { DiscordUserID = file.Name, UserNameLastFM = line1, ChartTimePeriod = ChartTimePeriod.Monthly }; db.Users.Add(User); Console.WriteLine("Added user " + User.UserNameLastFM); } else { Console.WriteLine("Skipped user " + line1 + " (already exists)"); } } catch { } } db.SaveChanges(); Console.WriteLine("Done! Press any key to stop..."); Console.ReadKey(); }