private static List <ulong> GetAdditionalRoleIds(WalrusUserInfo userInfo, SocketGuild guild, dbWalrusContext database) { List <ulong> customRoles = new List <ulong>(); if (userInfo.AdditionalRolesJSON != null) { // not going to try as *should* always be valid JSON if added using a command JObject customRolesJason = JObject.Parse(userInfo.AdditionalRolesJSON); foreach (var role in customRolesJason) { if (UInt64.TryParse(role.Value.ToString(), out ulong id)) { if (guild.Roles.Any(r => r.Id == id)) { customRoles.Add(id); } } } /* * foreach (string s in customRoleStrings) * { * string idString = database["role", s]; * if (idString != null) * { * if (UInt64.TryParse(idString, out ulong id)) * { * if (guild.Roles.Any(r => r.Id == id)) customRoles.Add(id); * } * } * }*/ } return(customRoles); }
public async Task MessageNonVerifiedAsync() { foreach (SocketGuildUser user in Context.Guild.Users) { if (user.IsBot) { continue; } if (user == Context.User) { continue; } if (user == Context.Guild.Owner) { continue; } Console.WriteLine($"Spamming {user.Username}..."); string s = user.Id.ToString(); try { WalrusUserInfo userInfo = database.WalrusUserInfoes.Where(x => x.UserId == s).FirstOrDefault(); if (userInfo == null) { IDMChannel c = await(user as SocketUser).GetOrCreateDMChannelAsync(); await c.SendMessageAsync("Hi there! I noticed that you haven't yet verified your email address with us on our server. " + "You can do that by sending me `svge!verify email <your email>`, then send me the code you recieve with `svge!verify code <code>`. " + "You should do it soon so you don't get kicked from the server and can get access to more server channels! "); } else { if (UInt64.TryParse(database["role", "student"], out ulong studentRoleId) && UInt64.TryParse(database["role", "communityMember"], out ulong commMemberId)) { if (user.Roles.Any(r => r.Id == studentRoleId) && !user.Roles.Any(r => r.Id == commMemberId)) { IDMChannel c = await(user as SocketUser).GetOrCreateDMChannelAsync(); await c.SendMessageAsync("Hi there! I noticed that you've verified your email with us but haven't yet got membership with us on SUSU, which you can get for free! " + "This really helps us out when we're asking SUSU for support or funding, so please go grab the free Community Membership here: " + "https://www.susu.org/groups/southampton-university-esports-society"); } } } } catch (Exception e) { Console.WriteLine(e.Message); } } }
public async Task PermaRoleAsync(SocketGuildUser user, string roleName, ulong roleId) { IRole role = Context.Guild.GetRole(roleId); if (role == null) { await ReplyAsync("That role doesn't appear to exist on this server! (honestly not sure how you even managed that tbh...)"); return; } string userIdString = user.Id.ToString(); WalrusUserInfo userInfo = database.WalrusUserInfoes.Where(x => x.UserId == userIdString).FirstOrDefault(); if (userInfo == null) { userInfo = database.WalrusUserInfoes.Add(new WalrusUserInfo { UserId = user.Id.ToString(), Verified = false, Username = user.Username + "#" + user.Discriminator, Email = null, Code = null, IGNsJSON = @"{}", AdditionalRolesJSON = @"{}" }); } // not running checks as should always be valid JSON JObject additionalRolesJson = JObject.Parse(userInfo.AdditionalRolesJSON); if (!additionalRolesJson.ContainsKey(roleName)) { additionalRolesJson.Add(roleName, roleId.ToString()); userInfo.AdditionalRolesJSON = additionalRolesJson.ToString(); await database.SaveChangesAsync(); await user.AddRoleAsync(role); } await ReplyAsync("Permanent role added to user :)"); }
public async Task CodeAsync(string code) { if (!Context.IsPrivate) { await ReplyAsync(Context.User.Mention + " " + database["string", "errReqDm"]); await Context.Message.DeleteAsync(); return; } string userId = Context.User.Id.ToString(); WalrusUserInfo userInfo = database.WalrusUserInfoes.Where(x => x.UserId == userId).FirstOrDefault(); if (userInfo == null) { await ReplyAsync("It appears you haven't yet registered your email with us! " + "Send me `svge!verify email <your_email> to start the verification process :)"); } else { SocketGuild guild = Context.User.MutualGuilds.Where(x => x.Id.ToString() == database["config", Program.Debug ? "guildDebugId" : "guildId"]).First(); if (userInfo.Code == code) { userInfo.Verified = true; await database.SaveChangesAsync(); await VerifyModule.UpdateAsync(guild.Users.Where(x => x.Id == Context.User.Id).Single(), guild, database); await ReplyAsync("You are now verified and your roles have been updated! Welcome to SVGE :D"); } else { await ReplyAsync("That code appears to be invalid! If you've changed your email recently then you will have been sent " + "a new code to use, so please double check your email :)"); } } }
public async Task EmailAsync([Remainder] string email) { if (!Context.IsPrivate) { await ReplyAsync(Context.User.Mention + " " + database["string", "errReqDm"]); await Context.Message.DeleteAsync(); return; } if (!Context.User.MutualGuilds.Any(x => x.Id.ToString() == database["config", Program.Debug ? "guildDebugId" : "guildId"])) { await ReplyAsync("You aren't a member of our Discord server! If you're a student as the University of Southampton or are a SUSU member," + $"you can join our Discord here: {database["config", "guildInvite"]}"); } if (!IsValidEmail(email)) { await ReplyAsync(database["string", "errEmailInvalid"]); return; } WalrusUserInfo userInfo = await database.WalrusUserInfoes.FindAsync(Context.User.Id.ToString()); if (database.WalrusUserInfoes.Any(x => x.Email == email)) // see if someone's already verified/attempted to verify with that email { if (email == (userInfo.Email ?? "")) { if (userInfo.Verified) { await ReplyAsync("You're already verified with that email! If you're missing roles then wait for a role updated (run daily)" + "or contact a member of the committee if you've still not got your role."); } else { await ReplyAndDeleteAsync("You've aleady attempted to verify with that email. Do you want me to send you another email?" + " Type \"yes\" to confirm or \"no\" to cancel.", timeout : TimeSpan.FromSeconds(31)); var response = await NextMessageAsync(new EnsureFromUserCriterion(Context.User.Id), timeout : TimeSpan.FromSeconds(30)); if (response == null) { return; } if (response.Content.ToLower() != "yes") { await ReplyAsync("I won't send you another email :)"); return; } else { await SendEmailAsync(email, userInfo.Code); await ReplyAsync("Okay, I've sent you a new email :)"); } } } else // someone has already verified with this email { await ReplyAsync("Someone has already verified with this email! If you believe this to be a mistake then please contact " + "a member of the committee ASAP!!!"); } } else { if (userInfo == null) { var newUser = new WalrusUserInfo { UserId = Context.User.Id.ToString(), Verified = false, Username = Context.User.Username + "#" + Context.User.Discriminator, Email = email.ToLower(), Code = RandomString(8), IGNsJSON = @"{}", AdditionalRolesJSON = @"{}" }; if (await SendEmailAsync(email, newUser.Code)) { database.WalrusUserInfoes.Add(newUser); await database.SaveChangesAsync(); await ReplyAsync("Email sent! Once you've got your code, DM me with `svge!verify code <your code>` :)"); } } else { if (userInfo.Verified) { await ReplyAndDeleteAsync($"You're already verified with the email {userInfo.Email}. Are you sure you want to unverify and use" + "this email instead? Type \"yes\" to confirm or \"no\" to cancel.", timeout : TimeSpan.FromSeconds(31)); var response = await NextMessageAsync(new EnsureFromUserCriterion(Context.User.Id), timeout : TimeSpan.FromSeconds(30)); if (response == null) { return; } if (response.Content.ToLower() != "yes") { await ReplyAsync("Okay, I won't reset your verification :)"); return; } else { userInfo.Email = email.ToLower(); userInfo.Verified = false; userInfo.Code = RandomString(8); if (await SendEmailAsync(email, userInfo.Code)) { await database.SaveChangesAsync(); } } } else { // they exist in the database but they've not not verified yet or added their email userInfo.Email = email.ToLower(); userInfo.Code = RandomString(8);; if (await SendEmailAsync(email, userInfo.Code)) { await database.SaveChangesAsync(); await ReplyAsync("Email sent! Once you've got your code, DM me with `svge!verify code <your code>` :)"); } } } } }
public async Task ResetUsersAsync() { int num = 1; foreach (IGuildUser user in Context.Guild.Users) { try { Console.WriteLine($"[{num++}] Resetting roles for {user.Username}..."); if (user.IsBot) { continue; } if (user.Id == Context.Guild.OwnerId) { continue; } if (user == Context.User) { continue; } string userId = user.Id.ToString(); WalrusUserInfo userInfo = database.WalrusUserInfoes.Where(x => x.UserId == userId).FirstOrDefault(); if (userInfo == null) // not verified, email not even in db { List <IRole> roles = user.RoleIds.ToList().ConvertAll(x => Context.Guild.GetRole(x)).ToList <IRole>(); foreach (IRole role in roles) { if (!role.IsManaged && !(role == Context.Guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } continue; } if (userInfo.AdditionalRolesJSON == null) // no custom roles { List <IRole> roles = user.RoleIds.ToList().ConvertAll(x => Context.Guild.GetRole(x)).ToList <IRole>(); foreach (IRole role in roles) { if (!role.IsManaged && !(role == Context.Guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } database.WalrusUserInfoes.Remove(userInfo); } else { // get custom role IDs List <ulong> customRoleIds = GetAdditionalRoleIds(userInfo, Context.Guild, database); // cast to IRole list, excluding custom roles, and remove them from user List <IRole> roles = user.RoleIds.Except(customRoleIds).ToList().ConvertAll(x => Context.Guild.GetRole(x)).ToList <IRole>(); foreach (IRole role in roles) { if (!role.IsManaged && !(role == Context.Guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } // reset their database entry userInfo.Verified = false; userInfo.Email = null; userInfo.Code = null; } } catch (Exception e) { Console.WriteLine(e.Message); } } await database.SaveChangesAsync(); await ReplyAsync("User information for this server has been reset..."); }
// called by daily function (via above static function) or by the code command private static async Task UpdateAsync(SocketGuildUser user, SocketGuild guild, dbWalrusContext database) // not callable as a Discord command { //Console.WriteLine("Updating..."); Console.WriteLine($"Updating user information for {user.Nickname ?? user.Username}:"); if (user.IsBot) { return; } string userId = user.Id.ToString(); WalrusUserInfo userInfo = database.WalrusUserInfoes.Where(x => x.UserId == userId).FirstOrDefault(); if (userInfo == null) { Console.WriteLine($"\tUser {user.Nickname ?? user.Username} does not exist in the database!"); List <IRole> roles = (user as IGuildUser).RoleIds.ToList().ConvertAll(x => guild.GetRole(x) as IRole); Console.WriteLine($"\tNumber of roles: {roles.Count}"); foreach (IRole role in roles) { if (!role.IsManaged && !(role == guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } return; } Console.WriteLine("\tGetting custom roles"); List <ulong> customRoleIds = GetAdditionalRoleIds(userInfo, guild as SocketGuild, database); if (!userInfo.Verified) { Console.WriteLine($"\tUser {user.Nickname ?? user.Username} is not verified!"); // remove everything that isn't a custom role List <IRole> roles = (user as IGuildUser).RoleIds.Except(customRoleIds).ToList().ConvertAll(x => guild.GetRole(x) as IRole); Console.WriteLine($"\tNumber of roles: {roles.Count}"); foreach (IRole role in roles) { if (!role.IsManaged && !(role == guild.EveryoneRole) && !role.Name.Contains("CPS")) { try { await user.RemoveRoleAsync(role); } catch { Console.WriteLine($"\tFailed to remove role {role.Name}."); } } } foreach (IRole role in customRoleIds.ConvertAll(id => guild.GetRole(id))) { if (!role.IsManaged && !(role == guild.EveryoneRole)) { try { await user.AddRoleAsync(role); } catch { Console.WriteLine($"\tFailed to add customer role {role.Name}"); } } } return; } IRole studentRole = ParseRole(database, "student", guild as SocketGuild); if (studentRole != null) { if (userInfo.Email.Split('@')[1] == database["config", "studentEmailDomain"]) { await user.AddRoleAsync(studentRole); } else { Console.WriteLine("\tRemoving student role..."); await user.RemoveRoleAsync(studentRole); } } WalrusMembershipList membership = database.WalrusMembershipLists.Where(x => x.Email == userInfo.Email).FirstOrDefault(); IRole communityMembershipRole = ParseRole(database, "communityMember", guild as SocketGuild); IRole dlcMembershipRole = ParseRole(database, "dlcMember", guild as SocketGuild); IRole gotyMembershipRole = ParseRole(database, "gotyMember", guild as SocketGuild); if (membership != null) { /* if they're in the membership list then they at least have the community membership level */ if (communityMembershipRole != null) { await user.AddRoleAsync(communityMembershipRole); } if (membership.Membership.Replace("\"", "").Split(',').Contains("DLC Bundle Membership") && dlcMembershipRole != null) { await user.AddRoleAsync(dlcMembershipRole); } if (membership.Membership.Replace("\"", "").Split(',').Contains("Game of the Year Membership") && gotyMembershipRole != null) { await user.AddRoleAsync(gotyMembershipRole); } } else { if (communityMembershipRole != null) { await user.RemoveRoleAsync(communityMembershipRole); } if (dlcMembershipRole != null) { await user.RemoveRoleAsync(dlcMembershipRole); } if (gotyMembershipRole != null) { await user.RemoveRoleAsync(gotyMembershipRole); } } //get additional roles only returns IDs for roles that exist in the guild so no need to check again foreach (IRole role in customRoleIds.ConvertAll(id => guild.GetRole(id))) { try { await user.AddRoleAsync(role); } catch { Console.WriteLine($"\tFailed to add role {role.Name}."); } } }