public async Task AssignApplicantAsync([Remainder][Summary("The applicant id to verify")] string parameters = null) { string userLookup = Context.User.Username + "#" + Context.User.Discriminator; SocketGuildUser user = Bot.GetSocketGuildUser(userLookup); string reply = "Something went wrong, not sure what."; string applicantReferenceIdString = ""; if (Context.IsPrivate) { if (parameters != null) { applicantReferenceIdString = parameters.Trim(); if (applicantReferenceIdString.Length != 9) { // this is not an applicant id as it is not 9 digits long reply = "The applicant id that you provided: " + applicantReferenceIdString + " is not a 9 digit number. The correct way to use this command is: !applicant 123456789 (where 123456789 should be replaced with your own applicant id)"; } else { // 9 characters, could be id int applicantReferenceId; bool isNumber = int.TryParse(applicantReferenceIdString, out applicantReferenceId); if (!isNumber) { // may have been 9 digits but was not an integer reply = "The applicant id that you provided: " + applicantReferenceIdString + " is not a 9 digit number. The correct way to use this command is: !applicant 123456789 (where 123456789 should be replaced with your own applicant id)"; } else { // it was 9 digits and successfully parsed as an int // now we can check the db for the applicant id to // verify Applicant applicant = null; bool isApplicant = ApplicantsFile.Instance.TryGetApplicant(applicantReferenceId, out applicant); // check DB if (isApplicant) { bool snowflakeAdded = applicant.AddDiscordSnowflake(user.Id); if (snowflakeAdded) { // assignRole of applicant as they have supplied a valid id _ = Bot.AddRoleToUser(user, Bot.GetRole("applicant")); reply = "Thanks. Welcome to the Computer Science and Technology Discord Server. As an applicant you now have access to the Applicant Zone; check out the channels in there and feel free to talk amongst yourselves or ask us any questions that you like."; //add applicant to discordLookup list ApplicantsFile.Instance.UpdateDiscordLookup(applicant); } else { reply = "The applicant id that you provided: " + applicantReferenceIdString + " did not work. Please check that you have supplied the right id. If you are sure, then please get in touch as something has gone wrong.)"; } } else { reply = "The applicant id that you provided: " + applicantReferenceIdString + " did not work. Please check that you have supplied the right id. If you are sure, then please get in touch as something has gone wrong.)"; } } } } else { // no parameter provided to command reply = "You attempted to use this command without its required parameter: your 9 digit applicant id. The correct way to use this command is: !applicant 123456789 (where 123456789 should be replaced with your own applicant id)"; } } else { reply = "Please send me this request in a Direct Message (you can reply to this message if you like). You should delete your previous public message if you can."; } Bot.SendMessage(user, reply); _ = FileLogger.Instance.Log(new LogMessage(LogSeverity.Info, "Bot", "[Applicant]: " + userLookup + " sent applicant id " + applicantReferenceIdString + " and was told: " + reply)); string notification = "[Applicant]: " + userLookup + " sent applicant id " + applicantReferenceIdString + " and was told: " + reply; Bot.Notify(notification); }
public async Task AddRoleAsync([Remainder][Summary("The role to add to a comma separated list of users")] string parameters = null) { string userLookup = Context.User.ToString(); SocketGuildUser requester = Bot.GetSocketGuildUser(userLookup); string reply = "Something went wrong, not sure what."; if (Context.IsPrivate) { if (parameters != null) { // we need to be careful here as we can't just split by space as some of the roles and username descriminator combos have spaces int openQuotePos = parameters.IndexOf('"'); if (openQuotePos != -1) { int closeQuotePos = parameters.IndexOf('"', openQuotePos + 1); int length = closeQuotePos - (openQuotePos + 1); string roleString = parameters.Substring(openQuotePos + 1, length); SocketRole role = Bot.GetRole(roleString); if (role != null) { parameters = parameters.Substring(closeQuotePos + 1); string[] parametersTokens = parameters.Split(','); int totalNumber = parametersTokens.Length; int count = 0; foreach (string parameterToken in parametersTokens) { string partialReply = "Something went wrong and I don't know what.\n"; string roleAddee = parameterToken.Trim(); if (roleAddee.Contains('#')) { // discord user lookup // should return university id and user details SocketGuildUser discordUser = Bot.GetSocketGuildUser(roleAddee); if (discordUser != null) { //matches server discord user ulong discordSnowflake = discordUser.Id; Student student = null; if (StudentsFile.Instance.TryGetDiscordStudent(discordSnowflake, out student)) { _ = Bot.AddRoleToUser(discordUser, role); partialReply = "I have added the role: " + roleString + " to user: "******"#" + discordUser.Discriminator + " (" + student.StudentId + ")\n"; } else { partialReply = "The Discord username and discriminator (" + discordUser.Username + "#" + discordUser.Discriminator + ") does not match a student in our records. Please check that you have typed it correctly. They may not have verified their Discord user using the Canvas quiz.\n"; } } else { partialReply = "The Discord username and discriminator (" + roleAddee + ") does not match a Discord user on the server. Please check that you have typed it correctly\n"; } } else if (roleAddee.Length == 9) { int studentId = 0; if (int.TryParse(roleAddee, out studentId)) { Student student = null; if (StudentsFile.Instance.TryGetStudent(studentId, out student)) { // this discord user matches one of the students SocketGuildUser discordUser = Bot.GetSocketGuildUser(student.DiscordSnowflake); if (discordUser != null) { _ = Bot.AddRoleToUser(discordUser, role); partialReply = "I have added the role: " + roleString + " to user: "******"#" + discordUser.Discriminator + " (" + student.StudentId + ")\n"; } else { partialReply = "The student id provided (" + roleAddee + ") is in the list, but may have left the server.\n"; } } else { partialReply = "The student id provided (" + roleAddee + ") does not match our records. Please check that you have typed it correctly. They may not have joined the Discord server.\n"; } } } reply = count is 0 ? partialReply : reply += partialReply; count++; } } else { reply = "The role " + roleString + " does not exist on the server. Please check that you have typed it correctly."; } } else { // no space so there is a shortage of parameters reply = "You attempted to use this command without its required parameters: the role you want to add, and the student id that you want to add it to."; } } else { // no parameter provided to command reply = "You attempted to use this command without its required parameters: the role you want to add, and the student id that you want to add it to."; } } else { reply = "Please send me this request in a Direct Message (you can reply to this message if you like). You should delete your previous public message if you can."; } Bot.SendMessage(requester, reply); string requesterLookup = requester.Username + "#" + requester.Discriminator + " (" + requester.Nickname + ")"; _ = FileLogger.Instance.Log(new LogMessage(LogSeverity.Info, "AddRoleModule", "[AddRole]: " + requesterLookup + " was told: " + reply)); }