public async Task setBirthday(SocketUser user, string month, string day) { try { //check if birthday channel in guild exists bool birthdayChannelExists = false; foreach (var serverBirthday in Birthday.serverBirthdays) { //TODO if (serverBirthday.serverId == Context.Guild.Id) { birthdayChannelExists = true; break; } } if (birthdayChannelExists == false) { await ReplyAsync($"Require birthday channel to be set to add birthdays"); return; } //set the birthday UserBirthday userBirthday = new UserBirthday(); userBirthday.userId = user.Id; try { userBirthday.month = int.Parse(removeLeading0s(month)); userBirthday.day = int.Parse(removeLeading0s(day)); } catch (Exception e) { Console.WriteLine("ERROR parsing birthday"); return; } //find the guild in serverBirthdays foreach (var server in Birthday.serverBirthdays) { //if we found the server to add user to if (server.serverId == Context.Guild.Id) { //remove this user if they're in alraedy //see link for bug (would force command to be executed twice) //https://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute foreach (var birthday in server.getBirthdays().ToList()) { if (birthday.userId == userBirthday.userId) { server.getBirthdays().Remove(birthday); } } //add user to server Birthdays server.addUser(userBirthday); } } //rewrite the file Birthday.rewriteBirthdayFile(Context); //have Birthday reset its birthday list Birthday.getBirthdays(); await ReplyAsync($"Birthday added for {user}"); } catch (Exception exception) { try { await ReplyAsync("General Error"); } catch (Exception ex) { } } }//end task
public void addUser(UserBirthday user) { userBirthdays.Add(user); }
public static void getBirthdays() { //clear the channel list announcementChannels.Clear(); //clear server birthday list serverBirthdays.Clear(); //for each guild, get its announcment channel foreach (SocketGuild guild in guilds) { ulong id = guild.Id; string file = $"{baseDirectory}/{id}/{fileName}"; StreamReader reader = null; try { reader = new StreamReader(file); } catch (Exception e) { } string channelID = null; //read line if (reader == null) { continue; } try { channelID = reader.ReadLine(); } catch (Exception e) { //couldn't read line Console.WriteLine("Exception: " + e.Message + "\n"); } //if channelId is null, close reader and continue if (channelID == null) { if(reader != null) { reader.Close(); } continue; } //if not null, add to list of announcement channels ulong channelIDNum = ulong.Parse(channelID); //create a new serverBirthday to hold our info ServerBirthday serverBirthday = new ServerBirthday(); serverBirthday.setChannelId(channelIDNum); serverBirthday.setServerId(guild.Id); //get all users birthday string birthdaysFile = $"{baseDirectory}/{id}/{fileName2}"; StreamReader birthdayReader = null; try { birthdayReader = new StreamReader(birthdaysFile); } catch (Exception e) { Console.Error.WriteLine("Birthday file not found"); }; try { reader.Close(); } catch (Exception e) { } announcementChannels.Add((SocketChannel)guild.GetChannel(channelIDNum)); //if no file, just continue if (birthdayReader == null) { continue; } //while we have birthdays to read while(birthdayReader.Peek() >= 0) { //read a line string line = ""; try { line = birthdayReader.ReadLine(); } catch(Exception e) { Console.WriteLine("Error reading line from birthday file"); } //create a new birthday user and parse the line UserBirthday birthday = new UserBirthday(); birthday.parseLine(line); //add this to list of birthdays serverBirthday.addUser(birthday); } //add serverBirthday to the list of server birthdays serverBirthdays.Add(serverBirthday); //close our readers try { birthdayReader.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } } return; }