/// <summary> /// An event handler which is fired when a role has been updated /// It manages the CommandList as appropriate if the name has been changed /// </summary> /// <param name="oldRole"></param> /// <param name="newRole"></param> /// <returns></returns> private void roleUpdated(SocketRole oldRole, SocketRole newRole) { // If the role updated was in the whitelist and the name was updated, update the commandlist if (WhitelistedRoles.Contains(oldRole.Id.ToString()) && oldRole.Name != newRole.Name) { string oldRoleName = GetRoleNameCommand(oldRole.Name); // Remove the old role from the list so we can check if there are other roles with the same name WhitelistedRoles.Remove(oldRole.Id.ToString()); // If none exist with the same name, we can safely remove the command if (WhitelistedRoles.All((id) => GetRoleNameCommand(GetRoleByID(id)?.Name) != oldRoleName)) { server.Commandlist.Remove(oldRoleName); } // Readding the role because the ID didn't change, and it was just for the check above WhitelistedRoles.Add(oldRole.Id.ToString()); // We might be trying to add the same command twice, because of roles that share the same first word // For example: Stable Dweller exists, now trying to add Stable Pony. if (!server.Commandlist.ContainsKey(GetRoleNameCommand(newRole.Name))) { server.Commandlist.Add(GetRoleNameCommand(newRole.Name), new Command(server, GetRoleNameCommand(newRole.Name), ModifyRolesByCommandName, PrivilegeLevel.Everyone, new HelpMsgStrings("", ""))); } Console.WriteLine("The role " + oldRole.Name + " has been updated to the role " + newRole); } }
/// <summary> /// Grants/removes the user the specified role /// </summary> /// <param name="e"></param> /// <param name="roleName"></param> private void ModifyRoles(ServerMessage e, string roleName) { if (mayNotAccessRoleCommands(e)) { return; } // Help! if (roleName == "" || roleName == "help") { server.safeSendMessage(e.Channel, "Just write ``!<role name> of the role you want to get/remove>``.\n" + "For example: ``!wastelander`` or ``!stable dweller``"); return; } // Sanitization, to stop @everyone pings roleName = roleName.Replace("@", ""); var specifiedRole = server.getServer().Roles.FirstOrDefault((r) => r.Name.ToLower() == roleName.ToLower()); // If there is no such role, print a response if (specifiedRole == null) { server.safeSendMessage(e.Channel, "Sorry, but the role '" + roleName + "' does not exist! Please write ``!role help`` for assistance!"); } // If the role is whitelisted else if (WhitelistedRoles.Contains(specifiedRole.Id.ToString())) { // If the user has that role, remove it if (specifiedRole.Members.Any((u) => u.Id == e.Author.Id)) // You could probably check it the other way around too, but this already works and I'm pretty lazy { e.Author.RemoveRoleAsync(specifiedRole); server.safeSendMessage(e.Channel, "*Woof!* I have borked the " + roleName + " role off of you!"); } // If the user doesn't have that role, add it else { e.Author.AddRoleAsync(specifiedRole); server.safeSendMessage(e.Channel, "*Woof!* I have borked the " + roleName + " role onto you!"); } } // Message if the role selected is forbidden else { server.safeSendMessage(e.Channel, "This role is unavailable to simple plebians such as yourself."); } }
/// <summary> /// Adds/removes whitelisted roles the users can't get via the !role command /// </summary> /// <param name="e"></param> /// <param name="roleName"></param> private void ModifyWhitelistedRole(ServerMessage e, string roleName) { if (!on) { return; } var specifiedRole = server.getServer().Roles.FirstOrDefault((r) => r.Name.ToLower() == roleName.ToLower()); if (specifiedRole == null) { server.safeSendMessage(e.Channel, "The role " + roleName + " does not exist!"); } // Add the role to the whitelist else if (!WhitelistedRoles.Contains(specifiedRole.Id.ToString())) { WhitelistedRoles.Add(specifiedRole.Id.ToString()); // We might be trying to add the same command twice, because of roles that share the same first word // For example: Stable Dweller exists, now trying to add Stable Pony. if (!server.Commandlist.ContainsKey(GetRoleNameCommand(specifiedRole.Name))) { server.Commandlist.Add(GetRoleNameCommand(specifiedRole.Name), new Command(server, GetRoleNameCommand(specifiedRole.Name), ModifyRolesByCommandName, PrivilegeLevel.Everyone, new HelpMsgStrings("", ""))); } server.safeSendMessage(e.Channel, "The role " + specifiedRole.Name + " has been added to the list of whitelisted roles users could get via the !role command, or by writing !<role name>"); } // Remove the role from the whitelist else { WhitelistedRoles.Remove(specifiedRole.Id.ToString()); string realRoleName = GetRoleNameCommand(specifiedRole.Name); // If none exist with the same name, we can safely remove the command if (WhitelistedRoles.All((id) => GetRoleNameCommand(GetRoleByID(id)?.Name) != realRoleName)) { server.Commandlist.Remove(realRoleName); } server.safeSendMessage(e.Channel, "The role " + specifiedRole.Name + " has been removed from the list of whitelisted roles users could get via the !role command, or by writing !<role_name>"); } }
/// <summary> /// An event handler which is fired when a role has been deleted /// It manages the whitelist and commandlist as appropriate /// </summary> /// <param name="deletedRole"></param> /// <returns></returns> private void roleDeleted(SocketRole deletedRole) { // If the deleted role is from this server and is whitelisted, properly remove it from all lists if (WhitelistedRoles.Contains(deletedRole.Id.ToString())) { string deletedRoleName = GetRoleNameCommand(deletedRole.Name); // Remove the old role from the list so we can check if there are other roles with the same name //if it was never in, we can stop here if (!WhitelistedRoles.Remove(deletedRole.Id.ToString())) { return; } // If none exist with the same name, we can safely remove the command if (WhitelistedRoles.All((id) => GetRoleNameCommand(GetRoleByID(id)?.Name) != deletedRoleName)) { server.Commandlist.Remove(deletedRoleName); } Console.WriteLine("The role " + deletedRole.Name + " was deleted, updated its whitelist/commandlist standings"); } }